-1

I am new to Java and programming in general.

In my program, I use if statements to read specific user input. But, when I enter the code "75832", it processes it as if I entered "24231". Also, if I enter anything besides those numbers, it processes it as if I entered "24231". I've done some other basic programs like this before, but I don't know what I did or am doing wrong.

Any help?

static void gateMethod() throws InterruptedException
{

String stageCode;
int randomChunk;

Random generator = new Random();
Scanner input = new Scanner (System.in);

randomChunk = generator.nextInt(6);
System.out.println("Welcome to Datahub v.1");
System.out.println("\rEnter a phrase or number: ");
stageCode = input.nextLine();

if (stageCode.equals("24231"));
{
    if(randomChunk == 0)
    {
        System.out.println("Data Chunk found! Please wait...");
        Thread.sleep(2000);
        System.out.println("Chunk 1/6:");
        System.out.println("text");
        Thread.sleep(2000);
        System.out.println("");
        gateMethod();

    }
    else if(randomChunk == 1)
    {
        System.out.println("Data Chunk found! Please wait...");
        Thread.sleep(2000);
        System.out.println("Chunk 2/6;");
        System.out.println("text");
        Thread.sleep(2000);
        System.out.println("");
        gateMethod();

    }
    else if(randomChunk == 2)
    {
        System.out.println("Data Chunk found! Please wait...");
        Thread.sleep(2000);
        System.out.println("Chunk 3/6:");
        System.out.println("text");
        Thread.sleep(2000);
        System.out.println("");
        gateMethod();
    }
    else if(randomChunk == 3)
    {
        System.out.println("Data Chunk found! Please wait...");
        Thread.sleep(2000);
        System.out.println("Chunk 4/6:");
        System.out.println("text");
        Thread.sleep(2000);
        System.out.println("");
        gateMethod();   
    }
    else if(randomChunk == 4)
    {
        System.out.println("Data Chunk found! Please wait...");
        Thread.sleep(2000);
        System.out.println("Chunk 5/6:");
        System.out.println("text");
        Thread.sleep(2000);
        System.out.println("");
        gateMethod();   
    }
    else if(randomChunk == 5)
    {
        System.out.println("Data Chunk found! Please wait...");
        Thread.sleep(2000);
        System.out.println("Chunk 6/6:");
        System.out.println("text");
        Thread.sleep(2000);
        System.out.println("");
        gateMethod();   
    }
}
if (stageCode.equals("75832"));
{
    System.out.println("");
    System.out.println("Data correct! Please wait...");
    Thread.sleep(2000);
    System.out.println("text");
    System.out.println("more text");
}   

}

tbodt
  • 16,609
  • 6
  • 58
  • 83
  • While I try to work it out, just a tip, you could cut out that whole big if/else if chunk using `System.out.println("Chunk " + (randomChunk + 1) + "/6;");` – Jordan Jul 23 '13 at 23:56

1 Answers1

4

Remove the semicolon from the end of this line:

if (stageCode.equals("24231"));

and from this line:

if (stageCode.equals("75832"));

Java will treat the semicolon as the entire body of the if condition here. Then the block in braces following it will always be executed, because it's not attached to the if condition as you expected it to be.

rgettman
  • 176,041
  • 30
  • 275
  • 357
  • This is the second similar situation I've seen posted here in the last couple weeks. Is there a compiler option that detects this potential error and displays a warning? If so, maybe we should encourage people to use it. If not, maybe we should encourage someone to add one!! – ajb Jul 24 '13 at 00:10
  • @ajb I don't think there's a compiler option for that, but a good IDE should detect it. IntelliJ IDEA detects these cases this way by highlighting the `if` as a warning and remarking `'if' statement has empty body`. – rgettman Jul 24 '13 at 00:16
  • Checkstyle or pmd or findbugs. – bmargulies Jul 24 '13 at 01:28