0

So below i have an issue that i cant wrap my head around as to why its not working. The first block of java code works exactly how i wanted it to (e.g. set salary amounts to whatever, and if its not in range give an error and repeat the initial question.)

My problem is this in the second block of code I assumed making a boolean again and emulating the above code to check whether or not my inputs were == "", and if not rinse and repeat like before but it doesnt? i wrote it out exactly the same (of course changing the values) but i simply cant get t to function. If anyone can help or point me toward the right path id appreciate it :)

//First Block.....

boolean salaryState = false;
         while (salaryState == false){
             System.out.print("Salary: ");
         salary = (input.nextDouble());     
            if(salary >= 45000 && salary <= 49999) {    //if salary amount in this range
                this.coachCategory = "Junior";          //then set coaches category to this
                salaryState = true;
         }
            else if(salary >= 50000 && salary <= 59999) {   //if salary amount in this range
                this.coachCategory = "Youth";               //then set coaches category to this
                salaryState = true;
         }
            else if(salary >= 60000 && salary <= 70000) {   //if salary amount in this range
                this.coachCategory = "Senior";              //then set coaches category to this
                salaryState = true; 
         }
            else {  //if invalid salary amount then print error

                if (salary <= 45000 && salary >= 70000);
                    System.out.println("ERROR: Salary range needs to be within 45 000 - 70 000.");

//Second Block...

         boolean isName = false;
     while (isName == false) {
         System.out.print("Name: ");
     name = (input.nextLine());
        if(name != "") {
            this.name = name;
            isName = true;
        }
        else {
            if(name == "");
                System.out.println("ERROR: Please enter a name.");
        }
buræquete
  • 14,226
  • 4
  • 44
  • 89
  • by the way im a complete novice haha so please go easy :P – Kono Harris Sep 20 '19 at 02:53
  • Empty isn't the same as null. If you mean empty, i.e. zero length, you need to use the `String` API. Comparing Java strings with == doesn't compare the content. – user207421 Sep 20 '19 at 02:58
  • Possible dupe http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Scary Wombat Sep 20 '19 at 03:03
  • Sorry not exactly sure how to implement String API this is the first time ive actually seen the reference haha. – Kono Harris Sep 20 '19 at 03:07
  • @KonoHarris And you don't need to compare to "" in the `else` part. You know it is already equal to "" because the `if` failed. And you have an extra `;` in that last `if`. – user207421 Sep 20 '19 at 03:28

1 Answers1

0

Replace

name != "" with "name.length()>0"

Your solution will work.

Here is the full solution:

import java.util.Scanner;
public class Main
{
 public static void main(String[] args) {
     String heroname;
     Scanner input = new Scanner(System.in);
  System.out.println("Hello World");
  String name;
  boolean isName = false;
  while(isName==false){
      System.out.println("Name: ");
      name = (input.nextLine());
      if(name.length()>0){
          heroname = name;
          isName = true;
      }else{
             System.out.println("Try Again");

      }
  }
 }
 
}
b1k
  • 115
  • 7