Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: New to JAVA, problem with first code, need to know how to fix.

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation 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


  2. #2
    Junior Member
    Join Date
    Jan 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation 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

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Please anyone can you fix my Code??
    By java_rookie in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 6th, 2012, 09:59 AM
  2. How do i fix my code?
    By beebee007 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 14th, 2012, 10:49 AM
  3. Help me to fix this code
    By rcbandit2 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 31st, 2011, 02:30 PM
  4. What do i have to do to fix my code?
    By kram in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 07:13 AM
  5. Java Image Problem! Probably easy fix!
    By TrivialFate in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 14th, 2011, 07:12 AM