Home > Blockchain >  How do I stop the for loop or use the original data
How do I stop the for loop or use the original data

Time:12-28

Sorry for my English, but how am I suppose to run the ERROR MESSAGE without repeating 3 times?

This is the original one, if I run the ERROR JOptionPane it will repeat 3 times

String name = JOptionPane.showInputDialog(null, "Enter the Student name to search:", "Input", JOptionPane.QUESTION_MESSAGE);
        String data = ("Cannot find the student \""   name   "\"!!"); 
        for (int i = 0; i < student.length; i  ){    
            if(name.equals(student[i].getName())){
                data = ("\n Course \t Admin# \t Name \n"   student[i].getName()   student[i].getAdminNum()   student[i].getCourse()   student[i].getGpa()   student[i].getNewModule()   "\n");
                JOptionPane.showMessageDialog(null, data, "Message", JOptionPane.INFORMATION_MESSAGE);
            }
            Else{
                 JOptionPane.showMessageDialog(null, data, "Message", JOptionPane.ERROR_MESSAGE);
            }

And this is the current one, which the if else statement is wrong as I couldn't read the original data

String name = JOptionPane.showInputDialog(null, "Enter the Student name to search:", "Input", JOptionPane.QUESTION_MESSAGE);
        String data = ("Cannot find the student \""   name   "\"!!"); 
        for (int i = 0; i < student.length; i  ){    
            if(name.equals(student[i].getName())){
                data = ("\n Course \t Admin# \t Name \n"   student[i].getName()   student[i].getAdminNum()   student[i].getCourse()   student[i].getGpa()   student[i].getNewModule()   "\n");
            }
        }
        if(data != data){
            JOptionPane.showMessageDialog(null, data, "Message", JOptionPane.INFORMATION_MESSAGE);
        }
        else {
            JOptionPane.showMessageDialog(null, data, "Message", JOptionPane.ERROR_MESSAGE);
        }

I'm so sorry if there is any misconception thinking passes to you all due to my broken English.

CodePudding user response:

If i undertand your answer, you are finding the "continue" function.

This one breaks de for loop.

https://www.w3schools.com/java/java_break.asp

CodePudding user response:

Create a local variable of name say not_found (int) and increment this found variable in else part instead of showing Dialog everytime.

After end of for loop, check for found variable and display dialog box according to your requirements.

int not_found=0;
String name = JOptionPane.showInputDialog(null, "Enter the Student name to 
search:", "Input", JOptionPane.QUESTION_MESSAGE);
    String data = ("Cannot find the student \""   name   "\"!!"); 
    for (int i = 0; i < student.length; i  ){    
        if(name.equals(student[i].getName())){
            data = ("\n Course \t Admin# \t Name \n"   student[i].getName() 
  student[i].getAdminNum()   student[i].getCourse()   student[i].getGpa()   
student[i].getNewModule()   "\n");
            JOptionPane.showMessageDialog(null, data, "Message", 
JOptionPane.INFORMATION_MESSAGE);
        }
        else{
             not_found  :
        }

if(not_found!=0){
JOptionPane.showMessageDialog(null, data, 
"Message",OptionPane.ERROR_MESSAGE);
}
  • Related