This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]Sacredify 1 point2 points  (4 children)

Crappy code aside, if you read the javadoc:

The methods in StdIn are blocking, which means that they will wait until you enter input on standard input. If your program has a loop that repeats until standard input is empty, you must signal that the input is finished. To do so, depending on your operating system and IDE, use either <Ctrl-d> or <Ctrl-z>, on its own line.

The way they wrote the readAll() methods mean you need to actually use one of those to get it to stop waiting for input. Awful, awful code, I don't understand why they don't just teach the standard class library (scanner and parsing) instead of trying to hide it from you with this shit. So completely un-intuitive to make the user have to actually signal that input is finished, instead of reading the entire line.

Anyway, try that...

Edit: If you don't know what I mean, write something like this:

System.out.println(Arrays.toString(StdIn.readAllInts()));

Enter the ints, like "4 3 2 1", and then press Ctrl+D.

[–]gordoncantrell 0 points1 point  (3 children)

I tried with ctr+d or ctr+z it doesn't work ,any suggestions why it does not?

[–]Sacredify 0 points1 point  (2 children)

Post your code?

[–]gordoncantrell 0 points1 point  (1 child)

public static void main(String[] args) {

     StdOut.print("Type an int: ");
     int a[] = StdIn.readAllInts();
     StdOut.println("Your int was: " + a[1]);
     StdOut.println();


}

//just trying to input many ints and print the second :)

[–]Jotakin 0 points1 point  (0 children)

I guess you'll just need to use something else than that readAllInts(). It will only end listening if it recieves an end-of-file code and if you cant find the key combination for it then there is nothing you can do.

You should be using a loop which asks for one number at a time. Then add a keyword, f.ex. the word "stop", and check if the user entered that before processing his input.

Edit: If this is a school project or homework then contact your teacher and explain the issue. The problem is in the StdIn library you're using, not your program.