Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Error: Could not find or load main class Program

    New to programming, this is an assignment I am working on. I have hit a wall, and not sure where to go from here. Continue to get the same error Could not find or load main class Program. Any assistance will be greatly appreciated.

    Code:

    Write a console program that:
    Uses a while loop to perform the following steps:
    Prompt the user to kbInput two integers: firstNumber and secondNumber where secondNumber is at least 10 greater
    than firstNumber, both numbers are positive integers, and secondNumber is less than 1000.
    Verify that the user entered acceptable numbers, and if not, provide error feedback and prompt them again.
    Output all results to a file in the same directory as the program, placing an appropriate
    label between each section of output. Note that your program must be able to run repeatedly overwriting
    the file from the previous run.
    Output all odd numbers between firstNumber and secondNumber inclusive, one number per line.
    Output the sum of all numbers between firstNumber and secondNumber exclusive.
    Uses a for loop to perform the following steps:
    Continue writing to the same file as before.
    Write a label as before.
    Output all numbers from secondNumber to firstNumber in a single line with commas separating the numbers.
    Write the date and time as the last line in the file in the format yyyy-mm-dd hh:mm:ss.

    */
    PHP Code:
    import java.util.*;
    import java.io.*;
    import java.text.SimpleDateFormat;

    public class 
    Assignment2 {

        public static 
    void main(String[] argsthrows IOException {
            
    Scanner kbInput = new Scanner(System.in);
            
    int firstNumber 0secondNumber 0;
            
    boolean valid false;
            while (!
    valid) {
                
    valid false;
                try {
                    
    System.out.print("Enter first number :");
                    
    firstNumber kbInput.nextInt();

                    if (
    firstNumber 0) {
                        
    System.out.println("Enter positive number ");
                    } else if (
    firstNumber 989) {
                        
    System.out.println("Enter number less than 900");
                    } else {
                        
    valid true;
                    }
                } catch (
    Exception e) {
                    
    System.out.println("Enter numeric value");
                    
    kbInput = new Scanner(System.in);
                }
            }
            
    valid false;
            while (!
    valid) {
                try {
                    
    valid false;
                    
    System.out.print("Enter second number :");
                    
    secondNumber kbInput.nextInt();
                    if (
    secondNumber 0) {
                        
    System.out.println("Enter positive number ");
                    } else if (
    secondNumber 1000) {
                        
    System.out.println("Enter number less than 1000");
                    } else if ((
    secondNumber firstNumber) < 10) {
                        
    System.out.println("Second number should be at least 10 greater than first number");
                    } else {
                        
    valid true;
                    }
                } catch (
    Exception e) {
                    
    System.out.println("Enter numeric value");
                    
    kbInput = new Scanner(System.in);
                }
            }
            
    FileWriter fileWriter = 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 firstNumber and secondNumber inclusive, one number per line.
            
    fileWriter.write("1. All odd numbers between firstNumber and secondNumber inclusive");
            
    fileWriter.write(System.getProperty("line.separator"));
            for (
    int i firstNumber<= secondNumberi++) {
                if (
    != 0) {
                    
    fileWriter.write("");
                    
    fileWriter.write(System.getProperty("line.separator"));
                }
            }
            
    //Output the sum of all numbers between firstNumber and secondNumber exclusive.
            
    fileWriter.write("2. The sum of all numbers between firstNumber and secondNumber exclusive");
            
    fileWriter.write(System.getProperty("line.separator"));
            
    int sum 0;
            for (
    int i firstNumber 1secondNumberi++) {
                
    sum += i;
            }
            
    fileWriter.write(sum "");
            
    fileWriter.write(System.getProperty("line.separator"));
            
    //Output all numbers from secondNumber to firstNumber in a single line with commas separating the numbers.
            
    fileWriter.write("3. All numbers from secondNumber to firstNumber in a single line with commas separating the numbers");
            
    fileWriter.write(System.getProperty("line.separator"));
            for (
    int i firstNumber<= secondNumberi++) {
                
    fileWriter.write(",");
            }
            
    fileWriter.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.
            
    fileWriter.write("4. The date and time as the last line in the file in the format yyy-mm-dd hh:mm:ss");
            
    fileWriter.write(System.getProperty("line.separator"));
            
    fileWriter.write((new SimpleDateFormat("yyyy-mm-dd hh:mm:ss")).format(new Date()));
            
    System.out.println("File Assignment2.txt written");
            
    fileWriter.close();
        }

    Last edited by Fou-Lu; 03-14-2013 at 10:12 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Overall it looks fine for me, and without any modifications I can run it to give me this as a result:
    Code:
    1. All odd numbers between firstNumber and secondNumber inclusive
    5
    7
    9
    11
    13
    15
    2. The sum of all numbers between firstNumber and secondNumber exclusive
    90
    3. All numbers from secondNumber to firstNumber in a single line with commas separating the numbers
    5,6,7,8,9,10,11,12,13,14,15,
    4. The date and time as the last line in the file in the format yyy-mm-dd hh:mm:ss
    2013-09-14 03:09:19
    Which aside from the date format at the end being incorrect (it should either be 15:09 or 03:09 PM), looks good.

    Is the problem when you are trying to compile / run the program? What's the full error you are getting? One thing to watch for are both the package naming when calling / compiling the java, as well as the file extensions (don't provide the extensions).

    Edit:
    BTW, on a side note, the date/time does actually match exactly what the requirements are. So you may want to confirm / verify that; I'd suggest it should be either 'HH:mm:ss' or 'hh:mm:ss a'
    Last edited by Fou-Lu; 03-14-2013 at 10:17 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •