Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Something wrong with my Golf score code? Using NetBean --> Application Input : p

ID: 3846752 • Letter: S

Question

Something wrong with my Golf score code? Using NetBean --> Application

Input :

package golf;

import java.io.*;
import java.util.*;
import java.util.Scanner;
import java.util.ArrayList;

public class Golf {

public static void main(String[] args) throws FileNotFoundException {

int winner = 0;
int player1 = 0;
int player2 = 0;
int player3 = 0;
int player4 = 0;
int par = 0;
int num;
int par2 = 0;
int User1 = 0;
int User2 = 0;
int User3 = 0;
int User4 = 0;

Scanner scan = new Scanner(new File("golf.txt"));

for (int i = 0; i < 18; i++) {

par = scan.nextInt();
player1 = scan.nextInt();
player2 = scan.nextInt();
player3 = scan.nextInt();
player4 = scan.nextInt();
par2 = par2 + par;
User1 = User1 + player1;
User2 = User2 + player2;
User3 = User3 + player3;
User4 = User4 + player4;
}

winner = player1;
num = 1;

if (player2 < winner) {
winner = player2;
num = 2;
}

if (player3 < winner) {
winner = player3;
num = 3;
}

if (player4 < winner) {
winner = player4;
num = 4;
}

}

}

output :

Exception in thread "main" java.io.FileNotFoundException: golf.txt (The system cannot find the file specified)
   at java.io.FileInputStream.open0(Native Method)
   at java.io.FileInputStream.open(FileInputStream.java:195)
   at java.io.FileInputStream.<init>(FileInputStream.java:138)
   at java.util.Scanner.<init>(Scanner.java:611)
   at golf.Golf.main(Golf.java:25)
C:UserskluserAppDataLocalNetBeansCache8.2executor-snippets un.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)

How do I fix it, Thank you

Explanation / Answer

Your file should be directly under the project folder, and not inside any other sub-folder or directory.

else, try giving the following path relative to the project folder to search for file

like

or in windows,

hope it helps!

Do give a thumbs up! ^ ^

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote