New to JAVA, problem with first code, need to know how to fix.
import java.util.*;
public class A1RE5161843 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter length of rectangle :");
double length = input.nextDouble();
System.out.print("Enter width of rectangle :");
double width = input.nextDouble();
System.out.println("Area: "+(length * width));
System.out.println("Perimeter: "+((2*length)+(2*width)));
}
}
ERROR
Main.java:12: class A1RE5161843 is public, should be declared in a file named A1RE5161843.java
public class A1RE5161843 {
^
1 error
Code not building due to error I do not know how to resolve.
import java.util.*;
import java.io.*;
import java.text.SimpleDateFormat;
public class Assignment2 {
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
int firstNum = 0, secondNum = 0;
boolean valid = false;
while (!valid) {
System.out.print("Enter first number :");
firstNum = input.nextInt();
if (firstNum < 0) {
System.out.println("Enter positive number ");
} else if (firstNum > 1000) {
System.out.println("Enter number less than 1000");
} else {
valid = true;
}
}
valid = false;
while (!valid) {
System.out.print("Enter second number :");
secondNum = input.nextInt();
if (secondNum < 0) {
System.out.println("Enter positive number ");
} else if (secondNum > 1000) {
System.out.println("Enter number less than 1000");
} else if ((secondNum - firstNum) < 10) {
System.out.println("Second number should be at least 10 greater than first number");
} else {
valid = true;
}
}
FileWriter fw = new FileWriter(new File("Assignment2.txt"));
//Use a for loop to perform the following steps:
//Continue writing to the same file as before.
//Write ad label as before.
//Output all odd numbers between firstNum and secondNum inclusive, one number per line.
fw.write("1. All odd numbers between firstNum and secondNum inclusive");
fw.write(System.getProperty("line.separator"));
for (int i = firstNum; i <= secondNum; i++) {
if (i % 2 != 0) {
fw.write(i + "");
fw.write(System.getProperty("line.separator"));
}
}
//Output the sum of all numbers between firstNum and secondNum exclusive.
fw.write("2. The sum of all numbers between firstNum and secondNum exclusive");
fw.write(System.getProperty("line.separator"));
int sum = 0;
for (int i = firstNum + 1; i < secondNum; i++) {
sum += i;
}
fw.write(sum + "");
fw.write(System.getProperty("line.separator"));
//Output all numbers from secondNum to firstNum in a single line with commas separating the numbers.
fw.write("3. All numbers from secondNum to firstNum in a single line with commas separating the numbers");
fw.write(System.getProperty("line.separator"));
for (int i = firstNum; i <= secondNum; i++) {
fw.write(i + ",");
}
fw.write(System.getProperty("line.separator"));
//Write the date and time as the last line in the file in the format yyy-mm-dd hh:mm:ss.
fw.write("4. The date and time as the last line in the file in the format yyy-mm-dd hh:mm:ss");
fw.write(System.getProperty("line.separator"));
fw.write((new SimpleDateFormat("yyyy-mm-dd hh:mm:ss")).format(new Date()));
System.out.println("File Assignment2.txt written");
fw.close();
}
}
clone edit download copy to clipboard private user's help delete
compilation info hide
Main.java:36: class Assignment2 is public, should be declared in a file named Assignment2.java
public class Assignment2 {
^
1 error
Re: New to JAVA, problem with first code, need to know how to fix.
The problem is pretty self-explanatory. Your class needs to be in a file with that awful name. What do you have it saved as now? Looks like Main.java. The other, more sane thing to do would be to just rename your class to match the file name.
--- Update ---
I've merged your two threads because they contain the same problem. Java classes need to be in a file with the same name as the class.