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);
...
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 months ago.
Improve this question
creditTIDstatusArray=[93312263-1-09722612223, 99802001-1-09102842369, 99802002-1-09102842369];
creditTIDstatusList.addAll(Arrays.asList(creditTIDstatusArry));
searchValue="99802002-1".
int retval=creditTIDstatusList.indexOf("99802002-1");
System.out.println("The element at index is:" retval);
Output: 2
Please let me know how I can find the index of the given above(searchValue)element.
As you never have posted the reproducible code.
Assumptions
creditTIDstatusArray is a String type array.
Your search query always stays in front of each String value in the array.
Multiple indexes may start with the same search value.
String[] creditTIDstatusArray=new String[]{"93312263-1-09722612223", "99802001-1-09102842369", "99802002-1-09102842369"};
String searchValue="99802002-1";
for (int i = 0; i < creditTIDstatusArray.length; i++) {
if(creditTIDstatusArray[i].startsWith(searchValue)){
System.out.println("Index :" + I); // this will print all the indexes that starts with searchvalue
}
}
I am not sure why you have added the array into a list and then searched index of because it will never work as you are searching only part of a String rather than the whole value or object.
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));
}
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.
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
public String toString() {
return questionText + "\n";
char label = 'a';
for(int i= 0; i < answers.length; i++){
return "("+label+")"+" "+answers[i]+ "\n";
label++;
}
}
It keeps saying line 4 is unreachable.
Because of return Statement at the very first line.
Once return is called, following lines doesn't matter.
I don't know what questionText is doing, but it may work fine even without the first line itself. Cheers
Because you have a return statement (question text) immediately after the start. So the char assignment will not run
After the return, you've left the function. Anything below that (within this function) can never execute.
This is how it should be with your code, and please dont forget, if answers.length() is just 2 also, it will return only first answer as you are already calling return. I would recommend saving this in list and returning list.
public String toString() { return questionText + "\n"; char label = 'a';
for(int i= 0; i < answers.length; i++){
label++;
return "("+label+")"+" "+answers[i]+ "\n";
}
With list :
public String toString() {
List<String> stringList = new ArrayList();
for(int i= 0; i < answers.length; i++){
arrayList.add(answers);
label++;
}
return arrayList;
I cannot understand your data structure as its written very badly by you. So, I can only help this much.
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]);