Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #8377

How can I read from the entire standard input at once?

From Chad <cdalten@gmail.com>
Newsgroups comp.lang.java.programmer
Subject How can I read from the entire standard input at once?
Date 2011-09-27 18:34 -0700
Organization http://groups.google.com
Message-ID <bbb89d6d-807d-4fbb-8c8d-9c060913cdb1@g23g2000vbz.googlegroups.com> (permalink)

Show all headers | View raw


The following code is a stripped down version of a large project. The
question is why do I have to press ctrl z every time to get the
output? That is, say I type the following at the command line

1 - 3

and then press the enter key on my keyboard. I press ctrl z and I get
'The value is 1' . Then I press it again, I get a 'The value is '.
Then I press it again, I get 'The value is -', etc.  Ideas on how to
get how get the entire output out at once? Ideally what I would like
to do is when I type in the above, press the enter key, then, after I
press ctrl z,  get

The value is 1
The value is
found the minus sign
The value is 3


import java.util.Scanner;

public class hack {

    public static int getNextNumber(String [] storeValues) {
        String value = null;
        int i = 0;
        Scanner input = new Scanner(System.in);

        while(input.hasNextLine()) {

            try {
                storeValues[i++] = input.nextLine();
            } catch (IllegalStateException e) {
		return -1; //EOF
            }
        }

        return 0;
    }

    public static void main(String[] args) {
        String[] values = new String[100];

	char[] getNumber;
	int value;
	int i = 0;
        while ( ( value = getNextNumber(values) ) != -1) {
		getNumber = values[0].toCharArray();
		switch (value) {
			case 0: {
				System.out.println("The value is " + getNumber[i]);
				i++;
				break;
			}
			case '+': {
				System.out.println("found plus sign");
				i++;
				break;
			}
			case '-': {
				System.out.println("found minus sign");
				i++;
				break;
			}
			case '*': {
				System.out.println("found mult sign");
				i++;
				break;
			}
			case '/': {
				System.out.println("found division sign");
				i++;
				break;
			}
			default: break;
		}
	}//end while

    }//end main()

Back to comp.lang.java.programmer | Previous | NextNext in thread | Find similar


Thread

How can I read from the entire standard input at once? Chad <cdalten@gmail.com> - 2011-09-27 18:34 -0700
  Re: How can I read from the entire standard input at once? Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-27 22:39 -0400
    Re: How can I read from the entire standard input at once? Roedy Green <see_website@mindprod.com.invalid> - 2011-09-28 09:25 -0700
      Re: How can I read from the entire standard input at once? Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-28 20:58 -0400
        Re: How can I read from the entire standard input at once? Chad <cdalten@gmail.com> - 2011-09-28 18:36 -0700

csiph-web