aspose file tools*
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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
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://aspose.com/file-tools
 
subject: reading user input of numbers into an array