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: problem with my class

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default problem with my class

    I have a question about my code I'm trying for the exercise.

    It says "Error, cannot load ValidatedInvoiceApp." What's wrong?

    package java;
    import java.text.NumberFormat;
    import java.util.Scanner;
     
    public class ValidatedInvoiceApp
    {
        public static void main(String[] args)
        {
            Scanner sc = new Scanner(System.in);
            String choice = "y";
     
     
            while (!choice.equalsIgnoreCase("n"))
            {
     
                boolean isValid = false;
                while(isValid == false){
     
                // get the input from the user
                System.out.print("Enter customer type (r/c): ");
                 String customerType = "r, c";
                 if (sc.hasNext("r,c"))
                {
     
                    isValid = true;
                }else
                    {
                        System.out.println("error! Invalid number.Try again.");
                    }
                    sc.nextLine();//discard anything on the line
     
     
                System.out.print("Enter subtotal:   ");
                double subtotal = sc.nextDouble();
     
     
     
     
     
                // get the discount percent
                double discountPercent = 0;
                if (customerType.equalsIgnoreCase("R"))
                {
                    if (subtotal < 100) {
                        discountPercent = 0;
                    }
                    else if (subtotal >= 100 && subtotal < 250) {
                        discountPercent = .1;
                    }
                    else if (subtotal >= 250) {
                        discountPercent = .2;
                    }
                }
                else if (customerType.equalsIgnoreCase("C"))
                {
                    if (subtotal < 250) {
                        discountPercent = .2;
                    }
                    else {
                        discountPercent = .3;
                    }
                }
                else {
                    discountPercent = .1;
                }
     
                // calculate the discount amount and total
                double discountAmount = subtotal * discountPercent;
                double total = subtotal - discountAmount;
     
                // format and display the results
                NumberFormat currency = NumberFormat.getCurrencyInstance();
                NumberFormat percent = NumberFormat.getPercentInstance();
                System.out.println(
                    "Discount percent: " + percent.format(discountPercent) + "\n" +
                    "Discount amount:  " + currency.format(discountAmount) + "\n" +
                    "Total:            " + currency.format(total) + "\n");
     
                // see if the user wants to continue
                System.out.print("Continue? (y/n): ");
                choice = sc.next();
                System.out.println();
     
                    }
                }
     
     
           }
     
    }


  2. #2
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem with my class

    Now it's giving me errors I don't understand.

    java.lang.SecurityException: Prohibited package name: java
    at java.lang.ClassLoader.preDefineClass(ClassLoader.j ava:649)
    at java.lang.ClassLoader.defineClass(ClassLoader.java :785)
    at java.security.SecureClassLoader.defineClass(Secure ClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader .java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader. java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:4 23)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 56)
    at sun.launcher.LauncherHelper.checkAndLoadMain(Launc herHelper.java:480)
    Exception in thread "main" Java Result: 1




    package java;
    import java.text.NumberFormat;
    import java.util.Scanner;
     
    public class ValidatedInvoiceApp
    {
        public static void main(String[] args)
        {
            Scanner sc = new Scanner(System.in);
            String choice = "y";
     
     
            while (!choice.equalsIgnoreCase("n"))
            {
                String customerType = "r, c";
                System.out.print("Enter customer type (r/c): ");
     
     
                 if (sc.hasNext(customerType)) 
                 {
                     customerType = sc.next();
                 }else
                 {
                     sc.nextLine();//discard anything on the line
                     System.out.println("error! Invalid number.Try again.");
                     continue;
                 }
     
     
                  System.out.print("Enter subtotal:   ");
                    double subtotal = sc.nextDouble();
     
     
     
     
     
     
                // get the discount percent
                double discountPercent = 0;
                if (customerType.equalsIgnoreCase("R"))
                {
                    if (subtotal < 100) {
                        discountPercent = 0;
                    }
                    else if (subtotal >= 100 && subtotal < 250) {
                        discountPercent = .1;
                    }
                    else if (subtotal >= 250) {
                        discountPercent = .2;
                    }
                }
                else if (customerType.equalsIgnoreCase("C"))
                {
                    if (subtotal < 250) {
                        discountPercent = .2;
                    }
                    else {
                        discountPercent = .3;
                    }
                }
                else {
                    discountPercent = .1;
                }
     
                // calculate the discount amount and total
                double discountAmount = subtotal * discountPercent;
                double total = subtotal - discountAmount;
     
                // format and display the results
                NumberFormat currency = NumberFormat.getCurrencyInstance();
                NumberFormat percent = NumberFormat.getPercentInstance();
                System.out.println(
                    "Discount percent: " + percent.format(discountPercent) + "\n" +
                    "Discount amount:  " + currency.format(discountAmount) + "\n" +
                    "Total:            " + currency.format(total) + "\n");
     
                // see if the user wants to continue
                System.out.print("Continue? (y/n): ");
                choice = sc.next();
                System.out.println();
     
                 } 
     
                }
     
     
                }

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem with my class

    nvm guys, I apologize. i messed with it enough and figured it out. i couldn't use a package with java as the name.

    package newstuff;
    import java.text.NumberFormat;
    import java.util.Scanner;
     
    public class tryValidatedInvoiceApp
    {
        public static void main(String[] args)
        {
            Scanner sc = new Scanner(System.in);
            String choice = "y";
     
     
            while (!choice.equalsIgnoreCase("n"))
            {
     
                System.out.print("Enter customer type (r/c): ");
                String customerType = sc.next();
     
                 if (customerType.equals("r"))
     
                 {
     
     
     
                  System.out.print("Enter subtotal:   ");
                    double subtotal = sc.nextDouble();
     
     
     
     
     
     
                // get the discount percent
                double discountPercent = 0;
                 if (customerType.equalsIgnoreCase("C"))
                {
                    if (subtotal < 250) {
                        discountPercent = .2;
                    }
                    else {
                        discountPercent = .3;
                    }
                }
                else {
                    discountPercent = .1;
                }
     
                // calculate the discount amount and total
                double discountAmount = subtotal * discountPercent;
                double total = subtotal - discountAmount;
     
                // format and display the results
                NumberFormat currency = NumberFormat.getCurrencyInstance();
                NumberFormat percent = NumberFormat.getPercentInstance();
                System.out.println(
                    "Discount percent: " + percent.format(discountPercent) + "\n" +
                    "Discount amount:  " + currency.format(discountAmount) + "\n" +
                    "Total:            " + currency.format(total) + "\n");
     
                // see if the user wants to continue
                System.out.print("Continue? (y/n): ");
                choice = sc.next();
                System.out.println();
     
                 }else if(customerType.equals("c"))
     
                 {
     
     
     
                  System.out.print("Enter subtotal:   ");
                    double subtotal = sc.nextDouble();
     
     
     
     
     
     
                // get the discount percent
                double discountPercent = 0;
                if (customerType.equalsIgnoreCase("R"))
                {
                    if (subtotal < 100) {
                        discountPercent = 0;
                    }
                    else if (subtotal >= 100 && subtotal < 250) {
                        discountPercent = .1;
                    }
                    else if (subtotal >= 250) {
                        discountPercent = .2;
                    }
                }
     
     
                // calculate the discount amount and total
                double discountAmount = subtotal * discountPercent;
                double total = subtotal - discountAmount;
     
                // format and display the results
                NumberFormat currency = NumberFormat.getCurrencyInstance();
                NumberFormat percent = NumberFormat.getPercentInstance();
                System.out.println(
                    "Discount percent: " + percent.format(discountPercent) + "\n" +
                    "Discount amount:  " + currency.format(discountAmount) + "\n" +
                    "Total:            " + currency.format(total) + "\n");
     
                // see if the user wants to continue
                System.out.print("Continue? (y/n): ");
                choice = sc.next();
                System.out.println();
     
     
     
     
     
     
                 } else
    		             {
    		                 sc.nextLine();//discard anything on the line
    		                 System.out.println("error! Invalid number.Try again.");
                     continue;}
     
                }
     
     
                }
     
    }

Similar Threads

  1. Replies: 5
    Last Post: October 18th, 2012, 01:43 PM
  2. Problem with running random class
    By Gizken in forum What's Wrong With My Code?
    Replies: 19
    Last Post: April 27th, 2012, 05:50 AM
  3. Inheritance; Problem with Test class
    By Charlie.beat in forum What's Wrong With My Code?
    Replies: 19
    Last Post: April 8th, 2012, 10:59 PM
  4. I am Facing problem in Scanner Class
    By snithishkumar in forum Java SE APIs
    Replies: 2
    Last Post: October 11th, 2011, 09:39 AM
  5. problem with data access when a class call another class
    By ea09530 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2010, 05:20 PM