Convert a string to int array [duplicate] - android

This question already has answers here:
How to convert object array to string array in Java
(11 answers)
Closed 9 years ago.
I need to convert a string stored like,
textView.setText(myVar);
Here
myVar = "23,45,64,78";
I would like it to convert into array like below,
int[] xAxis = new int[]{23,45,64,78};
How can I achieve this? Thanks

Try this:
String arr = "[23,45,64,78]";
String[] items = arr.replaceAll("\\[", "").replaceAll("\\]", "").split(",");
int[] results = new int[items.length];
for (int i = 0; i < items.length; i++) {
try {
results[i] = Integer.parseInt(items[i]);
} catch (NumberFormatException nfe) {};
}

Related

How to get a random value from a string array without repetition?

How to get a random value from a string array in android without repetition?
I have array in String.xml file as below -
<string-array name="msg">
<item>Cow</item>
<item>Pig</item>
<item>Bird</item>
<item>Sheep</item>
</string-array>
I am selecting random string by using following code -
String[] array = Objects.requireNonNull(context).getResources().getStringArray(R.array.msg);
String Msg = array[new Random().nextInt(array.length)];
Can anyone help me please? Thanks is advance...!
Can you just do something like this:
Collections.shuffle(copyOfArray);
Then loop through that?
for (int i = 0; i < copyOfArray.size(); i++) {
println(copyOfArray.get(i))
}
try this -
array = Objects.requireNonNull(context).getResources().getStringArray(R.array.msg);
//String msg = array[new Random().nextInt(array.length)];
LinkedList<String> myList = new LinkedList<String>();
for (String i : array)
myList.add(i);
Collections.shuffle(myList);
for (int i = 0; i < myList.size(); i++) {
String msg=myList.get(i);
}
Try this solution,
LinkedList<String> myList = new LinkedList<String>();
String[] words = { "Cow", "Pig", "Bird", "Sheep" };
for (String i : words)
myList.add(i);
Collections.shuffle(myList);
Then,
sampleText.setText(myList.pollLast());
pollLast() in LinkedList will retrieves and removes the last element of this list, or returns null if this list is empty.
try this.
int max = array.length() - 1;
int min = 0;
String Msg = array[new Random().nextInt(max - min + 1) + min];
First convert your String resource array to ArrayList
Fill value from current ArrayList to HashSet and convert that HashSet to newly ArrayList
Now shuffle that new ArrayList

JSON inner query in android [duplicate]

This question already has answers here:
How do I parse JSON in Android? [duplicate]
(3 answers)
Closed 6 years ago.
"BRAZIL": {
"channel": "brazil_ch",
"city": {
"Belo Horizonte": "belohorizonte_ch_org",
"Brasilia": "brasilia_ch_org",
"Curitiba": "curitiba_ch_org",
"Fortaleza": "fortaleza_ch_org",
"Porto Alegre": "portoalegre_ch_org",
"Recife": "recife_ch_org",
"Rio De Janeiro": "rio_ch_org",
"Salvador, Bahia": "salvador_ch_org",
"Sao Paulo": "saopaulo_ch_org"
}
}
This is my JSON. I need to retrieve the channel value in Brazil and also city values. Please help me.
Thanks in Advance.
JSONObject jsonObject = new JSONObject(jsonString);
String channel = jsonObject.getJSONObject("BRAZIL").getString("channel");
JSONArray jsonArray = jsonObject.getJSONObject("BRAZIL").getJSONArray("city");
int length = jsonArray.length();
String[] cities = new String[length];
for (int i = 0; i < length; i++) {
String city = jsonArray.getString(i);
cities[i] = city;
}

How to pass edit text values to integer array in Android

I'm a beginner in Android.
I have created a edit text field where can I enter values from 0-9. Now, I have to get these values in an integer array.
example : entered values are 12345.
I need an array containing these values
int[] array = {1,2,3,4,5};
I need to know the way to do this. Kindly help.
Try this :
String str = edittext.getText.toString();
int length = str.length();
int[] arr = new int[length];
for(int i=0;i<length;i++) {
arr[i] = Character.getNumericValue(str.charAt(i));
}
You can use something like this:
int[] array = new int[yourString.length()];
for (int i = 0; i < yourString.length(); i++){
array[i] = Character.getNumericValue(yourString.charAt(i));
}

how can i split the particular data from a string? [duplicate]

This question already has answers here:
Split string at commas
(4 answers)
Closed 7 years ago.
I am getting the lat-long from the current location now how can i split the only lat long values from a given string :-
"lat/lng: (23.0326542,72.5279697)"? i want to get only "23.0326542,72.5279697".
Try this:
String line = "lat/lng: (23.0326542,72.5279697)";
String[] array = line.split(",");
for (int i = 0; i < array.length; i++) {
Pattern pattern = Pattern.compile("(\\d+\\.\\d+)|(\\d+)");
Matcher m = pattern.matcher(array[i]);
if (m.find()) {
System.out.println(m.group(1));
}
}
It looks like you should be taking the substring of this string, from the beginning of the value you need, to the length of the string.

How do I split these String Arraylist [{ } , {}, { }...] to strings or Integer? [duplicate]

This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 8 years ago.
I have,
[{"GroupPosition":0,"ChildPosition":0},{"GroupPosition":0,"ChildPosition":1},{"GroupPosition":1,"ChildPosition":0}]
How do I split them into:
GroupPosition:
[0,0,1] or {0,0,1}
ChildPosition:
[0,0,0] or {0,0,0}
Your input is in json format. Consider my code is sample.
private static ArrayList<String> array_1 = new ArrayList<String>();;
String json = [{"GroupPosition":0,"ChildPosition":0},{"GroupPosition":0,"ChildPosition":1},{"GroupPosition":1,"ChildPosition":0}];
JSONArray leagues = new JSONArray(json);
for (int i = 0; i < leagues.length(); i++) {
JSONObject obj = leagues.getJSONObject(i);
array_1.add(obj.getString("GroupPosition"));
}

Categories

Resources