hotdogs mustardyeah
hotdogs mustardyeah

Reputation: 1

scanner troubles in java

I am writing an obj viewer of sorts with the Lightweight Java Game Library (lwjgl) and I am having some troubles with reading the external obj file. When I run the code it does not even print anything to the stack trace, much less print the nextLine. Here is my whole code (in case it is an error in the syntax outside of the block of code, or I put it in the wrong place.

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;


public class MainDisplay {

public void start() {
try {
    Display.setDisplayMode(new DisplayMode(800,600));
    Display.create();
}
catch (LWJGLException e) {
    e.printStackTrace();
    System.exit(0);
}

GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 800, 0, 600, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);

while (!Display.isCloseRequested()) {
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
    GL11.glColor3f(0.5f, 0.5f, 1.0f);


}
}
/**
 * @param args
 * @throws FileNotFoundException 
 */
public static void main(String[] args) throws FileNotFoundException {
    try {
        FileReader obj = new FileReader("test.obj");
        Scanner scanner = new Scanner(obj);
        String line = scanner.nextLine();
    } catch (IOException e) {
        e.printStackTrace();
    }


    MainDisplay mainDisplay = new MainDisplay();
    mainDisplay.start();



}

}

Upvotes: 0

Views: 91

Answers (1)

Karthik Balakrishnan
Karthik Balakrishnan

Reputation: 4383

Here, try this.

while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            System.out.println(line);
        }
        scanner.close();

What you had done was just store the value of the first line in the file in the variable. The while loop I've set up reads all the lines in the file, it stops only after reaching nullor EOF.

Upvotes: 1

Related Questions