String Array to Integer Array Java [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a string array for an app I am making. It loads an EditText, so I will instead create a dummy one.
String[] s = ["1","2","3"];
How would I change String s into an integer array, such as Int i?
Int[] i = [1,2,3];
I have tried Integer.parseInt on the string array but it does not work. Does anyone know how to convert string arrays into integer arrays?

Something like the following should work:
string[] s = ["1","2","3"];
int[] i = new int[s.length()];
for (int j = 0; j < s.length(); j++)
i[j] = Integer.parseInt(s[j]);

Related

how do I split array element into more element [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
this is my array:
[UserID|Name|Password|IC or Passport Number|Gender|Phone, 01234567|Bob|abcderf|MALE|01234567]
and I want to convert into:
[UserID|Name|Password|IC or Passport Number]
[01234567|Bob|abcderf|MALE|01234567]
this is my code: (but it doesn't work]
String[] arr = userList.toArray(new String[userList.size()]);
String[] arrr = arr[0].split(",");
for ( int i=0; i<arr.length;i++)
{
System.out.println(arr[i]);
}
You can simply use method from Arrays if lenght of array is always the same
String[] array1 = Arrays.copyOfRange(array, 0, 4);
String[] array2 = Arrays.copyOfRange(array, 4, 9);

Why my Variable richtigeHoeren only counts until 10? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have programmed an array which contains 15 Words. Now i wanna compare it with another array which contains the right words. Whenever a word is correkt i want increase my variable (richtigeHoeren). But when i type all the correct 15 words in my array the variable only counts until 10..
I dont know why, i hope you understand my question.
for(int k=0; k<begriffLoesungHoeren.size(); k++){
// Get the user input
String eintragung = begriffEintragungHoeren.get(k);
if(eintragung!=null){
// Compare user input with solution , if solution is right increase richtigeHoeren and jump to first for loop.
if(eintragung.equals(loesung)){
richtigeHoeren++;
break;
}
}
}
If you want to compare each element of the first list with each element of the second list, you should use the correct bounds:
for(int i = 0; i < begriffLoesungHoeren.size(); i++) {
String loesung = begriffLoesungHoeren.get(i);
for(int k = 0; k < begriffEintragungHoeren.size(); k++) {
String eintragung = begriffEintragungHoeren.get(k);
...

How to convert ArrayList of Strings to ints, so they can be added? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am trying to convert each String in an ArrayList to int so then I can add them,
asuma is an ArrayList containing String
I need to iterate over this ArrayList to convert String to int
for (int i = 0; i <=asuma.size(); i++) {
and then I need to add the integers. How do I do it?
I suspect that the ArrayList you are referring to is asuma and that it is an ArrayList<String>. In that case, you can do this:
for (String s : asuma) {
int valorFinal = Integer.parseInt(s);
}
I think u try that:
for (int i = 0; i <=asuma.size(); i++) {
int valorFinal = Integer.parseInt(asuma.get(i));
}

How to find first letter of each value in array using charAt in Java? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
As the title depicts, i'm trying to find the first letter of each value in a string array in Java using charAt. Once I used the included code, I got an error saying that they are incompatible type, where char cannot be converted to String. Any help? Thanks.
for (int i = 0; i < arraySize; i++)
{
firstName [i] = name[i].charAt(0);
}
It looks like you want a String from the entry in the name array not a char.
Use this instead
firstName [i] = name[i].substring(0, 1);
Change the char to a String using Character.toString(char), like
for (int i = 0; i < arraySize; i++) {
firstName[i] = Character.toString(name[i].charAt(0));
}
or change firstName to a char[] like
char[] firstName = new char[arraySize];
for (int i = 0; i < arraySize; i++) {
firstName[i] = name[i].charAt(0);
}
A one character char is a primitive type, a String is an Object type (a char is not a String). Also, please change the variable name because a letter is not a name. Perhaps firstInitial.

Convert a String to a Vector in Java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a function that has an output as a 4 character String like "1000". I need to convert this into a vector of dimension (4,1) in order to be able to make computation with matrices.
Any idea or help? Thank you very much in advance.
String s = "1000";
Vector myVec = new Vector();
//Convert the string to a char array and then just add each char to the vector
char[] sChars = s.toCharArray();
for(int i = 0; i < s.length(); ++i) {
myVec.add(sChars[i]);
}
Try:
Vector<Character> v = new Vector<Character>(Arrays.asList(yourString.toCharArray()))
As stated by fge, a List would be at least as useful as a Vector:
List<Character> l = Arrays.asList(yourString.toCharArray())

Categories