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 18 of 18

Thread: New to JAVA need some help

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Location
    Houston, TX
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default New to JAVA need some help

    Im currently in a JAVA class in college and we had to build a program that calculated commission. i think im in the ball park here with my code but when i run it i cant get past the second question. Im running it on netbeans can anyone see what i did wrong here? It has to be near the error message because i cant get past that point on the code. Now i do have a string that does not allow letters to be input into the second question im not sure if that is the problem or not either.


    package salesperson;
     
    import java.util.Scanner;
     
    public class Salesperson {
     
        public static void main(String[] args) {
            final double salary = 40000.00;
            final double commissionRate = 0.05;
            final double pricePerSale = 250.00;
     
            boolean addperson = true;
     
            Scanner sc = new Scanner(System.in);
            System.out.println ("Calculate your commission and total pay");
            System.out.println(" ");
     
            while (addperson == true){
                double total = 0;
                int currentNumber;
                String currentString;
                String name;
                String yesNo;
                boolean exit;
                boolean isNumber;
     
                System.out.println ("Enter name: ");
                name = sc.next();
                System.out.println(" ");
     
            do {
                System.out.print ("Enter amount of sales: ");
                currentString = sc.next();
                isNumber = isANumber(currentString);
                 if (isNumber == true) {
                     currentNumber = Integer.parseInt(currentString);
                     if (currentNumber > 0) 
                    {
                         total += currentNumber * pricePerSale;
                      }
            } {
                System.out.println("ERROR: only enter whole numbers");
                 }
     
                     }
            while (isNumber == false);
     
            System.out.println("");
            total = total * commissionRate;
            System.out.print (name + "Earned");
            System.out.printf("$%2.2f",total);
            System.out.println(" in commissions and Earned ");
            total += salary;
            System.out.printf("$%2.2f",total);
            System.out.println(" this year");
            System.out.println("");
     
             do {
                 System.out.println("Do you want to check another name y/n : ");
                 yesNo = sc.next();
                 System.out.println("");
                 switch (yesNo) {
                     case "n":
                     case "N":
                         addperson = false;
                         exit = true;
                         break;
                     case "y":
                     case "Y":
                         exit = true;
                         break;
                     default:
                         System.out.println ("Enter y or n only");
                         exit = false;
                 }      
             }while (exit == false);
          }
      }
     
     public static boolean isANumber (String s){
        int i =0;
        boolean valid = true;
        if (s.length()==0);
        valid = false;
     
        while (i < s.length()){
            if (!Character.isDigit(s.charAt(i))){
               valid =false;
                break;
            }
            i++;
        }
        return valid;
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: New to JAVA need some help

    it i cant get past the second question.
    Do you get an error message? Please copy the full text of the error message and paste it here.
    Copy all of the console that shows what was printed and what the user entered.

    On Windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Location
    Houston, TX
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New to JAVA need some help

    No, no error message just to one i made up in the code. Thats the error message i can not get past i think i have it looped wrong. thats all the code i got im also running the program on NetBeans

    this is the error message that i made and is in the above code
     {
                System.out.println("ERROR: only enter whole numbers");
                 }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: New to JAVA need some help

    That's not quite what I think of an error message.

    Can you post the console's contents from when the program is executed to show what you are talking about?

    Some of the usage of {}s is unusual and could be causing you problems.

    You need to use the javac command's -Xlint option for warnings. For example:

    D:\Java\jdk1.7.0.7\bin\javac.exe -Xlint Salesperson.java
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Location
    Houston, TX
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New to JAVA need some help

    this is what it looks like when i run it
    ===================================
    run:
    Calculate your commission and total pay

    Enter name:
    mike

    Enter amount of sales: 12
    ERROR: only enter whole numbers
    Enter amount of sales: 1
    ERROR: only enter whole numbers
    Enter amount of sales: 3
    ERROR: only enter whole numbers
    Enter amount of sales: 4
    ERROR: only enter whole numbers
    Enter amount of sales: 5
    ERROR: only enter whole numbers
    Enter amount of sales: -1
    ERROR: only enter whole numbers
    Enter amount of sales: a
    ERROR: only enter whole numbers
    Enter amount of sales:

    ================================
    see how even if i enter a normal number i still get the error code that i made. I want it to only show that Error code if someone trying to enter a negative number or a letter.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: New to JAVA need some help

    Did you see these from my last post:

    Some of the usage of {}s is unusual and could be causing you problems. Proper formatting makes the code easier to read and understand.

    You need to use the javac command's -Xlint option for warnings. For example:

    D:\Java\jdk1.7.0.7\bin\javac.exe -Xlint Salesperson.java
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: New to JAVA need some help

    Wiz, you may not notice this but you've coded it so it always gives you ERROR: only enter whole numbers.
    Have you thought of using the Math.floor(double n) function? It will only round it to the biggest whole number because it will almost never be just a plain whole number so it rounds it for you in the program, and if it still doesn't work for you just edit your code for us to see.

    I'm sure this is intentional because it doesn't seem done yet but Its forever looping once you reach the second question.

    Edit: Compiler doesn't matter, Eclipse still has your problem

  8. #8
    Junior Member
    Join Date
    Jan 2013
    Location
    Houston, TX
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New to JAVA need some help

    Norm I think i get what you are saying about javac commands but that is not the problem since im Using NetBeans Program to build the code and also to run the code therefor no need for command prompt or javac but i am very confused on the {} symbols im not to sure where to place them. i tried to get rid of a few but then i will get syntax errors for not having a closed loop.

    shadowhoundz where would i enter Math.floor(double n) sorry still very new at this

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: New to JAVA need some help

    that is not the problem
    I think if you were to use the -Xlint option to get the compiler's warning message, it would help you fix part of the problem.

    The poorly positioned {}s make the code hard to read and understand, but that does not change how the code will compile and execute. The compiler doesn't care too much about formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Jan 2013
    Location
    Houston, TX
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New to JAVA need some help

    oh ok ill try to clean up the code {}'s to make it easer to read. what is -xlint and i think one problem is that it keeps looping anyidea how to stop it from looping back to the Error message i have made?

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: New to JAVA need some help

    what is -xlint
    You need to use the javac command's -Xlint option for warnings. For example:

    D:\Java\jdk1.7.0.7\bin\javac.exe -Xlint Salesperson.java

    I have no idea how to tell an IDE how to use that javac option.

    how to stop it from looping back
    What condition in the loop keeps the loop looping? What code sets that condition?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Jan 2013
    Location
    Houston, TX
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New to JAVA need some help

    This is where im having an issue with it just looping the error message at the bottom in the code



    while (addperson == true){
    double total = 0;
    int currentNumber;
    String currentString;
    String name;
    String yesNo;
    boolean exit;
    boolean isNumber;

    System.out.println ("Enter name: ");
    name = sc.next();
    System.out.println(" ");

    do {
    System.out.print ("Enter amount of sales: ");
    currentString = sc.next();
    isNumber = isANumber(currentString);
    if (isNumber == true) {
    currentNumber = Integer.parseInt(currentString);
    if (currentNumber > 0)
    {
    total += currentNumber * pricePerSale;
    }
    } {
    System.out.println("ERROR: only enter whole numbers");
    }

    }
    while (isNumber == false);

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: New to JAVA need some help

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.


    Where controls the value of isNumber?
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Junior Member
    Join Date
    Jan 2013
    Location
    Houston, TX
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New to JAVA need some help

    package salesperson;
     
    import java.util.Scanner;
     
    public class Salesperson {
     
        public static void main(String[] args) 
        {
            final double salary = 40000.00;
            final double commissionRate = 0.05;
            final double pricePerSale = 250.00;
     
            boolean addperson = true;
     
            Scanner sc = new Scanner(System.in);
            System.out.println ("Calculate your commission and total pay");
            System.out.println(" ");
     
            while (addperson == true){
                double total = 0;
                int currentNumber;
                String currentString;
                String name;
                String yesNo;
                boolean exit;
                boolean isNumber;
                boolean valid;
     
                System.out.println ("Enter name: ");
                name = sc.next();
                System.out.println(" ");
     
            do {
                System.out.println ("Enter amount of sales: ");
                currentString = sc.next();
                System.out.println (" ");
                isNumber = isANumber(currentString);
                 if (isNumber == true) 
      {
                     currentNumber = Integer.parseInt(currentString);
                     if (currentNumber > 0) 
        {
                         total += currentNumber * pricePerSale;
           }
             } 
               {
                System.out.println("===========  ERROR: only enter whole numbers  ===========");
                  }
     
                                      }
            while (isNumber == false);
     
            System.out.println("");
            total = total * commissionRate;
            System.out.print (name + "Earned");
            System.out.printf("$%2.2f",total);
            System.out.println(" in commissions and Earned ");
            total += salary;
            System.out.printf("$%2.2f",total);
            System.out.println(" this year");
            System.out.println("");
     
             do {
                 System.out.println("Do you want to check another name y/n : ");
                 yesNo = sc.next();
                 System.out.println("");
                 switch (yesNo) {
                     case "n":
                     case "N":
                         addperson = false;
                         exit = true;
                         break;
                     case "y":
                     case "Y":
                         exit = true;
                         break;
                     default:
                         System.out.println ("Enter y or n only");
                         exit = false;
                 }      
             }       while (exit == false);
          }
      }
     
     public static boolean isANumber (String s){
        int i =0;
        boolean valid = true;
        if (s.length()==0);
        valid = false;
     
        while (i < s.length())
        {
            if (!Character.isDigit(s.charAt(i)))
                  {
                      valid =false;
                      break;
            }
                      i++;
        }
                      return valid;
    }
     
    }

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: New to JAVA need some help

    Have you fixed the problem? I don't see any comments in the last post.

    I'm done for tonight. Back tomorrow.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Junior Member
    Join Date
    Jan 2013
    Location
    Houston, TX
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New to JAVA need some help

    nope still not working and still unsure of what i have done to cause this lol. i guess this happends when you are new to this type of thing. anyone else see whats happening? look at previous post please.

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: New to JAVA need some help

    Try compiling with the javac command using the -xlint option to get useful warning messages.

    What controls the value of isNumber?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: New to JAVA need some help

    I see his problem, he has no values to go to the next question, its forever looping because his variable stays the same