Audit of text availability in variables



  • The problem arose: I have a variable dx in which I insert a letter and a number (e.g. D2). This code I'm checking the letter A,B,C,D,E and the figures 1.2,3.5 in the variable dx, and if there is an unacceptable letter/charter, I'm asking you to reintroduce the text, but this code also passes other letters and figures (e.g. V7). What's my mistake?

    package War;
    

    import java.util.Scanner;

    public class Convert extends Main {
    public void convert(){
    for(;shipP==false&&shipP1==false;){
    Scanner scan = new Scanner(System.in);
    dx = scan.nextLine();
    if(dx.contains("A")||dx.contains("a")){
    x=0;
    shipP=true;
    }
    if(dx.contains("B")||dx.contains("b")){
    x=1;
    shipP=true;
    }
    if(dx.contains("C")||dx.contains("c")){
    x=2;
    shipP=true;
    }
    if(dx.contains("D")||dx.contains("d")){
    x=3;
    shipP=true;
    }
    if(dx.contains("E")||dx.contains("e")){
    x=4;
    shipP=true;
    }

            if(dx.contains("1")){
                y=0;
                shipP1=true;
            }
            if(dx.contains("2")){
                y=1;
                shipP1=true;
            }
            if(dx.contains("3")){
                y=2;
                shipP1=true;
            }
            if(dx.contains("4")){
                y=3;
                shipP1=true;
            }
            if(dx.contains("5")){
                y=4;
                shipP1=true;
            }
        }
        shipP=false;
        shipP1=false;
    }
    

    }



  • Replace yours. for That's it.

     while(!shipP || !shipP1)
    

    or on that (hey, de Morgan)

     while(!(shipP && shipP1))
    

    And try not to build such blank cycles.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2