0

My program is to use a JOption pane to input an option. Those options (1 or 2) are to be linked to a .txt file that I have saved under my package in my NetBeans project. This is what i have so far. I will also attach the .txt files that have the numbers embedded in which my program is supposed to read and output. Any help is greatly appreciated! My input trap/JOption pane is working perfectly. But I'm having an error when reading the files.

run:
error opening the filecreeper.txt
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

This is what is being output at this point.

package project;

import java.awt.Color;
import javax.swing.JFrame;
import java.awt.Graphics; 
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JOptionPane;

public class Project extends JFrame{
private static final String filename1 = "creeper.txt";
private static final String filename2 = "sun.txt";
private static final int FRAME_SIZE = 700;
private String filename;
private String stringName;
private String COLOR;
private String LINE;
private String OVAL;
private String FILLEDOVAL;
private String RECTANGLE;
private String FILLEDRECTANGLE;
int i;

Scanner inputFile = new Scanner(System.in);

public static void main(String[] args){
Project p = new Project();   
 }

Scanner fin = null;

@Override
public void paint(Graphics g) {
    Graphics canvas = this.getContentPane().getGraphics();

    int num1;
    int num2;
    int num3;
    int num4;
    int color1;
    int color2;
    int color3;

    try{

       fin = new Scanner(new File(filename));

    }catch(FileNotFoundException e){
            System.err.println("error opening the file" + filename);
            System.exit(1);

    }while (fin.hasNext()){

            String reader = fin.next();

    switch(stringName){
        case "COLOR":
            color1 = inputFile.nextInt();
            color2 = inputFile.nextInt();
            color3 = inputFile.nextInt();
            canvas.setColor(new Color (color1, color2, color3));
            break;

        case "LINE":
            num1 = inputFile.nextInt();
            num2 = inputFile.nextInt();
            num3 = inputFile.nextInt();
            num4 = inputFile.nextInt();
            canvas.drawLine(num1, num2, num3, num4);
            break;

        case "OVAL":
            num1 = inputFile.nextInt();
            num2 = inputFile.nextInt();
            num3 = inputFile.nextInt();
            num4 = inputFile.nextInt();
            canvas.drawOval(num1, num2, num3, num4);
            break;

        case "FILLEDOVAL":
            num1 = inputFile.nextInt();
            num2 = inputFile.nextInt();
            num3 = inputFile.nextInt();
            num4 = inputFile.nextInt();
            canvas.fillOval(num1, num2, num3, num4);
            break;

        case "RECTANGLE":
            num1 = inputFile.nextInt();
            num2 = inputFile.nextInt();
            num3 = inputFile.nextInt();
            num4 = inputFile.nextInt();
            canvas.drawRect(num1, num2, num3, num4);
            break;

        case "FILLEDRECTANGLE":
            num1 = inputFile.nextInt();
            num2 = inputFile.nextInt();
            num3 = inputFile.nextInt();
            num4 = inputFile.nextInt();
            canvas.fillRect(num1, num2, num3, num4);
            break;

    }
        }
}
public Project() {

    String valueString;
    int input = 0;
    do{//input error trap

    valueString = JOptionPane.showInputDialog("Choose your file:\n1) Creeper\n2) Sun");

    try{

    input = Integer.parseInt(valueString);

    }catch (NumberFormatException e){

    }

    } while ((input != 1) && (input != 2));

    switch(input){
        case 1:
            filename = filename1;
            break;
        case 2:
            filename = filename2;
            break;
    }

    setSize(700, 700);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setLocationRelativeTo(null);
    setVisible(true);
}
}


creeper.txt
COLOR 0 161 94
FILLEDRECTANGLE 0 0 1000 1000
COLOR 0 0 0
FILLEDRECTANGLE 125 100 150 150
FILLEDRECTANGLE 425 100 150 150
FILLEDRECTANGLE 275 250 150 250
FILLEDRECTANGLE 200 325 75 250
FILLEDRECTANGLE 425 325 75 250


sun.txt
COLOR 255 255 128
FILLEDOVAL 200 200 200 200
COLOR 0 0 0
OVAL 200 200 200 200
LINE 200 200 10 50
LINE 200 400 10 550
LINE 400 200 550 10
LINE 400 400 650 650
COLOR 255 100 100
LINE 300 450 300 650
LINE 450 300 670 300
LINE 300 150 300 10
LINE 150 300 10 300
user3338808
  • 11
  • 1
  • 2

2 Answers2

0

The file path is incorrect. You can put the files in the same directory or provide the full path like:

private static final String filename1 = "C:/Users/Foo/creeper.txt";

Another option is to put the files together with the sources and:

new File(Project.class.getResource(filename).toURI());

For more information, see this question.

Community
  • 1
  • 1
elias
  • 15,010
  • 4
  • 40
  • 65
0

1.Make sure the file path is correct.If the ".java" file is in same folder as those text files then the path mentioned in the above code works.

2.I found a mistake in your code below, rest else code looks fine.

you are actually using inputFile.nextInt() , it should be like fin.nextInt().Modify this at all the places used in switch case statements.

RKC
  • 1,834
  • 13
  • 13