Results 1 to 8 of 8
  1. #1
    DMarsh12 is offline Member
    Join Date
    Mar 2011
    Posts
    43
    Rep Power
    0

    Default Array Scanning from Text help

    Hello everyone, I have posted a few days ago about making an array input from a text file and everyone had done an excellent job in pointing me in the right direction. At last I felt I have figured it all out but unfortunately I'm getting an error :/

    Here is the code for the array:


    Java Code:
    	private Scanner inputStream;
    	
    	public void openFile()
    	{
    		 File textFile = new File("C:\\Users\\Dan\\Desktop\\studentId.txt"); //locates the textfile location
    
    		    inputStream = null; //creates a scanner to read the file
    
    		    try //looks for the file
    		    {
    		        inputStream = new Scanner(textFile);
    		    }
    		    catch (FileNotFoundException e) //if the file isn't located
    		    {
    		        System.out.println("File not found");
    		    }
    	}
    	
    
    public void sortFile()
    	{
    		
    		System.out.println("The sorted file is as follows:");
    
    				int ID=inputStream.nextInt();
    				int IDarray[]=new int[9];
    				for(int i=0; i<10; i++)
    				{
    					IDarray[i]=inputStream.nextInt();
    					System.out.println(IDarray[i]);
    				}
    to my understanding, in my sortFile method the operations are as follows;

    i have an int called ID which takes the input from my inputStream scanner and assigns those variables to it.
    the array I made was created with a value of 10 (i have 10 IDs)
    so i made a loop up to 9 which assigns the value of i to whatever value the inputStream comes in as. For example if the numbers taken in are 1 2 3 4 then i[0] should be 1, i[1] should be 2 etc... for some reason it's printing out this error:


    Exception in thread "main" java.util.InputMismatchException
    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 readFile.sortFile(readFile.java:53)
    at mainExecution.main(mainExecution.java:17)

    I feel like I'm so close so any other nudges in the right direction would be much appreciated!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Array Scanning from Text help

    Exception in thread "main" java.util.InputMismatchException
    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 readFile.sortFile(readFile.java:53)
    The nextInt() method called on line 53 expects to read an int value but is finding some other type of data.

    Some solutions:
    don't use nextInt() to read non int data
    Test that the next item is an int by using the hasNextInt() method.
    Make sure the data is an integer
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    DMarsh12 is offline Member
    Join Date
    Mar 2011
    Posts
    43
    Rep Power
    0

    Default Re: Array Scanning from Text help

    Quote Originally Posted by Norm View Post
    The nextInt() method called on line 53 expects to read an int value but is finding some other type of data.

    Some solutions:
    don't use nextInt() to read non int data
    Test that the next item is an int by using the hasNextInt() method.
    Make sure the data is an integer
    Thanks for the tip! I got rid of the errors but for some reason it's not printing out any numbers...any idea why?

    Here's what I did:

    Java Code:
    				int ID=inputStream.nextInt();
    				int IDarray[]=new int[9];
    				while (inputStream.hasNextInt()==true)
    				{
    					for(int i=0; i<10; i++)
    					{
    					IDarray[i]=inputStream.nextInt();
    					System.out.println(IDarray[i]);
    					}
    				}

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Array Scanning from Text help

    it's not printing out any numbers
    Does the hasNextInt() method ever return true? If not, the while() loop won't be executed.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    DMarsh12 is offline Member
    Join Date
    Mar 2011
    Posts
    43
    Rep Power
    0

    Default Re: Array Scanning from Text help

    Quote Originally Posted by Norm View Post
    Does the hasNextInt() method ever return true? If not, the while() loop won't be executed.
    Ok so you have been EXTREMELY helpful so far and I'm just about there...

    Here is what I have:

    Java Code:
    int ID=inputStream.nextInt();
    				int IDarray[]=new int[10];
    				for(int i=0; i<10; i++)
    					{
    					IDarray[i]=ID;
    					System.out.println(IDarray[i]);
    					}
    its printing out just the first ID number for all ten iterations... so my question is how do I make a loop to store all the values of the inputStream into the variable ID?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Array Scanning from Text help

    store all the values of the inputStream into the variable ID?
    A variable can only hold one value, the last one assigned to it.

    The code in post#3 stored the values it read as input into an array. I think your problem is that the data being read is not integers.
    For debugging to see what is happening: Try using the next() method to read the data and print it out to see what is being read.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    DMarsh12 is offline Member
    Join Date
    Mar 2011
    Posts
    43
    Rep Power
    0

    Default Re: Array Scanning from Text help

    Quote Originally Posted by Norm View Post
    A variable can only hold one value, the last one assigned to it.

    The code in post#3 stored the values it read as input into an array. I think your problem is that the data being read is not integers.
    For debugging to see what is happening: Try using the next() method to read the data and print it out to see what is being read.
    wow, I was building my whole project around this premise...back to the drawing board! Although I must change everything I think I have found a solution... thank you so so so much!

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Array Scanning from Text help

    Glad you are making progress. I find that it's sometimes necessary to add some printlns to code to see what it is doing and that often shows some bad assumptions.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Scanning a txt file with Java?
    By maskanio in forum New To Java
    Replies: 1
    Last Post: 11-01-2012, 11:53 PM
  2. Replies: 15
    Last Post: 05-14-2012, 01:42 PM
  3. Scanning a Plain text file
    By aw249 in forum New To Java
    Replies: 3
    Last Post: 02-03-2012, 06:16 PM
  4. Scanning text file and inserting contents into array
    By jmwalloh in forum New To Java
    Replies: 8
    Last Post: 03-24-2010, 01:33 PM
  5. Scanning in java
    By Waqas in forum Advanced Java
    Replies: 1
    Last Post: 07-10-2009, 09:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •