0

When I run program it never stops, and don't know how to make (for each line) different v number (gives me syntax error) thanks for helping. file format each line has string int string string

public static void main(String[] args) throws FileNotFoundException, IOException {
    String filelocation = "location";
    filelocation = JOptionPane.showInputDialog("Enter file address");
    BufferedReader inFile = new BufferedReader(new FileReader(filelocation)); //using BufferedReader to get readLine function

    ArrayList<Vehicle> VehicleList = new ArrayList<Vehicle>();
    int counter = 1;
    String dataRow = null; 
    dataRow= inFile.readLine();

    while ( dataRow != null){
        try{
            String[] temp = dataRow.trim().split("\\s+");
            Vehicle v(counter) = new Vehicle (temp[0], Integer.parseInt(temp[1]), temp[2], temp[3], temp[4]);
            VehicleList.add(v1);
            dataRow= inFile.readLine();
        }
        catch(Exception e){}
    }
    inFile.close();
    System.out.println(VehicleList);
    System.out.println(v1);

}
4
  • 3
    You really shouldn't be swallowing exceptions like that. Try at least e.printStackTrace() in your catch block, and you'll see whats really happening. Commented Apr 2, 2014 at 21:50
  • 1
    Java doesn't support the use of dynamic names like you're trying to do. However, you don't really need to have v1, v2, v3, etc. All you need is v because v is protected by the scope of your loop. ... So the only question I have is what you mean by 'it never stops'... it stops at the end, I assume. Commented Apr 2, 2014 at 21:51
  • 1
    Where do you declare v1? If it's inside the loop (which it definitely should be), then that last System.out.println isn't going to compile. But I don't see a declaration of it anywhere. Commented Apr 2, 2014 at 21:55
  • java.lang.ArrayIndexOutOfBoundsException: ( how can I change for each line to go in order? v1 next line v2 etc? thanks agiain Commented Apr 2, 2014 at 22:17

3 Answers 3

1

As has been been said, you are most likely getting an exception within your while loop.

But the main problem with that is dataRow = inFile.readLine() being in the while loop.

while ( dataRow != null){
        try{
            String[] temp = dataRow.trim().split("\\s+");
            Vehicle v(counter) = new Vehicle (temp[0], Integer.parseInt(temp[1]), temp[2], temp[3], temp[4]);
            VehicleList.add(v1);
            dataRow= inFile.readLine(); // <---shouldn't be in while loop
        }
        catch(Exception e){}
    }
    inFile.close();
    System.out.println(VehicleList);
    System.out.println(v1);

}

So what happens is, an exception is thrown, and then jumps to the catch block without ever changing the value of dataRow!

So everytime the while loop is executed, it is actually comparing dataRow to the first value you set it as.

You can get around this by changing the code as in the example below:

    while((dataRow = inFile.readLine()) != null){
        try {
            //some stuff
        }
        catch(Exception e){
            //some other stuff
        }

    }

Make sure to remove the line you have before the while loop that is dataRow = inFile.readLine()

So with this, it will behave more or less how you wanted it to. If you wanted to ignore the thrown exceptions, this will continue to process the file until the end. The line where the exception was thrown will be skipped.

Sign up to request clarification or add additional context in comments.

Comments

0

You can't assign Dynamic variable names. Java is a strongly type language.

What you can do, is save it on a list, and use the index for getting and setting it.

Your code should be as follow:

String[] temp = dataRow.trim().split("\\s+");
Vehicle v = new Vehicle (temp[0], Integer.parseInt(temp[1]), temp[2], temp[3], temp[4]);
VehicleList.add(v);
System.out.println(v);
dataRow= inFile.readLine();

For getting use

VehicleList.get(index)

For setting use

VehicleList.set(index, VEHICLE)

2 Comments

thanks, one more qestion i get java.lang.ArrayIndexOutOfBoundsException:
how can you change v to be static? like first loop has value of 0 than secound line 1 etc... ?
0

The most likely cause to the loop not terminating is an exception within the read loop (you consume the exception and try to continue). Based on your example, I think that you have a mismatch on input from split versus the Vehicle constructor. Also as you found out, you cannot have dynamic variable names such that Vehicle v(counter) = new Vehicle(... gives a syntax exception.

See if this version helps show you where the problem is.

public static void main(String[] args) throws FileNotFoundException, IOException {
    String filelocation = "location";
    filelocation = JOptionPane.showInputDialog("Enter file address");
    BufferedReader inFile = new BufferedReader(new FileReader(filelocation)); //using BufferedReader to get readLine function

    ArrayList<Vehicle> VehicleList = new ArrayList<Vehicle>();
    int counter = 0;
    String dataRow = null; 
    dataRow= inFile.readLine();

    try{
        while ( dataRow != null){
            String[] temp = dataRow.trim().split("\\s+");

            if (temp.length != 5) 
                throw new Exception("split returned "+temp.length);

            Vehicle v = new Vehicle (temp[0], Integer.parseInt(temp[1]), temp[2], temp[3], temp[4]);  
            VehicleList.add(v);
            System.out.println(v);
            dataRow= inFile.readLine();
        }
    }
    catch(Exception e){
        e.printStackTrace();
    }
    finally {
        inFile.close();
    }

    System.out.println(VehicleList);

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.