Results 1 to 2 of 2
Thread: Scanner input into array
- 12-09-2011, 04:38 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 18
- Rep Power
- 0
Scanner input into array
I have inputs such as "1 2 3" being inputted into my program.
I am using Scanner to look for the numbers. I would like to put these numbers into an array, but how would I initialize the array size if the inputs vary in size? For example, what if the input has more than 3 numbers?
Here is the code I am working with:
Java Code:Scanner scan = new Scanner(System.in); int [] array = new int [3]; for (int i = 0; i < array.length; i++) { array[i] = scan.nextInt(); } for (int z = 0; z< array.length; z++) { System.out.print(array[z] + " "); }
- 12-09-2011, 04:57 AM #2
Re: Scanner input into array
3 options:
1 - ask user how many numbers they are entering and then create an array with that size.
2 - create an array of some length. Keep track of how many numbers have been entered. When the array is full create a new bigger array. Copy values from old array to new array. Insert new value.
3 - use a List.
Similar Threads
-
Converting Scanner input to JOptionPane
By Mideoan in forum New To JavaReplies: 1Last Post: 03-09-2011, 11:26 PM -
End Scanner int input with text value
By BillyB in forum New To JavaReplies: 3Last Post: 03-07-2011, 05:13 PM -
how to reverse some input from scanner
By moncur in forum New To JavaReplies: 24Last Post: 11-09-2010, 02:59 PM -
Changing Scanner input type
By mlad in forum New To JavaReplies: 13Last Post: 03-18-2010, 04:22 PM -
Scanner input problem
By slayer_azure in forum New To JavaReplies: 3Last Post: 05-26-2008, 10:49 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks