Re: read keyboard input and storing in an array?
- From: "Rhino" <no.offline.contact.please@xxxxxxxxxx>
- Date: Fri, 25 Nov 2005 13:04:17 -0500
<guitarromantic@xxxxxxxxx> wrote in message
news:1132938654.066970.114260@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Hey all,
> New to this, so apologies for the presumably basic question:
>
> I'm trying to store user input in an array, marks. Here's my [cut down]
> code:
>
> public class MarksDatabase
> {
> private int marks[];
>
> public MarksDatabase(String moduleName) {
> int[] marks = new int[200];
> }
>
> public static void readMarks() {
> Scanner keyboard = new Scanner(System.in);
> System.out.println("Enter marks, then put -1 blah blah");
> marks[] = keyboard.nextInt();
> }
>
> I get an error with the keyboard.nextInt part. I've probably
> misunderstood my instructions, but can anybody explain to me where I'm
> going wrong?
>
First of all, it's a lot easier to help with an error situation if you
specify the exact error message you are getting; you have only said that you
get an error but not which one.
Second, your logic seems to trying to read in 200 integers in a single gulp,
200 integers that the user has typed in a single logical line of input.
(Obviously, the input would wrap to new lines as you typed the 200 different
values, followed by the -1 to indicate the end of the input, but Java would
treat it all as one big chunk of input.) That is _not_ what the average
programmer would do.
A common way to process large numbers of input values is in a loop. Each
iteration of the loop would prompt for *one* value, read the value from the
input stream somehow, and store the value. The loop would usually terminate
when it had read all of the data it needed. In your case, you seem to want
exactly 200 values, no more, no less so you could simply execute the loop
exactly 200 times until you had all of the values. However, it would be
better (in my opinion) to make the program more flexible in case you had
situations in which the number of grades to be read was not precisely 200.
You have the beginnings of that logic already since your prompt tells the
user to enter -1 when they have no more data to input. Assuming you want
that flexibility, why don't you try to rewrite the code so that it is
prompting for data in a loop and stops when the value -1 is supplied?
That causes one complication though: if the user ever wants to input more
than 200 values, the program will have an error when it tries to store the
201st value since the array only has room for 200 values. You can get around
that in a few ways:
- make the array big enough to hold the largest conceivable number of values
that you can ever imagine anyone giving your program. Perhaps that is 250
values, perhaps it is 50 million. (I don't recall if there is a maximum size
for an array so it is possible that you could pick a size that is too big to
compile but if that happens, there should be a clear compile message telling
you what happened. If you get that message, just use a smaller size for the
array to prevent the error message.)
- use a structure that is not fixed in size, like a Vector, instead of an
int array. This will force you to store the int values as Integer ('Integer'
is a wrapper class. A wrapper class enables you to store primitives like
ints in Objects which can then be stored in structures like Vectors or
collections.) You will also need to convert the Integer back to int when you
get it back out of the Vector. The good news is that converting from int to
Integer and back again is very easy.
- write the loop so that it stops when the input is -1 _OR_ you are at the
maximum size of the array. That way, you can store a very small number of
values or any number of values up tol 200 but no more. That is probably the
solution I would suggest at your stage with Java; it should make the program
sufficiently flexible to impress your teacher slightly.
One other thing you need to consider is the type of data that
keyboard.nextInt() returns to your program. I think someone posted a code
fragment using the Scanner class before (you? one of your classmates?); as I
recall it is not a class from the standard Java API. That is probably okay
for teaching purposes, although I frown on showing students non-standard
classes, but it means that you need to find the API that your teacher is
using and find out what keyboard.nextInt() is returning. From the method
name, I assume it is an int but you should make sure of that. If it isn't
returning an int, you will need to convert whatever it gives you into an int
so that you can store it in the array. (Or change the array so that it
contains whatever keyboard.getInt() returns!) Otherwise, you'll get an error
if you try to shove a non-int into an int array.
Why don't you try changing the code along the lines I've suggested and see
how it goes. If you have problems, post back to the same thread and someone
here will probably help you.
The third and final thing I'm going to say is probably too advanced for you
to appreciate at this stage and I'm not suggesting you act on it. I'm simply
mentioning it to make you think about design issues. That is a good practice
to get into for when you are writing "real" programs.
Basically, I don't think you'd ever see a professional program prompt a user
for 200 different values from a command line. Not even one value at a time,
let alone 200 in one gulp! If I were writing a professional program and
wanted to get that much data from someone, I'd probably use a GUI so that I
could give them an input form of some kind for the data. That might mean
writing a GUI in a Java application or applet or it might mean using an HTML
form in a servlet. Other possibilities also exist, like Struts or other
"frameworks" that are intended for this purpose.
The other common approach to this situation would be for the marks to be
written to a file or database via some process or another; then your program
would simply read the file or database without having to prompt the user for
anything. Of course, the process that wrote the file in the first place
would probably prompt the user for the marks somewhere along the line but
that might not be _your_ problem in the real world :-)
As your course proceeds, you may learn something about these professional
techniques; if not, maybe a future course or some self-study on your part
will show you these approaches. But don't sweat this too much right now; you
are still learning basics and you can learn these other techniques when the
time comes.
But it's never too early to start thinking like a professional designer,
even if you can't build professional quality components yet :-)
Rhino
.
- Follow-Ups:
- Re: read keyboard input and storing in an array?
- From: guitarromantic@xxxxxxxxx
- Re: read keyboard input and storing in an array?
- References:
- read keyboard input and storing in an array?
- From: guitarromantic@xxxxxxxxx
- read keyboard input and storing in an array?
- Prev by Date: Re: OO Factory
- Next by Date: Re: Advice on best objects to use
- Previous by thread: read keyboard input and storing in an array?
- Next by thread: Re: read keyboard input and storing in an array?
- Index(es):
Relevant Pages
|