Home > OS >  How do I set the value of a variable based on another variable?
How do I set the value of a variable based on another variable?

Time:12-05

I am trying to take a scanner input for a variable named site and set another variable, emr, to one of 3 possible values based on what was input for site.

import java.util.Scanner;

public class Program
{
    public static void main(String[] args) {
        Scanner read = new Scanner(System.in);
        String site = read.nextLine();
            if (site == "site1"){
                String emr = "sampletext";
            } else if (site == "site2"){
                String emr = "domain";
            } else {
                String emr = " ";
            }
        System.out.print(emr);
    }
}

Is there a way I can make this work? My understanding of Java is (clearly) limited at best, I started very recently.

CodePudding user response:

Each variable has a scope in Java. The variable doesn't exist outside of its scope. So technically the emr declared here

if (site == "site1"){
     String emr = "sampletext";
}

is not valid here

else if (site == "site2"){
     String emr = "domain";
}

because both the emr's in the if and else if blocks have block scope.

However, the emr declared as below

String site = read.nextLine();
String emr;

is valid through out the method as it has method scope. You can read more here

The working code snip is as follows.

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner read = new Scanner(System.in);
        String site = read.nextLine();
        String emr;

        if (site.equals("site1")){
           emr = "sampletext";
        } else if (site.equals("site2")){
           emr = "domain";
        } else {
           emr = " ";
        }
        System.out.print(emr);
    }
}

When it comes to comparing Objects such as String you need to be careful. You might want to take a look at the accepted answer here

As @Hleb Shypula pointed, if you want to do away with the last else block, you would want to initialize a default value to emr upfront. Below is the updated code.

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner read = new Scanner(System.in);
        String site = read.nextLine();
        String emr = " ";

        if (site.equals("site1")){
           emr = "sampletext";
        } else if (site.equals("site2")){
           emr = "domain";
        } 
        System.out.print(emr);
    }
}

CodePudding user response:

Although other answers will work, I encourage you to split away the logic of checking for what input value is given and assigning the response value.

See the following:

public class ScannerProgram {

  public static void main(String[] args) {
    Scanner read = new Scanner(System.in);
    String site = read.nextLine();
    
    System.out.println(getEmr(site));
  }

  private static String getEmr(String inputString) {
    switch (inputString) {
      case "site1":
        return "sampletext";
      case "site2":
        return "domain";
      default:
        return " ";
    }
  }
}

Another thing to note: depending on the Java version you're using, you may be able to reduce this code even further and utilise newer approaches and definitions of your variables.

For example, with Java 11 you don't need to define a variable's type if the value is assigned at the time of creation. In other words, it's redundant to do so.

So the definitions

Scanner read = new Scanner(System.in);
String site = read.nextLine();

could become

var read = new Scanner(System.in);
var site = read.nextLine();

You could further improve the code with funcitonal way of writing switch statements in Java 17:

So the switch statement can become the following:

  private static String getEmr(String inputString) {
    return switch (inputString) {
      case "site1" -> "sampletext";
      case "site2" -> "domain";
      default -> " ";
    };
  }

CodePudding user response:

You just need to declare the emr variable outside if statement.

I have changed your code as bellow:

import java.util.Scanner;

public class Program
{
    public static void main(String[] args) {
        Scanner read = new Scanner(System.in);
        String site = read.nextLine();
        String emr;

        if (site == "site1"){
           emr = "sampletext";
        } else if (site == "site2"){
           emr = "domain";
        } else {
           emr = " ";
        }
        System.out.print(emr);
    }
}
  • Related