Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 50
  1. #21
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Reading From File Question??

    Cinsider to avoid this:

    Java Code:
                while(inFile.hasNext()) {
                    counter++;
                    inFile.next();
                }

    and this:
    Java Code:
      for(int i=0;i<counter;i++)   {...
    and use simple this:

    Java Code:
            while (inFile.hasNext()) {
                String temp = inFile.next();
                if (temp.matches("[\\d]*"))
                    something with temp;
            }

  2. #22
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: Reading From File Question??

    Sorry I have to use an Array and the for loop is to add values to the indices.

  3. #23
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Reading From File Question??

    You can use array or whatever You want, it doesn't make any difference to what I say.
    Have You considered to use class Stack?
    Stack is array of objects of the same type.
    Last edited by diamonddragon; 01-25-2012 at 04:07 AM.

  4. #24
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: Reading From File Question??

    We've not gotten there in class...so I doubt it. Is there a way to continue after an exception?

  5. #25
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Reading From File Question??

    Well, than obtain counter, as in Your first while loop, create an array (lenght of counter), and implement what I say.
    Is there a problem why You wouldn't do so?

    Or, You can do everything in one loop:
    Say if (temp.matches("[\\d]*")):
    - clone Your array, say tempArray
    - create new array (tempArray.length + 1)
    - copy tempArray into new array
    - update array with temp(String)
    Last edited by diamonddragon; 01-25-2012 at 04:18 AM.

  6. #26
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: Reading From File Question??

    In my professors instructions it does say use try/catch statements

  7. #27
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Reading From File Question??

    Quote Originally Posted by kraigballa View Post
    In my professors instructions it does say use try/catch statements
    Is doesnt matter, You can use try/catch, just put all that coding we are talking about into try block.

  8. #28
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: Reading From File Question??

    Here is my code...Everything is working...but the output is displaying 0 in place of the string in the text. Below code is sample file

    Java Code:
    public static void main (String args[])
    	{
    		int counter = 0; // Keep track of how many integers are in the file
    		double sum = 0;
    		String temp; // Temp int to hold values from file
    		 
    		Scanner scan = new Scanner (System.in);
    		System.out.println("Enter the file name: ");
    		String fName = scan.next();
    		
    		try	{
    			Scanner inFile = new Scanner(new FileReader (fName));
    	        
    	        while(inFile.hasNext())	{
    	            counter++;
    	            inFile.next();
    	        }
    	        
    	        Scanner inFile2 = new Scanner(new FileReader (fName));
    	        int scoresArray[] = new int[counter];
    	        for(int i = 0; i < counter; i++)	{
    	        	
    	            temp = inFile2.next(); // Fill the array with the integers
    
    	            try	{
    	            	scoresArray[i] = Integer.parseInt(temp);	
    	            }
    	            catch(NumberFormatException e) {
    	            	continue;
    	    		}
    	            
    	            sum += scoresArray[i];
    	        }
            
    	        Arrays.sort(scoresArray);
    	        letterGrades(scoresArray, counter);
    	        average(sum, counter);
    		}
    		catch (FileNotFoundException e)	{
    			System.out.println(fName + ": could not be found.");
    		}
    
    	}
    Sample File:
    79 86 97 82 90 89
    68 72 87 78 95 99
    100 10 ff
    12 13 gg

    Here's the output:
    Java Code:
    Enter the file name: 
    scores.txt
    Score	Letter Grade
    ------------------------------
    0		F
    0		F
    10		F
    12		F
    13		F
    68		D
    72		C
    78		C
    79		C
    82		B
    86		B
    87		B
    89		B
    90		A
    95		A
    97		A
    99		A
    100		A
    ------------------------------
    The average test score is: 64.28
    I dont want the two "0's" to show up....
    Last edited by kraigballa; 01-25-2012 at 06:29 AM.

  9. #29
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Reading From File Question??

    Quote Originally Posted by diamonddragon View Post
    Or maybe something like this:

    Java Code:
    String temp = input.next();
    if (temp.matches("[\\d]*"))
       (something with temp)
    Considering the OP is having problems with exceptions and general validation I think introducing Regex is probably not wise.

  10. #30
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Reading From File Question??

    Quote Originally Posted by kraigballa View Post
    I dont want the two "0's" to show up....
    Those two zeroes are the entries in your array for the two invalid values (the letters).
    In the bit of your code where you count the number of values in the file, if you do a number validation there and only counter++ if it is a number then you will get rid of those.

  11. #31
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: Reading From File Question??

    When I try to decrement the counter like this:

    Java Code:
    for(int i = 0; i < counter; i++)	{
    	        	
    	            temp = inFile2.next(); // Fill the array with the integers
    
    	            try	{
    	            	scoresArray[i] = Integer.parseInt(temp);
    	            	sum += scoresArray[i];
    	            }
    	            catch(NumberFormatException e) {
    	            	--i;
    	            	continue;
    	    		}
    	            
    	        }[
    I get this error:
    Java Code:
    Enter the file name: 
    scores.txt
    Exception in thread "main" java.util.NoSuchElementException
    	at java.util.Scanner.throwFor(Scanner.java:838)
    	at java.util.Scanner.next(Scanner.java:1347)
    	at ProcessScores.main(ProcessScores.java:35)

  12. #32
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default Re: Reading From File Question??

    The first thing you should do when you get an exception is go to the API doc for the method that threw the exception and see why it happened. If you have problems understanding the doc, copy it here with your questions.
    Your code called the Scanner class's next() method on line 35. Check the API doc for the Scanner class.

  13. #33
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: Reading From File Question??

    It says to throw it if there are no more elements in the enumeration. So I added an if statement to check for that, and now I am no longer getting that error, but the "0's" are still showing up.

    Java Code:
    try	{
    			Scanner inFile = new Scanner(new FileReader (fName));
    	        
    	        while(inFile.hasNext())	{
    	            counter++;
    	            inFile.next();
    	        }
    	        
    	        Scanner inFile2 = new Scanner(new FileReader (fName));
    	        int scoresArray[] = new int[counter];
    	        for(int i = 0; i < counter; i++)	{
    	        	
    	        	if (inFile2.hasNext())	{
    	        		temp = inFile2.next(); // Fill the array with the integers
    	        	}else	{
    	        		break;
    	        	}
    
    	            try	{
    	            	scoresArray[i] = Integer.parseInt(temp);
    	            	sum += scoresArray[i];
    	            }
    	            catch(NumberFormatException e) {
    	            	--i;
    	            	continue;
    	    		}
    	            
    	        }
    Output:
    Enter the file name:
    scores.txt
    Score Letter Grade
    --------------------------
    0 F
    0 F
    10 F
    12 F
    13 F
    68 D
    72 C
    78 C
    79 C
    82 B
    86 B
    87 B
    89 B
    90 A
    95 A
    97 A
    99 A
    100 A

  14. #34
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default Re: Reading From File Question??

    Perhaps you need two counters in the loop:
    One for the input
    one for where to put the good data.

  15. #35
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: Reading From File Question??

    I see what you're saying...I tried it and still same thing. Variable j should only increment if the try works, not if there is an exception.

    Java Code:
    for(int i = 0; i < counter; i++)	{
    	        	
    	        	if (inFile2.hasNext())	{
    	        		temp = inFile2.next(); // Fill the array with the integers
    	        	}else	{
    	        		break;
    	        	}
    
    	            try	{
    	            	scoresArray[j] = Integer.parseInt(temp);
    	            	sum += scoresArray[j];
    	            	j++;
    	            }
    	            catch(NumberFormatException e) {
    	            	continue;
    	    		}
    	            
    	        }

  16. #36
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default Re: Reading From File Question??

    Does that code work now?

    Do you understand that the array is NOT full, j has the number of good data elements, the rest of the array does not have any data.
    If you have an array of 100 slots and add 20 items of data to it, there will be 80 empty/unused slots.
    Last edited by Norm; 01-25-2012 at 06:42 PM.

  17. #37
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: Reading From File Question??

    Yes, so those unused slots will display '0'? How would I try and go about reading the file and skipping string/char??

  18. #38
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: Reading From File Question??

    I would have to have scoresArray be a temp Array and then transfer the values I want to the new Array of size j huh??

  19. #39
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default Re: Reading From File Question??

    That is what your code is doing now. When you get an exception, you skip the data.
    There are ways of working with partially filled arrays. Look at the methods you are using and see what choices there are.

  20. #40
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: Reading From File Question??

    Got it to work. Added a temp Array and then used j as the size of my new Array. I'm sure there is a shorter way, but for right now it will have to do. Thanks for your help

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. Question about fast reading files in Java.
    By Martino in forum Suggestions & Feedback
    Replies: 0
    Last Post: 05-09-2011, 03:34 PM
  2. URL Reading question
    By g123456 in forum New To Java
    Replies: 5
    Last Post: 03-17-2010, 03:07 AM
  3. Replies: 11
    Last Post: 11-03-2009, 05:32 PM
  4. [SOLVED] File reading question
    By wiz0r in forum New To Java
    Replies: 5
    Last Post: 04-19-2009, 01:21 AM
  5. Question abt.reading xml file using java
    By gvi in forum Advanced Java
    Replies: 6
    Last Post: 11-08-2007, 05:48 PM

Posting Permissions

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