0

Below is my code for one of my small projects. I have written what is required however its either I am missing something or haven't put a valid code to stop the continous looping with the println"Enter your keyword here". Can someone find my fault in this source code. What am I doing wrong here? I keep getting asked "Enter your keyword" question after I choose the exit option.

import java.util.Scanner;

public class JobApplication {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.println("Job Search Portal");
        System.out.println("_ _ _ _ _ _ _ _ _ ");
        System.out.println("name - To Enter Your Name:  ");
        System.out.println("dob - To Enter Your Date of Birth:  ");
        System.out.println("profession - To enter your profession");
        System.out.println("years - To enter the number of years you've workd");
        System.out.println("income - To enter your income from previous employer");

        String keyword = null;
        String name = null;
        String dob = null;
        String profession = null;
        int years = 0;
        long income = 0;
        String exit = null;

        do {
            System.out.println("\nEnter your keyword here");
            keyword = input.next();

            switch (keyword.toLowerCase()) {

                case "name":
                    System.out.println("Please enter your name: ");
                    name = input.next();
                    break;
                case "dob":
                    System.out.println("Please enter your date of birth");
                    dob = input.next();
                    break;
                case "profession":
                    System.out.println("Please enter your profession");
                    profession = input.next();
                    break;
                case "years":
                    System.out.println("Please enter the number of years experience you have");
                    years = input.nextInt();
                    if (years >= 2) {
                        if (years > 40) System.out.println("You are too old ");
                    } else {
                        System.out.println("You are in experienced");
                    }
                    break;

                case "income":
                    System.out.println("Please enter your previous income");
                    income = input.nextLong();
                    break;

                case "exit":
                    System.out.println("The following are your details!");
                    System.out.println("Name: " + name);
                    System.out.println("Date of Birth: " + dob);
                    System.out.println("Profession: " + profession);
                    System.out.println("Experience: " + years + " years");
                    System.out.println("Yearly Salary: K" + income);
                    System.out.println("We will get back to you soon!");
                    System.out.println("Good luck!");break;

                default: System.out.println("Enter a valid choice!");

        }

    }while(true);
  }
}
cssko
  • 3,027
  • 1
  • 18
  • 21

3 Answers3

0

You can use label for loops as described here.

In your example:

mainLoop: do {
//and then
break mainLoop;

However in my opinion changing condition in main loop would be better solution.

Community
  • 1
  • 1
jkordas
  • 155
  • 6
0

The reason why this keeps happening is because you're running it infinitely with your current condition of while(true). You need some condition to exit your while loop. Looking at your code, it looks like you want to stop asking questions after the user types in exit. Change your while condition to this:

while(keyword.toLowerCase().compareTo("exit") != 0);

or (as @Tom pointed out), better yet:

 while (!keyword.toLowerCase().equals("exit"));

This will guarantee that it runs at least once, and will exit when the user types in exit. Cheers!

Ceelos
  • 1,116
  • 2
  • 14
  • 35
  • Why should someone use `.compareTo("exit") != 0`? When you want to check if a String is unequal to another one, then use `while(!keyword.toLowerCase().equals("exit"));` – Tom Mar 14 '16 at 17:27
0

instead of While(true) use flag variable to control the loop.

i have rectify your code.. you can check it out.


import java.util.Scanner;
public class JobApplication {
public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    System.out.println("Job Search Portal");
    System.out.println("_ _ _ _ _ _ _ _ _ ");
    System.out.println("name - To Enter Your Name:  ");
    System.out.println("dob - To Enter Your Date of Birth:  ");
    System.out.println("profession - To enter your profession");
    System.out.println("years - To enter the number of years you've workd");
    System.out.println("income - To enter your income from previous employer");

    String keyword = null;
    String name = null;
    String dob = null;
    String profession = null;
    int years = 0;
    long income = 0;
    String exit = null;

    int flag=0;

    do {
        System.out.println("\nEnter your keyword here");
        keyword = input.next();

        switch (keyword.toLowerCase()) {

            case "name":
                System.out.println("Please enter your name: ");
                name = input.next();
                break;
            case "dob":
                System.out.println("Please enter your date of birth");
                dob = input.next();
                break;
            case "profession":
                System.out.println("Please enter your profession");
                profession = input.next();
                break;
            case "years":
                System.out.println("Please enter the number of years experience you have");
                years = input.nextInt();
                if (years >= 2) {
                    if (years > 40) System.out.println("You are too old ");
                } else {
                    System.out.println("You are in experienced");
                }
                break;

            case "income":
                System.out.println("Please enter your previous income");
                income = input.nextLong();
                break;

            case "exit":
                System.out.println("The following are your details!");
                System.out.println("Name: " + name);
                System.out.println("Date of Birth: " + dob);
                System.out.println("Profession: " + profession);
                System.out.println("Experience: " + years + " years");
                System.out.println("Yearly Salary: K" + income);
                System.out.println("We will get back to you soon!");
                System.out.println("Good luck!");
        flag=1;
        break;

            default: System.out.println("Enter a valid choice!");

    }

}while(flag == 0);
}
}
Tapas Thakkar
  • 845
  • 8
  • 19