In: Computer Science
I don't know why my java code is not running this error code pops up --> Error: Could not find or load main class DieRoll Caused by: java.lang.ClassNotFoundException: DieRoll.
Code below:
import java.util.Random;
import java.util.Scanner;
public class Assignment {
public static void combineStrings() {
String school = "Harvard";
String city = "Boston, MA";
int stringLen;
String upper, changeChar, combine;
stringLen = school.length();
System.out.println(school+" contains "+stringLen+" characters." );
upper = school.toUpperCase();
System.out.println(upper);
changeChar = upper.replace("A", "*");
combine = school +" "+city;
System.out.println("The final string is "+combine );
}
public static void dieRoll()
{
Scanner scanner = new Scanner(System.in);
Random random = new Random();
int sides, n1, n2, n3;
System.out.print("How many sides? ");
sides = scanner.nextInt();
n1 = random.nextInt(sides)+1;
n2 = random.nextInt(sides)+1;
n3 = random.nextInt(sides)+1;
System.out.println("First Roll\t="+n1);
System.out.println("Second Roll\t="+n2);
System.out.println("Third Roll\t="+n3);
System.out.println("Die Total\t="+(n1+n2+n3));
System.out.println("Average Roll\t="+((n1+n2+n3)/3.0));
}
public static void candy()
{
int numCarton=0,candyBars;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of candy bars : ");
candyBars = scanner.nextInt();
do
{
numCarton++;
candyBars = candyBars-24;
}while(candyBars > 0);
System.out.println("Number of Cartons needed = "+numCarton);
}
public static void main(String[] args) {
combineStrings();
dieRoll();
candy();
}
}
The code is absolutely fine but your error is Error: Could not find or load main class DieRoll Caused by: java.lang.ClassNotFoundException: DieRoll.
Then follow the below steps:
1.save the java code as Assignment.java then it will work fine.
This because if you won't save as Assignment.java it can't find the main class.
SOURCE CODE:
import java.util.Random;
import java.util.Scanner;
public class Assignment {
public static void combineStrings() {
String school = "Harvard";
String city = "Boston, MA";
int stringLen;
String upper, changeChar, combine;
stringLen = school.length();
System.out.println(school+" contains "+stringLen+" characters." );
upper = school.toUpperCase();
System.out.println(upper);
changeChar = upper.replace("A", "*");
combine = school +" "+city;
System.out.println("The final string is "+combine );
}
public static void dieRoll()
{
Scanner scanner = new Scanner(System.in);
Random random = new Random();
int sides, n1, n2, n3;
System.out.print("How many sides? ");
sides = scanner.nextInt();
n1 = random.nextInt(sides)+1;
n2 = random.nextInt(sides)+1;
n3 = random.nextInt(sides)+1;
System.out.println("First Roll\t="+n1);
System.out.println("Second Roll\t="+n2);
System.out.println("Third Roll\t="+n3);
System.out.println("Die Total\t="+(n1+n2+n3));
System.out.println("Average Roll\t="+((n1+n2+n3)/3.0));
}
public static void candy()
{
int numCarton=0,candyBars;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of candy bars : ");
candyBars = scanner.nextInt();
do
{
numCarton++;
candyBars = candyBars-24;
}while(candyBars > 0);
System.out.println("Number of Cartons needed = "+numCarton);
}
public static void main(String[] args) {
combineStrings();
dieRoll();
candy();
}
}