如何使取消按钮功能

时间:2016-07-06 21:44:19

标签: java

我意识到我做了一个无限循环,每次进入该循环时,我的取消按钮都不起作用,对话框会不断弹出。

这是代码

   String buffer = " "; //Input a string into console
   boolean badInput = true;
   boolean badInput2 = true;
   String idNum = ""; //ask for id number 
   String skill = ""; // ask for skill number
   int skillInt = 0; // skill is an int

   //prompt user for file location
   File loc = new File(JOptionPane.showInputDialog(null, "Please provide the file location: "));
   RandomAccessFile store = new RandomAccessFile(loc, "rw");

   //prompt user for a command
   String cmd = "start";
   int recLocation = 0;
   while(cmd.compareToIgnoreCase("end")!=0){ //stay in program (loop) until end command is given
       cmd = JOptionPane.showInputDialog(null, "Please input a command (new, old or end): ");

       //creating new entry
       if(cmd.compareToIgnoreCase("new")==0){
           while(badInput){ //keep them in loop until they give the input in the right format
               idNum = JOptionPane.showInputDialog(null, "Please input an ID number (1 - 20): ");
              // else JOptionPane.CANCEL_OPTIONsetDefaultCloseOperation(JOptionPane.EXIT_ON_CLOSE);
               try{
                   //corresponding int for ID number, which becomes the record location
                   //if number is not 1-20, code below
                   recLocation = Integer.parseInt(idNum);
                   if(recLocation<1 || recLocation>20){
                       System.out.println("Please check that your number is between 1 and 20.");
                   }else{
                       badInput = false;
                       break;  
                   }

               }
               catch(NumberFormatException NF){ // if input isnt a number
                   System.out.println("Please check that your number is in the correct format.");
               }
           }

           //ask for names
           String pName = JOptionPane.showInputDialog(null, "Please input a player name: ");
           String tName = JOptionPane.showInputDialog(null, "Please input a team name: ");

           //ask for skill level
           while(badInput2){ //keep them in the loop until they give the input in the right format
               skill = JOptionPane.showInputDialog(null, "Please input a skill level (0 - 99): ");
               try{
                   //corresponding int for skill number, to check if in the right format
                   skillInt = Integer.parseInt(skill);
                   if(skillInt<0 || skillInt>99){
                       System.out.println("Please check that your number is between 0 and 99.");
                   }else{
                       badInput2 = false;
                       break;  
                   }

               }
               catch(NumberFormatException NF){ //exception or error thrown if input is not in correct format
                   System.out.println("Please check that your number is in the correct format.");
               }
           }

           String date = JOptionPane.showInputDialog(null, "Please input today's date (ex: 25Jun2014): ");

           //formatting id number
           if (idNum.length() < 2){
               idNum = idNum+buffer;
           }
           //formatting player name
           if (pName.length() > 26){
               pName = pName.substring(0, 26);
           } else {
               while(pName.length() < 26){
                   pName = pName+buffer;
               }
           }

           //formatting team name
           if (tName.length() > 26){
               tName = tName.substring(0, 26);
           } else {
               while(tName.length() < 26){
                   tName = tName+buffer;
               }
           }
           //formatting date
           if (date.length() > 9){
               date = date.substring(0, 9);
           } else {
               while(date.length() < 9){
                   date = date+buffer;
               }
           }
           //formatting skill
           if (skill.length() < 2){
               skill = skill+buffer;
           }
           //create full, identifying string
           String fullRecord = idNum + "   " + pName + tName + skill + "   " + date;
           store.seek((RECORD_LENGTH+2) * (recLocation-1));
           store.writeUTF(fullRecord);

           //reset booleans
           badInput = true;
           badInput2 = true;

       }

       //accessing old entry
       if(cmd.compareToIgnoreCase("old")==0){
           idNum = JOptionPane.showInputDialog(null, "Please input an ID number (1 through 20): ");      
           recLocation = Integer.parseInt(idNum);
           store.seek((RECORD_LENGTH+2)*(recLocation-1));
           String fullRecord = store.readUTF();

           //interpret information from full string
           try
           {idNum = fullRecord.substring(0, 5);
           String pName = fullRecord.substring(5, 31);
           String tName = fullRecord.substring(31, 57);
           skill = fullRecord.substring(57, 62);
           String date = fullRecord.substring(62, 71);
           System.out.println("ID: "+idNum+" NAME: "+pName+" TEAM: "+tName+" SKILL: "+skill+" DATE: "+date);
           }
           catch(StringIndexOutOfBoundsException S){
               System.out.println("No record found at that location.");
           }
       }

      // JOptionPane.showMessageDialog(null, "good bye");
   }

很抱歉,如果我没有格式化这个权利。这是我的第一次。我想知道如何让取消按钮退出循环。我尝试使用if (cmd == null) System.exit(0);但似乎没有用。我是java的新手,我没什么经验所以请耐心等待。

1 个答案:

答案 0 :(得分:1)

首先我建议使用equalsIgnoreCase作为String比较而不是compareIgnoreCase:https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#equalsIgnoreCase(java.lang.String)

while(!cmd.equalsToIgnoreCase("end")) {

然后,如果其中一个JOptionPane.showInputDialog()返回null表示它们已被取消,则可以使用if语句将cmd字符串设置为“end”。我想虽然你应该尝试简化循环,因为它在一个块中有很多。这使得调试变得困难。