0

while trying to do some exercises to learn java i wrote a simple program to calculate the count of worked hours and give the deserved salary.

package horulycalc;

import java.util.Arrays;
import java.util.Scanner;

public class HorulyCalc {

    public static void main(String[] args) {
        Scanner param = new Scanner(System.in);
        String [] titles  = {"Developer","Designer","Data Entry","Manager","CEO"};
        System.out.println("Hello and welcome to Job Counter");
        System.out.println("Please enter you Job Title");
        String title = param.nextLine();
        while(!Arrays.asList(titles).contains(title)){
            System.out.println("Please enter valid Job Title"); 
            title = param.nextLine();
            if(Arrays.asList(titles).contains(title)){
                System.out.println("Please enter your Hours for this week :");
                String count = param.nextLine();
                System.out.printf("Your Salary is : $ %f",HoursMath(HourRate(title),Integer.parseInt(count)));
                break ;
            }
        }
    }

    public static int HourRate(String jobTitle){
        int rate = 0;
        switch(jobTitle){
            case "Developer":
                rate = 10 ;
                break;
            case "Designer":
                rate = 8 ;
                break ;
            case "Data Entry":
                rate = 6;
                break ;
            case "Manager":
                rate = 15 ;
                break;
            case "CEO":
                rate = 36;
                break ;
        }
        return rate ;
    }

    public static float HoursMath(int rate ,int count){
        float total ;
        total = rate * count ;
        return total ;
    }
}

the program run fine if i added a wrong Job title for the first time i mean an input which is not included within the job titles array.

when i enter a valid Job Title in the first time for example "CEO" the program break and netbeans how it as its finished

4
  • 1
    I suggest you talk to the person who wrote it and ask them why they did that. ;) If you want the hours calculation after a valid title is entered, put it after the loop. Commented Jul 27, 2014 at 17:48
  • 1
    @PeterLawrey should i talk to myself ? Commented Jul 27, 2014 at 17:51
  • 1
    @Dr.Neo - talking to yourself usually helps.. :). Commented Jul 27, 2014 at 17:53
  • Yes, ;) You can move the if statement outside the loop and simplify the code. i.e. you want to calculate the hours after you have a valid title, I assume, so put that after it. Commented Jul 27, 2014 at 18:01

2 Answers 2

2

That's because you are not doing anything when the user first enters a valid value (which is in your array..)

    System.out.println("Please enter you Job Title");
    String title = param.nextLine(); // read title..
    while(!Arrays.asList(titles).contains(title)){ // while title is not present in array.
     }
 // nothing here--> what if title is present in the array / list?
  //So,Put this code here :. The below lines of code will be executed only hen you have a valid entry.
    System.out.println("Please enter your Hours for this week :");
    String count = param.nextLine();
    System.out.printf("Your Salary is : $ %f",HoursMath(HourRate(title),Integer.parseInt(count)));
            break 
Sign up to request clarification or add additional context in comments.

4 Comments

His code does actually work. I think the problem is that it breaks after a single iteration with a "correct" job title input. Possibly I'm not understanding his question. But running the code with "CEO" prints "Your Salary is : $ 360.000000".
@DaniëlKnippers - No. he is using a while loop. So, if he enters "CEO" or any valid entry for the first time, the while will not be executed.
oops my bad, I first entered an invalid entry :-) he should just put his calculation logic after the while.
@DaniëlKnippers - ya.. putting his calculation logic outside the while will help..
1

Since your while loop already tests for valid titles, you shouldn't test it inside the loop too.

This is simpler :

while(!Arrays.asList(titles).contains(title)){
    System.out.println("Please enter valid Job Title"); 
    title = param.nextLine();
}
System.out.println("Please enter your Hours for this week :");
String count = param.nextLine();
System.out.printf("Your Salary is : $ %f",HoursMath(HourRate(title),Integer.parseInt(count)));

You don't have to break from the loop this way. Once you are out of the loop, you know you have a valid title.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.