i need to fix this java code. it not working on Repl.it website online compiler.
error
-----------------------------------------------
javac -classpath .:/run_dir/junit-4.12.jar:target/dependency/*
-d . Main.java
java -classpath .:/run_dir/junit-4.12.jar:target/dependency/*
Main
Exception in thread "main" java.io.FileNotFoundException:
C:\\Users\\Carter Collins\\Downloads\\input.txt (No such file or
directory)
at java.base/java.io.FileInputStream.open0(Native Method)
at
java.base/java.io.FileInputStream.open(FileInputStream.java:219)
at
java.base/java.io.FileInputStream.(FileInputStream.java:157)
at java.base/java.util.Scanner.(Scanner.java:639)
at Main.main(Main.java:18)
exit status 1
------------------------------------------------
codes::
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception
{
Scanner scan = new Scanner(System.in);
Scanner sc = new Scanner(new File("C:\\\\Users\\\\Carter Collins\\\\Downloads\\\\input.txt"));
Random rand = new Random();
int Bank = 0;
int currentBet = 0;
int Select = 0;
String Fruit = "";
int currentWinning = 0;
char status = 'x';
int random1;
int random2;
int random3;
String fruits[] = {"Cherry", "Orange", "Apple", "Peach", "Melon", "Pear"};
System.out.println("Welcome to Carter Collins' Slot Machine!");
Bank = sc.nextInt();
System.out.println("You inserted $" + Bank + " into the slot machine.");
while(status == 'x' || status == 'X')
{
System.out.println("You currently have $" + Bank + " , you can bet with multiples of $10.");
System.out.println("If you enter 1, you will bet $10");
System.out.println("If you enter 2, you will bet $20, etc.");
System.out.println("Please enter how much you want to bet this time: ");
currentBet = scan.nextInt();
currentBet = currentBet * 10;
while(currentBet > Bank)
{
System.out.println("Sorry, you don't $" + currentBet + " now. You can only bet less than the amount of money you have.");
System.out.println("You currently have $" + Bank + " , you can bet the multiple of $10.");
System.out.println("If you enter 1, you will bet $10");
System.out.println("If you enter 2, you will bet $20, etc.");
System.out.println("Please enter how much do you want to bet this time: ");
currentBet = scan.nextInt();
}
System.out.println("Your current bet: $" + currentBet);
System.out.println("Please select one of the fruit by entering a number between 1 and 6:");
System.out.println("1 - Cherry");
System.out.println("2 - Orange");
System.out.println("3 - Apple");
System.out.println("4 - Peach");
System.out.println("5 - Melon");
System.out.println("6 - Pear");
System.out.println("Please enter your selection: ");
Select = scan.nextInt();
while(Select > 6 || Select < 1)
{
System.out.println("Invalid selection. you must enter a number between 1 and 6 inclusive.\n");
System.out.println("Please select one of the fruit by entering a number between 1 and 6:");
System.out.println("1 - Cherry");
System.out.println("2 - Orange");
System.out.println("3 - Apple");
System.out.println("4 - Peach");
System.out.println("5 - Melon");
System.out.println("6 - Pear");
System.out.println("Please enter your selection: ");
Select = scan.nextInt();
}
Fruit = fruits[Select - 1];
random1 = rand.nextInt(5) + 1;
random2 = rand.nextInt(5) + 1;
random3 = rand.nextInt(5) + 1;
System.out.println("Result: " + fruits[random1] + " " + fruits[random2] + " " + fruits[random3]);
if(fruits[random1] == Fruit)
{
currentWinning = currentWinning + currentBet;
}
if(fruits[random2] == Fruit)
{
currentWinning = currentWinning + currentBet;
}
if(fruits[random3] == Fruit)
{
currentWinning = currentWinning + currentBet;
}
if(fruits[random1] != Fruit && fruits[random2] != Fruit && fruits[random3] != Fruit)
{
System.out.println("None of the randomly selected words matched your selection. You won $0");
Bank = Bank - currentBet;
currentBet = 0;
}
if(currentWinning > 0)
{
System.out.println("You have won $" + currentWinning);
Bank = Bank + currentWinning;
currentWinning = 0;
}
PrintWriter out = new PrintWriter("input.txt");
out.println(Bank);
out.close();
System.out.println("Do you want to continue playing (y/Y) or quit (n/N):");
status = scan.next().charAt(0);
while(status != 'y' && status != 'n' && status != 'N' && status != 'Y')
{
System.out.println("Invaild choice!!! Please enter y or Y to play, n or N to quit: Do you want to \n continue playing (y/Y) or quit (n/N): ");
status = scan.next().charAt(0);
}
if(status == 'n' || status == 'N'){
System.out.println("Thank you for choosing Carter Collins' Slot Machine! Good bye!");
}
}
}
}
there is a run time exception file not found in line :7 that means the location which you have given in line :7 is not available in computer directory.
please ensure first that the file should be available.
Exception in thread "main"
java.io.FileNotFoundException: C:\Users\Carter
Collins\Downloads\input.txt (The system cannot find the path
specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown
Source)
at java.util.Scanner.<init>(Unknown
Source)
at Main.main(Main.java:15)
Get Answers For Free
Most questions answered within 1 hours.