Forum Stats

  • 3,816,091 Users
  • 2,259,138 Discussions
  • 7,893,390 Comments

Discussions

cannot go back to a Label/Line in the program

3293553
3293553 Member Posts: 6
edited Sep 6, 2016 3:45PM in Swing

Hello Oracle users,

I made a program that

tells you how old are you

the program work and all,

but i have a problem!

i will explain, if the user doesn't

type correctly the date String it makes an error,

now i used "try {", and in the lower part

of the program i used "Catch".

it catch's the error and all, that's not the problem,

the problem is after the "catch" statement

i want to go back up to the upper code

where the input date String line.

so the user has anther chance.

But i Don't know how to go back

to the upper line/label.

And that's the problem.

Here's the code, the line where

i want to go back to is 21,

where is written the label "dateof:"

Note: to run the program successfully

run from "command line"

import java.time.*;import java.time.temporal.ChronoUnit;import java.util.*;import java.time.format.DateTimeFormatter;import javax.swing.JOptionPane;/** * * @author PC-User */public class InputDate {    /**     * @param args the command line arguments     */    public static void main(String[] args) {        // TODO code application logic hereString age;        dateof: //label date, i want to return to here        age = JOptionPane.showInputDialog(null,"dd.MM.yyyy By:", "Type date of birth", JOptionPane.QUESTION_MESSAGE);   /* All sorts of code:* ...* ...* ...* ...* ...* ...* ...* ...* ...*/int year_burn;        int birth_year;        int birth_months;        int days_burn;        try {               DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");        LocalDate d1 = LocalDate.parse(age,formatter);LocalDate dNow = LocalDate.now();  // Current date    Period p = Period.between(d1,dNow);long p2 = ChronoUnit.DAYS.between(d1,dNow);long p3 = ChronoUnit.WEEKS.between(d1,dNow);System.out.println("You are " + p.getYears() + " years, " + p.getMonths() +" months, and " + p.getDays() +                    " days old. (" + p2 + " days total), (" + p3 + " Total Weeks)");/* all sorts of code:* ...* ...* ...* ...* ...* ...* ...* ...* ...* ...* ...* ...* ...* ...* ...*/}catch(java.time.format.DateTimeParseException ex) {                    System.err.println("DateFormatException "+ ex.getMessage());            JOptionPane.showMessageDialog(null, ex.getMessage(), "Syntex of date wrong, Please type date by pattern", JOptionPane.ERROR_MESSAGE);           //  break dateof; // here i want to return to the top where label "dateof:"                   System.out.println("System has encourted an error so it will Close");        JOptionPane.showMessageDialog(null,"System has encourted an error so it will Close","Program Error ",JOptionPane.ERROR_MESSAGE);        System.exit(0);        }    }}

Answers

  • SanjeevChauhan
    SanjeevChauhan Member Posts: 1,554 Gold Trophy
    edited Aug 9, 2016 12:57PM

    This seems to be a swing question and not related to ADF.

    But I believe you can introduce an infinite while loop to ask user again and again untill either user input is in correct format or he decides to quit.

    public static void main(String[] args) { 

    while(true){

        

        

         try{

             // your-code

            // you can add a condition on which you want to exit the loop (for example user hits exit button, after 10 unsuccessfull attmpt etc)

              if(condition-that-you-want-to-check){

                  break;

             }

        }

    catch(java.time.format.DateTimeParseException ex) { 

             

                System.err.println("DateFormatException "+ ex.getMessage()); 

                JOptionPane.showMessageDialog(null, ex.getMessage(), "Syntex of date wrong, Please type date by pattern", JOptionPane.ERROR_MESSAGE); 

                

    //  break dateof; // here i want to return to the top where label "dateof:" 

                

            System.out.println("System has encourted an error so it will Close"); 

            JOptionPane.showMessageDialog(null,"System has encourted an error so it will Close","Program Error ",JOptionPane.ERROR_MESSAGE); 

            //System.exit(0);  No need of system.exit

            } 

     

        } 

    }

    }

    Thanks

    Sanjeev

  • 3293553
    3293553 Member Posts: 6
    edited Aug 13, 2016 11:33AM

    Thank you,

    SanjeevChanuhan

    This is a good answer but

    not exactly what I need.

    Because I have more Lines of code

    under the "Age Date" Input

    like name, profession, and so on...

    they are located in the lines where

    are written:

    "/* all sorts of code:"

    in the sample above, that i published here.

    I just didn't add them for my own reasons.

    So if i add the code:

    "while(true){"

    in the top, it will affect all the code

    and that's not exactly what i need.

    I would love to use a command like "goto" or such

    or some class to import, if it may be possible.

    Thanks,

    Yaniv R.

  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    edited Aug 23, 2016 4:00AM

    The point is: you should limit the code that is inside the try/catch to a minimum, exactly that much as you want to be repeated (e.g. the output that requests the datae and the actual imput code).

    place the while() loop around the try/cach, but have the while depending on a variable.

    first instruction inside the try/catch should be to set the variable to TRUE, last instruction inside the try/catch should be to set the variable to FALSE.

    boolean isDateFailed;do{  try{    isDateFailed=true;    // your code to request the date    isDateFailed=false;  }catch(DateTimeParseException ex){    //your exception code  }}while(isDateFailed);

    As @SanjeevChauhan wrote it is a really bad idea to call System.exit() in a catch block since this will ent the JVM and in turn your program. This is usually not what you want.

    bye

    TPD

  • 3293553
    3293553 Member Posts: 6
    edited Sep 1, 2016 9:31AM

    Thank you,

    TPD-Opitz

    the code that you recommended

    is a litlle bit complicated,

    becuase i have more "input statmentes" in the porgram,

    and ambressaing each one of them in a while loop

    is a litlle bit messy and not well orginzied code.

    but i found, anthor command that can help me,

    this command jump's to a line number in the program:

    "GotoFactory.getSharedInstance().getGoto().go(4);"

    where "(4)" is the line number i want to goto.

    but there's still a problem when i write it inside my program

    it color's the command in red!.

    i guess that it need's some import class.

    but i don't know which, please help me,

    find the import class for that command.

    here is a sample of using this command in a program:

    public class GotoDemo {   2     public static void main(String[] args) {   3         int i = 3;   4         System.out.println(i);   5         i = i - 1;   6         if (i >= 0) {   7             GotoFactory.getSharedInstance().getGoto().go(4);   8         }   9           10         try {  11             System.out.print("Hell");  12             if (Math.random() > 0) throw new Exception();              13             System.out.println("World!");  14         } catch (Exception e) {  15             System.out.print("o ");  16             GotoFactory.getSharedInstance().getGoto().go(13);  17         }  18     }  19 }

    If you can find me a import class for the command:

    "GotoFactory.getSharedInstance().getGoto().go(4);"

    I will Be Very Thankful,

    Yaniv R.

  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    edited Sep 6, 2016 3:45PM

    There is a reason why goto in Java is a reserved word without function.

    If you think you cannot code without using goto instruction or the equivalent mechanism you should consider programming in Basic or stop your programmers career at all.

    bye

    TPD

This discussion has been closed.