why would a print statement for debugging fix the problem?

Here is a quick newbie program that I am trying to make that will read in arguments from the command line and assign them to the appropriate arrayList (int, double, or string). It should stop when I type quit and then print out the contents of each arrayList. It works fine when I have the debugging print statement "why should this change anything" in the else clause where I read in the string, but doesn't work otherwise. (typing quit won't stop it and print the values). It seems that the for loop inside the while statement (also for debugging) is also essential to making the program work as intended. Anyone know how this could happen? Here is the code:

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

ArrayList<Integer> intList = new ArrayList<Integer>();

ArrayList<Double> doubleList = new ArrayList<Double>();

ArrayList<String> stringList = new ArrayList<String>();

int i;

while(scanner.hasNext("quit") == false) {

if(scanner.hasNextInt())

{

intList.add(new Integer(scanner.nextInt()));

scanner.nextLine();

}

else if(scanner.hasNextDouble())

{

doubleList.add(new Double(scanner.nextDouble()));

scanner.nextLine();

}

else

{

stringList.add(scanner.nextLine());

System.out.println("why does this change things");

}

for (i = 0; i < intList.size(); i++)

{

System.out.println((int)intList.get(i));

}

}

for (i = 0; i < intList.size(); i++)

{

System.out.println(intList.get(i));

}

for (i = 0; i < doubleList.size(); i++)

{

System.out.println(doubleList.get(i));

}

for (i = 0; i < stringList.size(); i++)

{

System.out.println(stringList.get(i));

}

}

}

Message was edited by:

Nairbog

[1934 byte] By [Nairboga] at [2007-11-26 17:09:09]
# 1

Works fine for me, with or with out that println(). Maybe the fact that you print out the int list every time you get input is confusing things? Take out this:

for (i = 0; i < intList.size(); i++)

{

System.out.println((int)intList.get(i));

}

inside the while loop.

hunter9000a at 2007-7-8 23:36:57 > top of Java-index,Java Essentials,New To Java...
# 2

Nope that's not it, if I take that out the only way the program will work is if I only type in strings.

Wait a minute, so after taking out the debugging println statements the program works as intended for you? If so, something is wrong with my IDE maybe? I'm using Jcreator.

Message was edited by:

Nairbog

Nairboga at 2007-7-8 23:36:58 > top of Java-index,Java Essentials,New To Java...
# 3

For me to it worked fine. I used this code

import java.util.*;

public class Test

{

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

ArrayList<Integer> intList = new ArrayList<Integer>();

ArrayList<Double> doubleList = new ArrayList<Double>();

ArrayList<String> stringList = new ArrayList<String>();

int i;

while(scanner.hasNext("quit") == false)

{

if(scanner.hasNextInt())

{

intList.add(new Integer(scanner.nextInt()));

scanner.nextLine();

}

else if(scanner.hasNextDouble())

{

doubleList.add(new Double(scanner.nextDouble()));

scanner.nextLine();

}

else

{

stringList.add(scanner.nextLine());

//System.out.println("why does this change things");

}

for (i = 0; i < intList.size(); i++)

{

System.out.println((int)intList.get(i));

}

}

for (i = 0; i < intList.size(); i++)

{

System.out.println(intList.get(i));

}

for (i = 0; i < doubleList.size(); i++)

{

System.out.println(doubleList.get(i));

}

for (i = 0; i < stringList.size(); i++)

{

System.out.println(stringList.get(i));

}

}

}

When posting code use the code tags as described here.

http://forum.java.sun.com/help.jspa?sec=formatting

As mention remove the System.out, it serves no purpose unless you want conformation everytime. The code works regardless whether you use it or not though.

Explain your problem further if this is not what you meant.

kikemellya at 2007-7-8 23:36:58 > top of Java-index,Java Essentials,New To Java...
# 4

> If so, something is wrong with my IDE maybe? I'm

> using Jcreator.

That's probably it, using command line input in IDEs can be a pain in the ***. Just run it from the command line and it should work. If you don't know how => http://java.sun.com/docs/books/tutorial/getStarted/cupojava/win32.html

hunter9000a at 2007-7-8 23:36:58 > top of Java-index,Java Essentials,New To Java...
# 5

Just wanted to add some more info on the problem, I realized that if I put a println statement within every evaluation clause (if/else statements) then the program works. Additionally, the program will work for any combination of input if I just put a println statement outside the if/else statements but inside the while loop.

Nairboga at 2007-7-8 23:36:58 > top of Java-index,Java Essentials,New To Java...
# 6
BTW the println has nothing to do with whether this program works or not. println is simply confirming your input, thats all. it adds nothing extra
kikemellya at 2007-7-8 23:36:58 > top of Java-index,Java Essentials,New To Java...
# 7

I have written many other programs in JCreator without problems, and would like to continue to run and test them inside Jcreator, so I would be very thankful if anyone could identify why the println statement inside the while loop but outside the if/else clauses fixes it completely.

I realize that it shouldn't fix the program, but it does. If I take out the println statement it ceases to work, if I add it back in, it works perfectly.

Nairboga at 2007-7-8 23:36:58 > top of Java-index,Java Essentials,New To Java...
# 8

> I realize that it shouldn't fix the program, but it

> does. If I take out the println statement it ceases

> to work, if I add it back in, it works perfectly.

Then the problem isn't with the code, it's with how you're executing it. Do what I said in reply 4 and tell me if it works correctly without the printlns.

hunter9000a at 2007-7-8 23:36:58 > top of Java-index,Java Essentials,New To Java...
# 9
thanks hunter, it did work in the command prompt just fine without the print statements. Anyone familiar with jcreator that might know what is causing the problem?
Nairboga at 2007-7-8 23:36:58 > top of Java-index,Java Essentials,New To Java...