Results 1 to 6 of 6
  1. #1
    MattBSibley is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default File reading / writing

    I'm very sorry if this is in the wrong forum.. if it is, if you could tell me where it belongs, I'll gladly move it.

    I'm creating a text file that my java program is reading and writing from. The text file stores a name, birth year, birth month, and birth day like this: Sibley,1990,9,7. My reader is as follows:

    Java Code:
            File newFile = new File("data.txt");
            Scanner scanner = new Scanner(newFile).useDelimiter(",");
            try {
                String firstName = scanner.next();
                int birthYear = scanner.nextInt();
                int birthMonth = scanner.nextInt();
                int birthDay = scanner.nextInt();
                System.out.println(firstName + " " + birthYear + " " + birthMonth + " " + birthDay);
            } catch (Exception e) {
                System.out.println(e);
            }
    Unfortunately, whenever I have more than 1 line of data, this crashes. Giving me the message "java.util.InputMismatchException"

    I have more questions, but I don't want to ramble on if this isn't posted in the correct forum.

    Thanks in advance for any/all help :)
    Last edited by Fubarable; 04-18-2010 at 11:38 PM. Reason: Code tags added

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

    Default

    Quote Originally Posted by MattBSibley View Post
    ...
    Unfortunately, whenever I have more than 1 line of data, this crashes. Giving me the message "java.util.InputMismatchException"
    Show a typical file that causes a crash. Also, are you trying to get this program to read multiple lines of data, and if so, what do you wish it to do with the data?

    I have more questions, but I don't want to ramble on if this isn't posted in the correct forum.
    Thanks in advance for any/all help :)
    Yep, wrong forum. Moving.

    edit: welcome to the forum, by the way! Oh, and please read the link in my signature regarding code tags (which I've added to your post).

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

    Default

    One solution is to use two Scanner objects, one called fileScanner which uses a while loop to check if fileScanner.hasNextLine() and if so reads in the next line. This String is fed into the lineScanner object which uses your "," delimiter and grabs all the tokens from the line.

    Also, be careful to close all Scanner objects when you're done with them.

  4. #4
    MattBSibley is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default Cont'd

    Other,1968,0,24
    Sibley,1990,9,7

    Ok, well, Something like this: Sibley,1990,9,7
    works fine, but as soon as I have more than one:
    Other,1968,0,24
    Sibley,1990,9,7
    it fails with a "java.util.InputMismatchException"

    I have decided since my last post, that I will limit the amount of data in the file to 10 columns, so:
    Position1,User1,####,##,##
    Position2,User2,####,##,##
    Position3,User3,####,##,##
    Position4,User4,####,##,##
    Position5,User5,####,##,##
    Position6,User6,####,##,##
    Position7,User7,####,##,##
    Position8,User8,####,##,##
    Position9,User9,####,##,##
    Position10,User10,####,##,##
    I need a scanner that will scan the first line, set it to a string, then a second scanner will use the coma dilimited scanner to set the values of that row to a multi-dimensional array with a for loop and 2 counters (one for columns and one for rows) and go down the rows that way. Once my array is filled with the values, I should be set. I hate to sit here and ask for someone to code it for me, but I'll start working on it, and I'll post it as soon as I'm done. If anyone else has a better way, ro a good way for me to start, it would be greatly appreciate, I'll refresh this window often.

  5. #5
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    You can use BufferedReader to read each line correctly
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

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

    Default

    Quote Originally Posted by Webuser View Post
    You can use BufferedReader to read each line correctly
    Yep, it can be done with BufferedReader and then String#split, or again it can be done with two Scanners (Please see comments):
    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.InputMismatchException;
    import java.util.Scanner;
    
    public class Test1 {
      // private static final String MY_DATA = "src/yr2010/m04/d/myData.txt"; // need this to work on my system.
      private static final String MY_DATA = "myData.txt";
    
      public static void main(String[] args) {
        Scanner fileScanner = null; // *** Scanner #1
        try {
          File newFile = new File(MY_DATA);
    
          // the fileScanner is responsible for reading each line from the 
          // file, and that's it -- nothing more.
          fileScanner = new Scanner(newFile);
          while (fileScanner.hasNextLine()) {
    
            // file scanner gets the line:
            String line = fileScanner.nextLine();  
            Scanner lineScanner = null;  // *** Scanner #2
            try {
    
              // The lineScanner is responsible for scanning the line
              // obtained previously by the fileScanner.
              // The linescanner extracts the names, and dates from the line
              lineScanner = new Scanner(line).useDelimiter(",");
              String firstName = lineScanner.next();
              int birthYear = lineScanner.nextInt();
              int birthMonth = lineScanner.nextInt();
              int birthDay = lineScanner.nextInt();
              System.out.println(firstName + " " + birthYear + " " + birthMonth + " " + birthDay);
            } catch (InputMismatchException e) {
              e.printStackTrace();
            } finally {
              lineScanner.close();
            }
          }
        } catch (FileNotFoundException e) {
          e.printStackTrace();
        } finally {
          if (fileScanner != null) {
            fileScanner.close();
          }
        }
      }
    }

Similar Threads

  1. reading and writing to file within a jar archive
    By porchrat in forum New To Java
    Replies: 3
    Last Post: 10-12-2009, 07:45 PM
  2. Replies: 3
    Last Post: 05-09-2009, 11:31 PM
  3. Reading and writing to a file
    By jigglywiggly in forum New To Java
    Replies: 13
    Last Post: 03-09-2009, 10:44 AM
  4. Reading/Writing to file
    By Doctor Cactus in forum New To Java
    Replies: 2
    Last Post: 10-28-2008, 02:05 PM
  5. Help with File reading and writing
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 06:47 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
  •