Results 1 to 14 of 14
  1. #1
    nightrise420 is offline Member
    Join Date
    Sep 2010
    Posts
    16
    Rep Power
    0

    Default Exeption in thread "main"....

    exception in thread "main" java.lang.noclassdefounderror: assign 4.
    i get this error message. I'm trying to write a program that promps user for item price and with the input i sum it up and display it using system.out.println. This is what i have so far. thank you


    import java.util.Scanner;
    public class Assign4
    {
    public static void determineClassAverage()
    {
    Scanner input = new Scanner( System.in );

    double total;
    double itemCounter;
    double price;
    double average;
    double commission;

    total = 0;
    itemCounter = 0;

    System.out.print( "Enter grade or -1 to quit: " );
    price = input.nextInt();

    while ( price != -1 )
    {
    total = total + price;
    itemCounter = itemCounter + 1;

    System.out.print( "Enter item value or -1 to quit: " );
    price = input.nextInt();

    average = (double) total / itemCounter;
    commission = total * .9;

    System.out.printf( "\nItems sold: \n",
    itemCounter);
    System.out.printf( "\nTotal amount: \n", total );
    System.out.printf( "\nCommission: \n",
    commission);
    System.out.printf( "Class average is %.2f\n", average );
    }
    }
    }

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    i just started your class replacing the code from the last printf with

    Java Code:
                System.out.printf("Class average is %.2f\n", average);
            }
        }
        public static void main(String[] args) {
            new Assign4().determineClassAverage();
        }
    }

    and i could start the method determineClassAverage without errors.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,010
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by nightrise420 View Post
    exception in thread "main" java.lang.noclassdefounderror: assign 4.
    The name of the class is Assign4 (with a capital A) not assign4 (lowercase a).

    kind regards,

    Jos

    ps. I guess that the space in 'assign 4' is a forum software hiccup.

  4. #4
    nightrise420 is offline Member
    Join Date
    Sep 2010
    Posts
    16
    Rep Power
    0

    Default

    when i run it, it only promps me twice and the program ends with the information.
    Also the result number of items sold doesn't appear for some reason.
    And my System.out.printf( "\nEarnings: $%.2f", earnings) results in this message.
    "Earnings: $310.70Enter item value or -1 to quit: "

  5. #5
    nightrise420 is offline Member
    Join Date
    Sep 2010
    Posts
    16
    Rep Power
    0

    Default

    Nevermind. I managed to fix it. Now only problem is the sum of input user doesn't add up correctly. And there is no item sold. Here's my code now
    //CIS 226 Assignment 4
    //Jeff Chung - September 27, 2010
    import java.util.Scanner;

    public class Assign4
    {
    public static void sales()
    {
    Scanner input = new Scanner( System.in );

    double total;
    double itemCounter;
    double price;
    double earnings;
    double commission;

    total = 0;
    itemCounter = 0;
    earnings = 0;
    commission = 0;

    System.out.print( "Enter grade or -1 to quit: " );
    price = input.nextInt();

    while ( price != -1 )
    {
    System.out.print( "Enter item value or -1 to quit: " );
    price = input.nextInt();
    total = price + total;
    itemCounter = itemCounter + 1;
    commission = total * .09;
    earnings = commission + 200;
    }

    System.out.printf( "\nItems sold: ", itemCounter);
    System.out.printf( "\nTotal amount: $%.2f", total );
    System.out.printf( "\nCommission: $%.2f", commission);
    System.out.printf( "\nEarnings: $%.2f", earnings);

    }
    public static void main(String[] args) {
    new Assign4().sales();

    }
    }

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,590
    Rep Power
    23

    Default

    problem is the sum of input user doesn't add up correctly. And there is no item sold.
    Could you copy and paste here the contents of the console window from when you execute the program that shows what it does?

    What does the code do with the -1 before it exits the loop?

  7. #7
    nightrise420 is offline Member
    Join Date
    Sep 2010
    Posts
    16
    Rep Power
    0

    Default

    As you can see the value of the item doesnt quite add up correctly. I think when i type -1 to quit, It takes the -1 and sums it up. I can't get the -1 to not add up correctly.

    Enter grade or -1 to quit: 1
    Enter item value or -1 to quit: 2
    Enter item value or -1 to quit: 3
    Enter item value or -1 to quit: 4
    Enter item value or -1 to quit: -1
    Items sold: 4
    Total amount: $8.00
    Commission: $0.72
    Earnings: $200.72

  8. #8
    nightrise420 is offline Member
    Join Date
    Sep 2010
    Posts
    16
    Rep Power
    0

    Default

    So the problem is when the user is asked for the first input before it is sent to the while statement, the input is lost and doesn't add up to the total amount.

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,590
    Rep Power
    23

    Default

    What does the code do with the -1 that was entered before it exits the loop?

  10. #10
    nightrise420 is offline Member
    Join Date
    Sep 2010
    Posts
    16
    Rep Power
    0

    Default

    I think it takes -1 as a price input and adds it the total amount.

  11. #11
    nightrise420 is offline Member
    Join Date
    Sep 2010
    Posts
    16
    Rep Power
    0

    Default

    import java.util.Scanner;

    public class Assign4
    {
    public void sales() {
    Scanner input = new Scanner( System.in );

    int itemCounter;
    double total;
    double price;
    double earnings;
    double commission;

    total = 0;
    itemCounter = 0;
    earnings = 0;
    commission = 0;

    System.out.print( "Enter item value or -1 to quit: " );
    price = input.nextInt();
    total = price + total;

    while ( price != -1 )
    {
    System.out.print( "Enter item value or -1 to quit: " );
    price = input.nextInt();
    total = price + total;
    itemCounter = itemCounter + 1;
    commission = total * .09;
    earnings = commission + 200;
    }
    if ( itemCounter != 0)
    {
    System.out.printf( "Items sold: %d\n", itemCounter );
    System.out.printf( "Total amount: $%.2f\n", total );
    System.out.printf( "Commission: $%.2f\n", commission);
    System.out.printf( "Earnings: $%.2f\n", earnings);
    }
    }

    public static void main(String[] args) {
    new Assign4().sales(); }
    }

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,590
    Rep Power
    23

    Default

    Does that account for your problem?
    You need to test for the -1 and exit the loop before you use it as data.

  13. #13
    nightrise420 is offline Member
    Join Date
    Sep 2010
    Posts
    16
    Rep Power
    0

    Default

    Yes precisely. Now if i can only find out how to write it haha

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,590
    Rep Power
    23

    Default

    One way out of a loop is to use a break statement.
    Another is to structure the loop so you ask for the value at the end of the loop just before the loop will test it to see if its to continue.

Similar Threads

  1. Replies: 5
    Last Post: 06-22-2010, 03:51 PM
  2. Replies: 1
    Last Post: 11-20-2009, 07:58 PM
  3. Replies: 6
    Last Post: 07-16-2009, 03:30 PM
  4. Replies: 1
    Last Post: 07-16-2009, 10:35 AM
  5. Replies: 1
    Last Post: 08-07-2007, 07:10 AM

Posting Permissions

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