Results 1 to 13 of 13
- 07-08-2010, 05:47 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 3
- Rep Power
- 0
Read user input into integer array
I am trying to get make a java program which takes integers from user & then store into an integer array of length "size". The only problem is that I am getting exceptions when I am about to enter numbers from console. I am using Eclipse IDE & below is my code:
import java.io.*;
import java.util.Scanner;
import java.util.*;
public class BetterProgrammerTask {
public static void main(String[] args) throws IOException {
int size = 0;
System.out.println("Enter the size of the array: ");
Scanner in = new Scanner(System.in);
size = in.nextInt();
in.close();
System.out.println("The size you enetered is " + size);
int[] a = new int[size];
Scanner in1 = new Scanner(System.in);
System.out.println("Enter the array: ");
int j=0;
while(j<size)
{
a[j]=in1.nextInt();
++j;
}
in1.close();
Arrays.sort(a);
System.out.println("The sorted array is ");
for (int i=0; i<size; i++)
{
System.out.println(a[i]);
}
}
}
Below is the exception message:
Enter the size of the array:
11
The size you enetered is 11
Enter the array:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at BetterProgrammerTask.main(BetterProgrammerTask.jav a:33)Last edited by varunb; 07-08-2010 at 05:51 PM.
- 07-08-2010, 06:26 PM #2
Get rid of the second Scanner object.
Java Code:Scanner in = new Scanner(System.in); size = in.nextInt(); System.out.println("The size you enetered is " + size); int[] a = new int[size]; System.out.println("Enter the array: "); int j = 0; while (j < size) { System.out.print("Enter "+ (j+1) + ": "); a[j] = in.nextInt(); ++j; } in.close();Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 07-08-2010, 07:07 PM #3
can you explain to the OP what happened and why he got the exception?
- 07-08-2010, 07:13 PM #4
I can only guess, as I'm not familiar with Scanner, but I reckon it boils down to the inputStream still holding the '\n' from the first nextInt() call, but please correct me if I'm wrong.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 07-08-2010, 07:20 PM #5
If the OP would read the API doc for the Scanner nextInt() method it is explained there.java.util.NoSuchElementException
After he understands why he gets the error then he needs to look at other Scanner methods to see how he can avoid this error.
- 07-08-2010, 07:38 PM #6
Ok, I've read it and I don't get it. Might be a language thing, but why is the input exhausted?
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 07-08-2010, 07:43 PM #7
I think exhausted here means that there is nothing more there.
He needs to test if anything is there by using a hasNext method.
The problem with that is that it will block waiting for something. A bit confusing to me.
hasNext() shouldn't block. If there isn't anything return false. I guess it wants to see what the next thing is and test it to be an int (or whatever).
There might be a method to call to test if the input stream is empty.
- 07-08-2010, 07:53 PM #8
That's what bugs me, if there is nothing more, it should wait for an input, no?
Scanner1 has read an input and is closed, fine. Scanner2 should start from scratch, but it doesn't.
The intro chapter in the API doesn't help much either leave aloe the Scanning tutorial in I/O.Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 07-08-2010, 08:24 PM #9
I can't figure out how you can use Scanner. There appears no way to call a hasNext method without it possibly blocking.
I can understand the a call to a next method could fail if there isn't anything in the input.
Here's an attempt not to block on Scanner having nothing to read.The main code is from another forum thread also having Scanner problems.
Java Code:import java.util.ArrayList; import java.util.Scanner; // Check if the scanner has any input available // Problem because Scanner.hasNext() will block!! class CheckScanner { static boolean hasNext(final Scanner sc) { Thread t = new Thread(new Runnable() { public void run() { sc.hasNext(); // this could block System.out.println("hasNext returned"); } }); t.start(); try{Thread.sleep(10);}catch(Exception x){}; // is this long enough? //If thread still alive, then it must be blocked if(t.isAlive()) { t.interrupt(); // What does this do for thread blocked on I/O System.out.println("interrupt called"); return false; }else{ return true; // If thread has exited, then hasNext() returned } } // end hasNext() } // end class public class HangingScanner { /** Main method */ public static void main(String[] args) { ArrayList<Integer> numbers = new ArrayList<Integer>(); // Create a Scanner Scanner input = new Scanner(System.in); System.out.println("hasNext=" + CheckScanner.hasNext(input)); //<<<<<<<<<<<<<<< // Read all numbers while(input.hasNextInt()){ System.out.print("Enter a number: "); // Convert string into integer int nxt = input.nextInt(); System.out.println(" got " + nxt); numbers.add(nxt); } System.out.print("numbers=" + numbers); } }
- 07-08-2010, 08:37 PM #10
Got it! I did a search in my backyard:
Java Programming - beginers question about taking input from console
the in.close() closes System.in, that's why the next scanner object fails, duh.
If this scanner has not yet been closed then if its underlying readable also implements the Closeable interface then the readable's close method will be invoked. If this scanner is already closed then invoking this method will have no effect.Last edited by PhHein; 07-08-2010 at 08:40 PM.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 07-08-2010, 08:58 PM #11
Thanks for that.
- 07-08-2010, 09:04 PM #12
You're welcome, now we only need the OP to come back and to understand.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 07-09-2010, 12:50 PM #13
Member
- Join Date
- Jul 2007
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Testing if an input is an integer
By Mayday in forum New To JavaReplies: 1Last Post: 05-08-2010, 05:14 PM -
need help with two dimensional array and setting it up using user input
By Peanuts1 in forum New To JavaReplies: 5Last Post: 11-26-2009, 07:01 PM -
how to know the input value of integer
By ran830421 in forum New To JavaReplies: 15Last Post: 11-18-2009, 09:01 PM -
input to an integer (simply)
By chitwood in forum Advanced JavaReplies: 3Last Post: 03-18-2009, 06:34 AM -
Can't get my "if" statement to read user input
By daletron3030 in forum New To JavaReplies: 7Last Post: 01-16-2009, 05:24 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks