When a button is clicked: get my Data!!!
Hey There..
I'm having a problem occurring within my code.
I have a txt file containing jibberish called testingfile.txt
testingfile.txt contains:
hithere34|where 293|21|3|9
heythere5|where 633|25|4|9
hellothere66|where 918|21|7|9
Now when the 'List' button is clicked I want it to just print in the console the data within the testingfile.txt (I want it to print out in the console so I know that it is being called).
I tried this but it keeps throwing an IOException.. :
Code :
public myGrades() {
initComponents();
txtAreaActions number = new txtAreaActions();
btnList.addActionListener(number);
}
public class txtAreaActions implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == btnList) {
Scanner scanner = new Scanner(new File("testingfile.txt"));
scanner.useDelimiter("\\|, \\s, ");
while (scanner.hasNextLine()) {
String s = scanner.next();
parseRecord(s);
}
scanner.close();
}
} catch (IOException error) {
System.out.println("Error");
}
}
}
public void parseRecord(String record) {
Scanner recIn = new Scanner(record);
recIn.useDelimiter("\\|, \\s");
String area1 = recIn.next();
String area2 = recIn.next();
int num1 = Integer.parseInt(recIn.next());
int num2= Integer.parseInt(recIn.next());
int num3 = Integer.parseInt(recIn.next());
double total = (num1 / num2) * num3;
System.out.println(area1 + area2);
System.out.printf("%.2f%n", total);
}
Any clues?
Thanks!
Re: When a button is clicked: get my Data!!!
It might help if you obtain more information than merely "Error".
Code :
} catch (IOException error) {
//System.out.println("Error");
error.printStackTrace();
}
Re: When a button is clicked: get my Data!!!
Got this:
java.io.FileNotFoundException: testingfile.txt (The system cannot find the file specified)
Re: When a button is clicked: get my Data!!!
In fact you will have got a stack trace that indicates where the problem occured: I'm guessing in the line that initialised scanner.
You could check the full path of the file you are trying to open (there is a File method that returns this) before you inialise the scanner.
Code :
File file = new File("testingfile.txt");
System.out.println("Using " + file.xxx());
Scanner scanner = ...
(Sorry, I'm posting from my phone and it's too painful to look up the method name...)
Re: When a button is clicked: get my Data!!!
Quote:
Originally Posted by
pbrockway2
You could check the full path of the file you are trying to open (there is a File method that returns this)...
(Sorry, I'm posting from my phone and it's too painful to look up the method name...)
getAbsolutePath() ;)
Your program must know where the file is...so you must specify the full or relative path to that file (relative to where you are running your java app). Assuming your class posted above is not in a package, the file must be located in the same directory as your class file.
Re: When a button is clicked: get my Data!!!
if it is inside a package how would I get the relative path name?
or would the getAbsolutePath() do the trick :)
i tried this:
Code :
public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == btnList) {
File file = new File("testingfile.txt");
Scanner scanner = new Scanner(file.getAbsoluteFile());
scanner.useDelimiter("\\|, \\s, ");
while (scanner.hasNextLine()) {
String s = scanner.next();
parseRecord(s);
}
scanner.close();
}
} catch (IOException error) {
System.out.println("Error");
}
}
}
No luck :(
Re: When a button is clicked: get my Data!!!
Where is the file relative to your class? Is your class in a package?