This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes reading user input of numbers into an array Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "reading user input of numbers into an array" Watch "reading user input of numbers into an array" New topic
Author

reading user input of numbers into an array

Lesa
Greenhorn

Joined: Feb 27, 2000
Posts: 4
Can anyone please tell me how to read a series of numbers in from the keyboard and put them into an array of integers for manipulation?
------------------
Eric Edwards
Ranch Hand

Joined: Feb 12, 2000
Posts: 60
Originally posted by Lesa:
Can anyone please tell me how to read a series of numbers in from the keyboard and put them into an array of integers for manipulation?

I did this in a hurry, but it works. It reads in ten numbers
and puts each number in an array. At the end it prints out the array. You can adjust it any way you want.
import java.io.*;
import java.text.*;
public class ReadNum
{ public static void main(String[] args)
{
int[] num = new int[10];// set up array of 10
for (int i = 0; i < 10; i++) // read in 10 numbers
{
System.out.println
("Enter a number."); // get a number
int x; // number you entered
try
{ InputStreamReader isr
= new InputStreamReader(System.in);
BufferedReader br
= new BufferedReader(isr);
String s = br.readLine();
x = Integer.parseInt(s);
num[i] = x; // put the number in the array
}
catch(IOException e)
{ x = 0;
}
}
for (int i = 0; i < 10; i++)
{
System.out.println(num[i]); // print out the array
}
}
}

[This message has been edited by Eric Edwards (edited February 27, 2000).]
 
I agree. Here's the link: http://jrebel.com/download
 
subject: reading user input of numbers into an array
 
Similar Threads
dESCENDING ORDER
Confuse the findByCriteria()
K&B, Chapter 3: Assignments, page 224
Array of ten numbers
help anda what is the package for the sort()