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]

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.
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.
> 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.