Re: read keyboard input and storing in an array?
- From: "Rhino" <no.offline.contact.please@xxxxxxxxxx>
- Date: Sun, 27 Nov 2005 19:49:20 -0500
<guitarromantic@xxxxxxxxx> wrote in message
news:1133048220.242541.274160@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Thanks for the very informative post, it was worth waiting for (I
> didn't see this message appear for a couple of days, oddly enough..).
>
> I should have been a little more diligent in my post: I didn't intend
> to let the user enter 200 values, haha. The spec for our work said
> allow up to 200 values to be entered, and I fully intend to do this in
> a loop, where the user can cancel it at any time by entering -1 (as the
> spec demands). Call my sloppy code above the product of being
> frustrated..
>
> As for the Scanner class, it probably was me posting it. That's what
> we're being told to use, so I have to stick with it.
>
> So I return to my original problem. When in a loop, how do I simply
> tell Java to store the next user-inputted integer in value [x] of an
> array?
>
That's not the easiest question to answer since I don't have access to the
API for your Scanner class. I *think* Scanner is probably just your
instructor's way of avoiding this, which is what I would use in my own
programs:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Then, the BufferedReader instance, br, would do this to read in a line of
text:
String input = br.readLine();
The data returned by 'br.readLine()' would always be a String so I've stored
it in a String called 'input'. Since I want to store an integer, I would
simply convert it to an int as follows:
int myInt = Integer.parseInt(input);
Then, you could copy 'myInt' to your array. (Of course, you could combine
several steps at once into a single, more complex line of code so that the
value obtained by readLine() was immediately converted from String to int
and then stored in the array.
But you don't use the standard techniques: instead, you teacher is showing
you homemade techniques that you won't see in the real world. He/she is
definitely not doing you a favour, at least in my opinon.
For your situation, the code should be pretty much what you already have:
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter an integer test score or -1 to quit.");
int ix = 4;
marks[ix] = keyboard.nextInt();
All I really added was the index within the square brackets on the fourth
line so that Java knows which entry of the array to use for your input. Of
course, when you write your loop, you will not simply assign '4' to 'ix';
you will set 'ix' equal to the value of your loop counter so that it
increments on each time through the loop.
Try coding it along the lines I've suggested and post again if you have
problems. But be sure to include the actual error message you are getting,
not just the fact that you are getting an error. You still haven't said what
error you are getting so I'm going to assume you got a compile error on the
'marks[] = keyboard.nextInt();' line and the message said something about
invalid syntax.
Rhino
.
- Follow-Ups:
- Re: read keyboard input and storing in an array?
- From: Roedy Green
- Re: read keyboard input and storing in an array?
- From: blmblm
- Re: read keyboard input and storing in an array?
- References:
- read keyboard input and storing in an array?
- From: guitarromantic@xxxxxxxxx
- Re: read keyboard input and storing in an array?
- From: Rhino
- Re: read keyboard input and storing in an array?
- From: guitarromantic@xxxxxxxxx
- read keyboard input and storing in an array?
- Prev by Date: Help with an Array Assignment
- Next by Date: Re: cant's compile with javac
- Previous by thread: Re: read keyboard input and storing in an array?
- Next by thread: Re: read keyboard input and storing in an array?
- Index(es):
Relevant Pages
|