0

I am working on a program that has two classes. Class 1 contains a constructor called "Probability" that takes three inputs: a String and two doubles. This code is shown below:

public Probability(String inputString, double inputDouble1, double inputDouble2) {
    this.inputString = inputString;
    this.inputDouble1 = inputDouble1;
    this.inputDouble2 = inputDouble2;
}

There is also a modifier that has five conditionals depending on the String that is fed in (i.e. if this.inputString == "String1"...), with a catch for invalid inputs. Class 2 calls the "Probability" constructor multiple times to create probabilities I need for my program.

So here's my dilemma. There are five String inputs that I need to be able to enter based on whatever I'm doing. Before, I was going into my Class 2 code and changing all of the references to this String manually (the references were "String1", "String2", etc...). In other words, the code looked something like this:

Probability P1 = new Probability("String1", double1, double2);

This is obviously a pain when you have twenty references. So I wanted to use a Scanner to take user input in order to change all of the references at once when I run my Main. The user would enter String1 when prompted, and then the input would be set equal to a String variable. This is my new code, where double1 and double2 are previous Scanner user inputs:

Scanner scan = new Scanner(System.in);

System.out.print("Please enter a string: ");
String userInput = scan.nextLine();
Probability P1 = new Probability(userInput, double1, double2);

But this doesn't work. I get the error that I've set up in my catch in the Class 1 modifier that says the input doesn't match any of the strings in the conditionals. I have tried the inputs String1 and "String1". Does anyone have any ideas why this may be? I have no issues with the double inputs fed from Class 2 into the Class 1 modifier.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Severus
  • 3
  • 1

1 Answers1

0

Yes my friend as other suggest you have to compare the two string using equals method not by using == operator which is for reference matching not content matching.

Girish
  • 1,717
  • 1
  • 18
  • 30