JAVA scanner causes if statement problems when using bag

88 Views Asked by At

I am new to JAVA and have been using IDE, to cut it short whenever I try to check if bag contains a string thats the same as the given input JAVA counts it as FALSE, even if the if statements such as "is input equal to 1" and "is 1 inside the bag" pass as true. here is an excerpt from my code, I would appreciate any help and advice.

          //user input
      System.out.println("Please enter a string (to exit, enter 'exit'): ");
      a=sc.next();
      if (a.equals("1")) {System.out.println("adpkgnosıfbgojadnofabsndofgna");}
      if (ValidAnswers1.contains("1")) {System.out.println("adpkgnosıfbgojadnofabsndofgna");}
      
      
      //error detection. after I learn bag it will become if bag contains string s.
      if (ValidAnswers1.contains(a)) {correct_input=1;} else {correct_input=0;}
      while (correct_input==0) 
      {
          System.out.println("you entered:"+ a+".");
          System.out.println("Please enter a valid string (to exit, enter 'exit')");
          a = sc.next();
          if (ValidAnswers1.contains(a)) {correct_input=1;} else {correct_input=0;}
      }

the console prints out both the keymashes and then diverts into the while loop. I have checked to make sure the while loop is correct by testing with fixed variables, but when scanner is used it seems to have an error.

2

There are 2 best solutions below

4
Dam On

I didn't understand, what you really want, i did the test and a yet is working fine. take a look at the class, maybe is some error in the variables or something

public class Main {
    
    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        String a;
        String ValidAnswers1 = "1";
        int correct_input = 0;
        
      //user input
      System.out.println("Please enter a string (to exit, enter 'exit'): ");
      a = sc.next();
      
      if (a.equals("1")) {
          System.out.println("adpkgnosıfbgojadnofabsndofgna");
      }
      if (ValidAnswers1.contains("1")) {
          System.out.println("adpkgnosıfbgojadnofabsndofgna");
      }
      
      //error detection. after I learn bag it will become if bag contains string s.
      if (ValidAnswers1.contains(a)) {
          correct_input=1;
      } else {
          correct_input=0;
      }
      
      while (correct_input==0) {
          
          System.out.println("you entered:"+ a+".");
          System.out.println("Please enter a valid string (to exit, enter 'exit')");
          a = sc.next();
          
          if (ValidAnswers1.contains(a)) {
              correct_input=1;
          } else {
              correct_input=0;
          }
      }
    }

Here is the output:

Please enter a string (to exit, enter 'exit'): 
a
adpkgnos?fbgojadnofabsndofgna
you entered:a.
Please enter a valid string (to exit, enter 'exit')
1
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
0
Dam On
      //Create a new Object Scan in the memmory
      Scanner scan = new Scanner(System.in);
      
      String input;
      String[] validAnswers = new String[]{"1","2","3","exit"};
      
      boolean isCorrect = false;

      //The method equalsIgnoreCase means that the text can be in uppercase too;
      /*The number between the tags "[]" means the number in the array since arrays
      starts with number "0" */
      
    do{
          System.out.println("Please enter a string (to exit, enter 'exit'): ");
          input = scan.next();

        if(input.equalsIgnoreCase(validAnswers[0])){
          
          isCorrect = true;
          System.out.println("Number 1");
          
        }else if(input.equalsIgnoreCase(validAnswers[1])){
          isCorrect = true;
          System.out.println("Number 2");
          
        }else if(input.equalsIgnoreCase(validAnswers[2])){
          
          isCorrect = true;    
          System.out.println("Number 3");
          
        }else if(input.equalsIgnoreCase(validAnswers[3])){
          
          isCorrect = true;
          System.out.println("EXIT!");
          
          //You could use the method System.exit(0) to finish the program;
          //If you put "1" in the exit value means that the prgram finished with some error;
          //System.exit(0);
        }else{
        //If the Answers is different from all of the others;
          System.out.println("you entered: " + input +".");
          System.out.println("Please enter a valid string (to exit, enter 'exit')");
        }
    }while(isCorrect != true);
    //I used the method do{}while because it's the only method that will exacute at least once;
  }