• Post Reply Bookmark Topic Watch Topic
  • New Topic

question about methed ?  RSS feed

 
noha saltan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please I need help

what the method I can use to read int???

because I have assigment but dont know how to solve it

the question is :

Write a Java program to do the following:
a. read 10 integer numbers and store them in an array.
b. find the average of all the numbers stored in the array.
c. print the values of the array and the results of the sum, and average.
d. find the maximum number in this array.

Please help me


int[] MyArry;

int MyArry[] =new int 10;

then how I can read the 10 int ???
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends on where you want to read them from ? Do you ask the user to input 10 numbers, or do you read them from a file ?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

As Satou pointed out above, the answer really depends on what is meant by "reading" these numbers. Judging from the other objectives in this assignment, I think reading them in from another file might be a little advanced. So my guess is that you're either entering the numbers as command line arguments, or asking the user to input these numbers as the program runs. Tell us a little more about how you need to do this, and we'll get you going in the right direction.
 
noha saltan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I want the user to input the intger then store it in array

thanks
 
Glen Bland
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There may be a more direct approach than this. I'd like to know it if there is. but this works:
prompt user to input an integer.
read the next line as a string.
do some error checking on the string to ensure it is all numbers, no letters.
typecast the string to int.
put the int in the next available array slot.
repeat for all 10.

to input from the command prompt as a string check out this very felpul page: http://www.cs.wisc.edu/~cs302/io/JavaIO.html#consoleIN
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, that about sums it up.


do some error checking on the string to ensure it is all numbers, no letters.
typecast the string to int.


You can't typecast a String to an int, but the method that you can use to convert a String to an int also does the error checking at the same time. Look through the JavaDoc for Integer and see if there isn't a method that takes a String argument and returns an int.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Glen Bland:
There may be a more direct approach than this. I'd like to know it if there is...

That's basically the approach.

For reading the input, I suggest using java.util.Scanner, which is a convenient alternative to the older IO classes. You could use Scanner methods like nextInt, but the safer way is probably to use nextLine and then perform some validation on the String before trying to parse it (not cast it) as an int. On the other hand, I think the intent of this assignment is to simply get the input and work with it, so I expect validation is probably beyond what's expected here.
 
Mike Brooks
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like the poster above stated. Scanner class would be good.

I would write a for loop that goes thru MyArray.length times and assign an int from the user. IE. MyArray[count] = scan.nextInt(); and just have count++ in the for loop. Also, in that for loop you can find the average and use an if statement to find and store teh largest number.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!