0

ArrayList bug? Solved!

I loaded a code up under the same title as here and my problem is that the System.out.println("Start"); runs two times when i use the ArrayList .get() method. I use a switch so the user can decide whether to add a String to the List or giving the program the position (Int) of a String and gets it back. I don't know why I get this problem so I hope you can help me. I wrote the code in Eclipse which worked perfectly except for this problem. I also don't know why I can't run this in the code playground because I only used java.util.Scanner and java.util.ArrayList. Thx for any help.

23rd Sep 2018, 8:51 AM
HerrVincling
HerrVincling - avatar
7 ответов
+ 4
HerrVincling Good 👍👍👍 and you are welcome 😉
24th Sep 2018, 3:49 PM
KrOW
KrOW - avatar
+ 3
The problem is more subtile than infinite loop... You have to note than .next and .nextXXX (except .nextLine) methods works in this way: They at first TRY to read delimitator (default at all whitespace chars) if is present, then read the string while another delimitator is found (or end of input) BUT they DONT consume this last delimitator (eg. this last delimitator is keeped in input). Instead .nexLine work differently because it have to read while newline, it read ALL chars in input while newline (or end of input) is found and this can cause some problem (and it consume the newline also opposited to .nextXXX). Suppose than your input is: "Hello \n18\nyears\n" Next suppose than want read all 3 words with a Scanner then you think that this code work like do you want: String hello= inp.nextLine(); int age= inp.nextInt(); String lastWord= inp.nextLine(); well, its not... Get us to understand... First .nextLine read correctly all first line "Hello" and because what i said before about .nextLine all its ok, and now input to read is "18\nyears". Next we read age with .nextInt then scanner read 18 BUT, because my previous explaination about .nextXXX, it keep "\n" in input then input now is "\nyears" in input. At lastWord reading happen the problem. Here we use .nextLine, but it read differently by .next/.nextXXX methods because it have to read while newline (or end input) is encountered. Then it read simply "" (an empty string) keeping "years" in input. Other problems that i see in your code are: - if your input is empty, a call to .nextLine (the first one) return an empty string then you have to handle this case also - At any time can happen error on input (EOF, type mismatch etc) then you have to handle these cases also
23rd Sep 2018, 10:52 AM
KrOW
KrOW - avatar
+ 3
HerrVincling Well done! sorry my answer didn't help, but I tried it before in CPG, and it worked for me : )
24th Sep 2018, 3:52 PM
Ipang
+ 2
I guess you need to add a condition where <schalter> variable can turn to false, otherwise the code will go on running infinitely, running a code that checks for input repetitively in Code Playground is kinda tricky.
23rd Sep 2018, 9:07 AM
Ipang
+ 2
After reading the answer by KrOW a couple of times I now understand it. It's really confusing if you dont know that because the java course here on sololearn didn't mentioned that. I tried the code from Ipang and I'm sorry but your code doesn't fix the/my problem. So I tried it myself and now it's working. Here's my new code for that case: case("Test2") : System.out.println(":"); a = input.nextInt(); b = input.nextLine(); System.out.println(arr.get(e - 1); It works and I don't have to change the rest of my code. Thank you very much for your help solving my problem.
24th Sep 2018, 3:42 PM
HerrVincling
HerrVincling - avatar
+ 1
Thank you for your answers. I'm new to java and English is not my native language so I couldn't understand everything. I know that the .get() method can throw an exception and I can handle the empty String case with the default case right? The case to deactivate the loop exists in my original code but to keep the code short I just uploaded that. The bug is both in my original code and also in my example code so my question is: How do I have to write the code so the .get() method doesn't run the while loop twice? What could work better? Thx for your help.
23rd Sep 2018, 11:08 AM
HerrVincling
HerrVincling - avatar
+ 1
HerrVincling try this, KrOW had given good suggestions, please take note of it. import java.util.Scanner; import java.util.ArrayList; public class Program { public static void main(String[] args) { Scanner input = new Scanner(System.in); ArrayList<String> arr = new ArrayList<String>(); boolean schalter = true; System.out.println("Start"); while(schalter == true) { switch(input.nextLine()) { case ("Test"): String newItem = input.nextLine(); arr.add(newItem); System.out.println("Add '" + newItem + "'\nItems: " + arr.size()); break; case ("Test2"): int index = input.nextInt(); System.out.println("Read item #" + index + " -> " + arr.get(index - 1)); break; case ("q"): schalter = false; } } } }
23rd Sep 2018, 5:01 PM
Ipang