Page 1 of 2 12 LastLast
Results 1 to 20 of 22
Like Tree3Likes

Thread: Any help would be appreciated (reading from file, sorting, arrays)!

  1. #1
    Labombadog's Avatar
    Labombadog is offline Member
    Join Date
    Jan 2012
    Location
    Ohio
    Posts
    8
    Rep Power
    0

    Default Any help would be appreciated (reading from file, sorting, arrays)!

    I am writing a program that reads from a file. The file contain a certain amount of numbers and want these numbers to be added to an array (not an ArrayList). Also need the final array to sort in ascending sequence with even and odd numbers.

    I am fairly new to Java, but I want to truly learn this language.

    This is what I got so far:
    Java Code:
    class readFile 
    {
      public static void main(String[] args) throws FileNotFoundException {
    			Scanner input = new Scanner(new File("c:\\temp\\myData.txt"));
    			
    			int [] number = new int [10];
    			int i = 0;
    			while(input.hasNextInt())
    				
    			{
    				number[i++] = input.nextInt();
    				System.out.println(number);
    				System.out.println(i);
    
    			}
    			
    			
    			}	
    		}
    This is what I get so far. I am not sure what I am doing wrong BUT when I compile I get no errors but I also get nothing. ???


    Any help would be appreciated. :)

  2. #2
    angrodm is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: Any help would be appreciated (reading from file, sorting, arrays)!

    Try adding a debug line before the while loop and one right inside the while loop. An example
    Java Code:
    class readFile 
    {
      public static void main(String[] args) throws FileNotFoundException {
                Scanner input = new Scanner(new File("c:\\temp\\myData.txt"));
                int [] number = new int [10];
                int i = 0;
                                             System.err.println("outside the while loop");
                while(input.hasNextInt())   
                {
                                             System.err.println("inside the while loop");
                    number[i++] = input.nextInt();
                    System.out.println(number);            //If you want to print the number you just read in you would need number[i-1]
                                                                     //the reason you need i-1 is because you just incremented i.
                    System.out.println(i);                    //This line will only print out i so 1, 2, 3, 4, ... on for as long as the loop runs.
     
                }
                }   
            }
    see if those end of printing. By using statements like these debug statements you can narrow down where your problem is. Hope this helps.

  3. #3
    Labombadog's Avatar
    Labombadog is offline Member
    Join Date
    Jan 2012
    Location
    Ohio
    Posts
    8
    Rep Power
    0

    Default Re: Any help would be appreciated (reading from file, sorting, arrays)!

    Hmm thanks for your tip. I have entered those lines and ended up getting an error on the outside of the loop.

    Thanks.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Any help would be appreciated (reading from file, sorting, arrays)!

    getting an error
    Post the full text of the error message and the code that caused it.

  5. #5
    Labombadog's Avatar
    Labombadog is offline Member
    Join Date
    Jan 2012
    Location
    Ohio
    Posts
    8
    Rep Power
    0

    Default Re: Any help would be appreciated (reading from file, sorting, arrays)!

    Quote Originally Posted by Norm View Post
    Post the full text of the error message and the code that caused it.
    When I run the code it takes me to the debug. Not sure why. But in the debugger console it says:


    outside the while loop


    This comes from the code on line 7: System.err.println("outside the while loop");

    Been working on this for 5 hours and feeling a bit discouraged..... Thanks for your help.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Any help would be appreciated (reading from file, sorting, arrays)!

    The program is waiting for you to type in a number: nextInt()
    Try typing in a number
    diamonddragon likes this.

  7. #7
    Labombadog's Avatar
    Labombadog is offline Member
    Join Date
    Jan 2012
    Location
    Ohio
    Posts
    8
    Rep Power
    0

    Default Re: Any help would be appreciated (reading from file, sorting, arrays)!

    Quote Originally Posted by Norm View Post
    The program is waiting for you to type in a number: nextInt()
    Try typing in a number
    When I run the code it takes me straight to the debugger (for some odd reason). Plus isn't nextInt() looking for the next available integer in my text file?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Any help would be appreciated (reading from file, sorting, arrays)!

    Sorry, missed the file as input. Is the first thing in the file a valid number?

    No idea why your IDE puts you in the debugger. Try asking how to use your IDE in the IDE section of the forum.

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

    Default Re: Any help would be appreciated (reading from file, sorting, arrays)!

    This:
    System.err.println("outside the while loop");

    is what You printed, with err PrintStream, instead of out PrintStream.
    You can use err or out PrintStream, and it is same, by convention PrintStream err is used for displaying error messages.

  10. #10
    Labombadog's Avatar
    Labombadog is offline Member
    Join Date
    Jan 2012
    Location
    Ohio
    Posts
    8
    Rep Power
    0

    Default Re: Any help would be appreciated (reading from file, sorting, arrays)!

    I closed the 'Console' window, and that was the reason it was taking me to the debugger instead. I restored the console window and getting the following errors:


    outside the while loop
    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.main(readFile.java:21)


    Bleh.

    Again thank you for all of your help, and having patience for a newcomer like myself.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Any help would be appreciated (reading from file, sorting, arrays)!

    So, just to clarify things, have you fixed the error now?

  12. #12
    Labombadog's Avatar
    Labombadog is offline Member
    Join Date
    Jan 2012
    Location
    Ohio
    Posts
    8
    Rep Power
    0

    Default Re: Any help would be appreciated (reading from file, sorting, arrays)!

    Quote Originally Posted by Fubarable View Post
    So, just to clarify things, have you fixed the error now?
    Unfortunately no. I am getting the errors that I posted above.

    outside the while loop
    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.main(readFile.java:21)

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Any help would be appreciated (reading from file, sorting, arrays)!

    For debugging, Change your code so that you read the int into a variable before putting it in the array. Something like the following:
    int var = scanr.nextInt()
    println("var=" + var);
    theArray[ix] = var;

    The printed output will show you where in the input file you are when the error happens.
    Fubarable likes this.

  14. #14
    Labombadog's Avatar
    Labombadog is offline Member
    Join Date
    Jan 2012
    Location
    Ohio
    Posts
    8
    Rep Power
    0

    Default Re: Any help would be appreciated (reading from file, sorting, arrays)!

    So I now have an issue. My text file contains: {6, 2, 3, 4, 5, 1, 9, 7, 8} and I believe when I run the program it crashes because the first thing it encounters is a left curly brace. Any techniques to avoid this or am I doing something wrong?

    *pulls hair* lol

    import java.io.*;
    import java.util.*;



    //create a scanner to read the file, declare the integer array,


    class readFile
    {
    public static void main(String[] args) throws FileNotFoundException {
    Scanner input = new Scanner(new File("c:\\temp\\myData.txt"));

    int [] number = new int [10];
    int [] oddNumber = new int [5]; //if the number is an odd number then it will go here
    int [] evenNumber = new int [4]; //if the number in an even number then it will go here

    for(int i = 0; i <= 10; i++)
    {
    number[i] = input.nextInt();

    if ( number[i] % 2 == 0)
    {
    evenNumber[i] = number[i];
    }
    else
    oddNumber[i] = number[i];
    System.out.println(evenNumber[i] + oddNumber[i]);
    }
    This is the what the console throws at me when I launch this program:

    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.main(readFile.java:20)
    Last edited by Labombadog; 01-26-2012 at 11:29 PM.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Any help would be appreciated (reading from file, sorting, arrays)!

    The Scanner class has some methods to test the type of the input, they all begin with has.
    Try using some of them to test the type of the next token before reading it.
    Labombadog likes this.

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

    Default Re: Any help would be appreciated (reading from file, sorting, arrays)!

    You could use input.next() to retrieve next complete token, assign it to String variable, and then analyze String(extrude everything except digits).
    Say:
    Java Code:
    String filteredToken;
    String token = input.next();
    for (int i = 0; i < token.length(); i++)
       if (Character.isDigit(token.charAt[i]))
          filteredToken += token.valueOf(token.charAt[i]);
    At this point, You will have filtered token, consisting just of digits.

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Any help would be appreciated (reading from file, sorting, arrays)!

    Are you back trying to confuse the OPs again?

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

    Default Re: Any help would be appreciated (reading from file, sorting, arrays)!

    Quote Originally Posted by Norm View Post
    Are you back trying to confuse the OPs again?
    Why, what's the problem now?

  19. #19
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Any help would be appreciated (reading from file, sorting, arrays)!

    Quote Originally Posted by Labombadog View Post
    So I now have an issue. My text file contains: {6, 2, 3, 4, 5, 1, 9, 7, 8} and I believe when I run the program it crashes because the first thing it encounters is a left curly brace. Any techniques to avoid this or am I doing something wrong?
    Also, can you show us the exact precise text file, not a description of what it contains as the details are important. You can either post the file in a forum note or you can upload the file if it has a txt extension.

    Also as a caveat, take diamonddragon's recommendations with a grain of salt. He means well but has been doing Java only as long as you have, if that.

  20. #20
    Labombadog's Avatar
    Labombadog is offline Member
    Join Date
    Jan 2012
    Location
    Ohio
    Posts
    8
    Rep Power
    0

    Default Re: Any help would be appreciated (reading from file, sorting, arrays)!

    No harm done. :) I appreciate everybody's effort in helping me.

    I uploaded the file: myData.txt

    I've also been messing around with the Scanners skip I am still getting errors. I went to Java tutoring today, and the tutor never showed up.... :\ I have more help here then in school. Go figure.

Page 1 of 2 12 LastLast

Similar Threads

  1. Reading a file into 5 arrays
    By mossberg in forum New To Java
    Replies: 2
    Last Post: 10-30-2011, 09:07 PM
  2. Sorting arrays
    By Lex in forum New To Java
    Replies: 4
    Last Post: 08-09-2011, 02:19 AM
  3. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  4. Sorting Two Arrays
    By Faye Rett in forum New To Java
    Replies: 4
    Last Post: 03-07-2010, 01:00 AM
  5. Arrays and File Reading
    By DaFlake in forum New To Java
    Replies: 3
    Last Post: 08-12-2009, 05:28 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
  •