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


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

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

From Chad <cdalten@gmail.com>
Newsgroups comp.lang.java.programmer
Subject Re: How can I read from the entire standard input at once?
Date 2011-09-27 20:47 -0700
Organization http://groups.google.com
Message-ID <e0ce6796-7139-4900-b511-ca70ffe5b75c@m15g2000vbk.googlegroups.com> (permalink)
References j5u1f4$pds$1@dont-email.me

Show all headers | View raw


On Sep 27, 7:39 pm, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
> 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]);
> >                            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.
>
> --

Disregard the original post.

Chad

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


Thread

Re: How can I read from the entire standard input at once? Chad <cdalten@gmail.com> - 2011-09-27 20:47 -0700
  Re: How can I read from the entire standard input at once? Lars Enderin <lars.enderin@telia.com> - 2011-09-28 09:57 +0200
  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

csiph-web