简体   繁体   中英

Unreachable Statement After For Loop with If Statement

I have an assignment that is very basic but it's all-new ground for me still. The program is supposed to ask the user(s) to enter their name, surname, grade, and subject. If the subject is not IT their input gets set aside. However, if the subject entered is IT their name, surname and grade will be used later on again to display a message that looks as followed:

DisplayedMessage

The information can differ depending on what the user(s) entered. Now the question comes in. I typed the code correctly but at the end where I display the message using JOptionPane it showed an error saying: "Unreachable Statement". (To clarify it happened next to the line of code) It was strange because it was almost identical to the memo of the assignment yet it had the error. It might have been the Netbeans being confused in a way but I redid the assignment at home and it worked as it should seen here:

    String name, surname, subject, message = "IT Learners:\n\n";
    int grade, counter = 0;

    for (int i = 0; i < 3; i++) {
        name = JOptionPane.showInputDialog("Enter Your Name Here: ");
        surname = JOptionPane.showInputDialog("Enter Your Surname Here: ");
        subject = JOptionPane.showInputDialog("Enter Your Subject Here: ");
        grade = Integer.parseInt(JOptionPane.showInputDialog("Enter Your "
                + "Grade Here: "));

        if (subject.equalsIgnoreCase("IT")) {
            message += name + " " + surname + " is in grade "
                    + grade + ".\n";
            counter++;
        }
    }
    JOptionPane.showMessageDialog(null, message + "There is a total of "
            + counter + " IT Learners.");

The next section of code is a copy of the code I wrote on the terminal during the period. I personally can't see the error and I came here to ask is it correct or is there a small error that I just can't seem to find.

Code I did at school:

    String name, surname, subject, message = "IT Learners:\n\n";
    int grade, counter = 0;

    for (int i = 0; i < 3; i++) {
        name = JOptionPane.showInputDialog("Enter Your Name Here: ");
        surname = JOptionPane.showInputDialog("Enter Your Surname Here: ");
        subject = JOptionPane.showInputDialog("Enter Your Subject Here: ");
        grade = Integer.parseInt(JOptionPane.showInputDialog
                ("Enter Your Grade Here: "));

        if (subject.equalsIgnoreCase("IT")) 
        {
            message += name + " " + surname + " is in grade "
                    + grade + ".\n";
            counter++;
        }
    }
    JOptionPane.showMessageDialog(null, message + "There is a total of "
            + counter + " IT Learners.");

It truly looks identical, was it just the Netbeans being confused or is there just a fine detail I am missing?

Entire code:

    import javax.swing.JOptionPane;

    /**
    *
    * @author (My Name)
    */
    public class ITLearners {

    public static void main(String[] args) {
    // TODO code application logic here
    String name, surname, subject, message = "IT Learners:\n\n";
    int grade, counter = 0;

    for (int i = 0; i < 3; i++) {
        name = JOptionPane.showInputDialog("Enter Your Name Here: ");
        surname = JOptionPane.showInputDialog("Enter Your Surname Here: ");
        subject = JOptionPane.showInputDialog("Enter Your Subject Here: ");
        grade = Integer.parseInt(JOptionPane.showInputDialog("Enter Your "
                + "Grade Here: "));

        if (subject.equalsIgnoreCase("IT")) {
            message += name + " " + surname + " " + " is in grade "
                    + grade + ".\n";
            counter++;
        }
    }
    JOptionPane.showMessageDialog(null, message + "There is a total of "
            + counter + " IT Learners.");
}
}

I feel quite silly right now. The problem in the code done at school was that an i in the for loop condition was a 1. Like this:

   for ( int i = 0; 1 < 3; i++; )

Lesson learned out of this: make sure you type everything correctly. With the 1 there I created an unintentionally infinite loop without realizing it. Thank you so much for the help, especially @AcidResin!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM