Hello all,

Currently I am trying to create a program that prompts the user to select a file via JFileChooser, and then continues to use that file to gather information for objects. I have no problem selecting the file from JFileChooser, but I do have a problem reading the correct information from the file.

A sample file I am trying to read is:

// Comment section
// Comment section

w : 100 : TheFulhams
w : 200 : TheBeckhams
e : 300 : Boy : Charlie : 100 : 20 : 21 : 22
e : 400 : Girl : Sally : 200 : 1 : 2 : 3

Ultimately, I want the program to check if the line starts with //, isEmpty, w, or e. If it is // or isEmpty I want the loop to skip the whole line. If the line starts with w, or e, I want the program to enter an if-statement for that specific letter, read the line after the w or e, and assign the information to the appropriate variable. I want to create an object using the information I gather from that line, then after it is finished with the line it should go back to the beginning of the while-loop and do the same thing for the next line.

The code for gathering information from the file is:

        // Method created to scan userFile to create objects and arrayList.
        public static void createObjects(Scanner inputFile) {

            // Delimiter set to ignore semi-colons
            inputFile.useDelimiter("//:,|\\n");


            // While loop used to continue creating objects if file is not done reading
            while (inputFile.hasNextLine()) {

                // Creates string line to use if-statement
                // Program should check for first letter in the statement, if true
                // it should skip the first letter and attach the following information
                // on the line to variables and restart the while loop until end of file
                // is reached.
                String line = inputFile.nextLine().trim();
                if ((line.startsWith("//")) || (line.isEmpty())) {
                    System.out.println("Comment Line or Blank Line");
                } else if (line.startsWith("w")) {
                    // Do stuff for w here
                    String partyIndex = inputFile.next();
                    String partyName = inputFile.next();
                    System.out.print(partyIndex);
                    System.out.print(partyName + "\n");
                } else if (line.startsWith("e")) {
                    // Do stuff for e here
                    String humanIndex = inputFile.next();
                    String humanType = inputFile.next();
                    String humanName = inputFile.next();
                    String partyIndex = inputFile.next();
                    String num1 = inputFile.next();
                    String num2 = inputFile.next();
                    String num3 = inputFile.next();
                } else {
                    break;
                }
            }
        }

At the moment I get I have all of the variables set as Strings because int is returning an exception error (due to the fact that the scanner.next() is trying to assign the full line to the variable it seems). The output I get is:

Comment Line or Blank Line
Comment Line or Blank Line
Exception in thread "main" java.util.NoSuchElementException
Comment Line or Blank Line
w : 200 : TheBeckhams
e : 300 : Boy : Charlie : 100 : 20 : 21 : 22
Comment Line or Blank Line
    at java.util.Scanner.throwFor(Scanner.java:907)
    at java.util.Scanner.next(Scanner.java:1416)
    at cmsc335project1.CMSC335Project1.createObjects(CMSC335Project1.java:117)
    at cmsc335project1.CMSC335Project1.CMSC335Project1(CMSC335Project1.java:47)
    at cmsc335project1.CMSC335Project1.main(CMSC335Project1.java:24)
Java Result: 1

It seems like the program is running the first two comment lines correctly, runs into an exception (I know I can use a try-catch but I would like to know why an exception occured when I don't think it should). When the program reaches String partyIndex = inputFile.next(); and String partyName = inputFile.next(); it seems like it ignores the first line it should be reading, and not only that but it is assigning the full line to the variable. Any guidance as to why it is doing this, and any solutions/hints that will help me learn is greatly appreciated.

The desired output I am looking for is as follows:
Comment Line or Blank Line
Comment Line or Blank Line
Comment Line or Blank Line
100TheFulhams
200TheBeckhams

Once I figure out how to do that I am pretty positive creating objects using the gathered variables from the text file and assigning them to an arraylist will be very simple.

Recommended Answers

All 3 Replies

If you want to understand why, put System.println("Read Content Line: "+line); at line 17. I have a feeling that you may not completely know what the useDelimiter() method is doing... Also, you mix next() with hasNextLine()... It would be better to use hasNext() with next() so no unexpected result would show up...

A suggestion...
1)read in a line as a whole string
2)trim() the string
3)use split("\\s*:\\s*") from String class
4)iterate through the split data (now you have each item of that line data w/o white space)

PS: You could check if the incoming string is empty after #2.

Thanks I finally got all of the information into their own variables. Would it also be safe to check for w or e after the line is empty? Such as an if statement like this:

if (isEmpty)
    continue;
else if (startsWith(w))
    split line
    iteration for w
else if (startsWith(e))
    split line
    iteration for e

Starting to work on it now but I figured I may as well ask incase I run into problems soon. Also, thanks for the line to put @17, really helped me understand why I was running into issues.

Yes, you could check for the starting value if you are dealing with them differently. And you are welcome.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.