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

Discussion in 'Java' started by Chad, Sep 28, 2011.

  1. Chad

    Chad Guest

    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++;
    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()
    Chad, Sep 28, 2011
    #1
    1. Advertising

  2. Chad

    Eric Sosman Guest

    On 9/27/2011 9:34 PM, Chad wrote:
    > 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


    Is this a serious question, or are you trolling? Benefit of
    the doubt ...

    > 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;
    > }


    A few observations about this method. First, every time it's
    called it creates a brand-new Scanner reading System.in. If you
    call it twelve times you've got twelve Scanners all fighting
    over the same input source. I'm too revolted by the idea to spend
    time figuring out how the Scanners resolve their squabbles; it's
    like asking whether the tapeworms are more likely to exit the
    terminus of your gut head- or tail-first. I prefer not to know.

    Second, and take note of this in relation to the subsequent
    switch statement, this method returns either 0 or -1. Nothing else,
    not ever.

    Third, the getNextNumber name is not at all descriptive of what
    the method does. You might as well have called it getLost.

    > public static void main(String[] args) {
    > String[] values = new String[100];
    >
    > char[] getNumber;
    > int value;
    > int i = 0;
    > while ( ( value = getNextNumber(values) ) != -1) {


    Since the method returns either 0 or -1, we know that if the
    program reaches this point we have `value' equal to 0.

    > getNumber = values[0].toCharArray();


    Why? Have you never heard of String's charAt method?

    > switch (value) {
    > case 0: {
    > System.out.println("The value is " + getNumber);
    > i++;
    > break;


    The switch statement selects this case every time. The first time
    around, it prints the first character (if there is one) of the first
    String (if there is one) read by the first Scanner, and ignores all the
    rest of the first String and any additional Strings the Scanner may
    have produced. The second time, it prints the second character (if there
    is one) of the first String (if there is one) read by the second Scanner
    (if it's able to read at all), ignoring all the rest of the input. The
    third time it outputs the third character of the third Scanner's first
    String, then the fourth time, ...

    > }


    Since `value' is zero, the remainder of the switch is irrelevant:
    None of it can ever be executed.

    > 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()


    The more I think about it, the more I think giving you the benefit
    of the doubt may have been foolhardy. Eleven to two you're trolling.

    --
    Eric Sosman
    d
    Eric Sosman, Sep 28, 2011
    #2
    1. Advertising

  3. Chad

    Roedy Green Guest

    On Tue, 27 Sep 2011 22:39:07 -0400, Eric Sosman
    <> wrote, quoted or indirectly quoted
    someone who said :

    > or are you trolling?


    what made you suspect that?
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    It should not be considered an error when the user starts something
    already started or stops something already stopped. This applies
    to browsers, services, editors... It is inexcusable to
    punish the user by requiring some elaborate sequence to atone,
    e.g. open the task editor, find and kill some processes.
    Roedy Green, Sep 28, 2011
    #3
  4. Chad

    Eric Sosman Guest

    On 9/28/2011 12:25 PM, Roedy Green wrote:
    > On Tue, 27 Sep 2011 22:39:07 -0400, Eric Sosman
    > <> wrote, quoted or indirectly quoted
    > someone who said :
    >
    >> or are you trolling?

    >
    > what made you suspect that?


    "Chad" has been around on this and other forums for several
    years. If s/he has that much experience with programming -- even if
    all s/he's done is Turtle Graphics -- the depth of incomprehension
    evidenced by the offered code is beyond my ability to accept at face
    value.

    Of course, it might be a new "Chad." Hence the benefit of the
    doubt, and the answer pointing out (some of) the problems.

    --
    Eric Sosman
    d
    Eric Sosman, Sep 29, 2011
    #4
  5. Chad

    Chad Guest

    On Sep 28, 5:58 pm, Eric Sosman <> wrote:
    > On 9/28/2011 12:25 PM, Roedy Green wrote:
    >
    > > On Tue, 27 Sep 2011 22:39:07 -0400, Eric Sosman
    > > <>  wrote, quoted or indirectly quoted
    > > someone who said :

    >
    > >> or are you trolling?

    >
    > > what made you suspect that?

    >
    >      "Chad" has been around on this and other forums for several
    > years.  If s/he has that much experience with programming -- even if
    > all s/he's done is Turtle Graphics -- the depth of incomprehension
    > evidenced by the offered code is beyond my ability to accept at face
    > value.
    >
    >      Of course, it might be a new "Chad."  Hence the benefit of the
    > doubt, and the answer pointing out (some of) the problems.
    >


    Eric beat me to the explanation. But there is a little bit more to the
    story. Over the years both me and Eric have gone back on forth on
    comp.lang.c, comp.unix.programmer, and a few other places. Anyways,
    I've gone on record several times as saying that I'm "bit slow". In
    real life I'm just some seasonal blue collar factory worker. Usually I
    just sit around the house and drink after they lay me off for the
    season.

    However, maybe like a year ago, I started to take some programming
    classes at the local extension. It was either that, or spend another
    year drinking myself sober. Now the question I posted in here is
    actually part of a school project.

    Chad
    Chad, Sep 29, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sjaakie Helderhorst

    Postback and process entire datagrid at once

    Sjaakie Helderhorst, Jun 3, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    306
    Sjaakie Helderhorst
    Jun 3, 2004
  2. MRW

    Displaying Entire Page at once

    MRW, Dec 10, 2006, in forum: ASP .Net
    Replies:
    6
    Views:
    287
    Gabriel Lozano-Morán
    Dec 11, 2006
  3. Adam Funk
    Replies:
    4
    Views:
    416
    Adam Funk
    Dec 21, 2007
  4. PerlFAQ Server
    Replies:
    0
    Views:
    91
    PerlFAQ Server
    Jan 26, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    100
    PerlFAQ Server
    Mar 16, 2011
Loading...

Share This Page