i’m new to java and i’ve try to study about open, read file on java through jGRASP. But I prefer to work with VS Code and when i code on VS, it can’t open the file. On the other hand, it works on jGRASP.
Can anyone told me what did i’m wrong?
Here is my code:
<code>import java.io.*;
import java.util.Scanner;
public class Pratice {
public static void main(String[] args) {
File dataFileLink = new File("CS121DataFile3.txt");
Scanner fileScan = null;
try {
fileScan = new Scanner(dataFileLink);
} catch (Exception e) {
// TODO: handle exception
System.out.println("File not open correctly");
System.exit(1);
}
String fname, lname;
int byear,dyear, age, tCount, yCount;
tCount = 0;
yCount = 0;
while (fileScan.hasNext()) {
fname = fileScan.next();
lname = fileScan.next();
byear = fileScan.nextInt();
dyear = fileScan.nextInt();
age = dyear - byear;
System.out.printf("President: %s lived %d years", lname, age);
System.out.println();
tCount++;
yCount += age;
}
System.out.println();
double avg = 1.0 * yCount / tCount;
System.out.printf("The avg age of the first %d president was %.1f", tCount, avg);
}
}
</code>
<code>import java.io.*;
import java.util.Scanner;
public class Pratice {
public static void main(String[] args) {
File dataFileLink = new File("CS121DataFile3.txt");
Scanner fileScan = null;
try {
fileScan = new Scanner(dataFileLink);
} catch (Exception e) {
// TODO: handle exception
System.out.println("File not open correctly");
System.exit(1);
}
String fname, lname;
int byear,dyear, age, tCount, yCount;
tCount = 0;
yCount = 0;
while (fileScan.hasNext()) {
fname = fileScan.next();
lname = fileScan.next();
byear = fileScan.nextInt();
dyear = fileScan.nextInt();
age = dyear - byear;
System.out.printf("President: %s lived %d years", lname, age);
System.out.println();
tCount++;
yCount += age;
}
System.out.println();
double avg = 1.0 * yCount / tCount;
System.out.printf("The avg age of the first %d president was %.1f", tCount, avg);
}
}
</code>
import java.io.*;
import java.util.Scanner;
public class Pratice {
public static void main(String[] args) {
File dataFileLink = new File("CS121DataFile3.txt");
Scanner fileScan = null;
try {
fileScan = new Scanner(dataFileLink);
} catch (Exception e) {
// TODO: handle exception
System.out.println("File not open correctly");
System.exit(1);
}
String fname, lname;
int byear,dyear, age, tCount, yCount;
tCount = 0;
yCount = 0;
while (fileScan.hasNext()) {
fname = fileScan.next();
lname = fileScan.next();
byear = fileScan.nextInt();
dyear = fileScan.nextInt();
age = dyear - byear;
System.out.printf("President: %s lived %d years", lname, age);
System.out.println();
tCount++;
yCount += age;
}
System.out.println();
double avg = 1.0 * yCount / tCount;
System.out.printf("The avg age of the first %d president was %.1f", tCount, avg);
}
}
When ever i use jGRASP, it work then print: President: Roosevelt lived 63 years
when ever i use VSCode, it doesn’t work then print:”File not open correctly”
New contributor
user25200159 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.