0

Ello, got a minor issue that I haven't got a clue on what's going wrong... Snippet of the code:

do{
      String third = JOptionPane.showInputDialog(null, "Please enter Year ["+day+"/"+month+"/YY?]");
      year = Integer.parseInt (third);
      validChoice = validYear(year);
   }while(!validChoice);

do{
      String goUS = JOptionPane.showInputDialog(null, "Do you want to switch the format to US? [Y/N]");
      goUS = goUS.toUpperCase();
      char conversion = goUS.charAt(0);
      validChoice = validOption(conversion);


      switch(conversion){
          case 'Y':

              if(year < 10)
               {
                  date.append(month).append("/").append(day).append("/" + "200").append(year);
               }else{
                  date.append(month).append("/").append(day).append("/" + "20").append(year);
              }
              JOptionPane.showMessageDialog(null, date);
              break;

          case 'N':

              if(year < 10)
               {
                  date.append(day).append("/").append(month).append("/" + "200").append(year);
               }else{
                  date.append(day).append("/").append(month).append("/" + "20").append(year);
              }
              JOptionPane.showMessageDialog(null, date);
              break;

          default:
              JOptionPane.showMessageDialog(null, "Invalid answer! Use Y/N", "Error", JOptionPane.ERROR_MESSAGE);
       }}while(!validChoice);

//// METHODS:

public static boolean validYear (int year){
    boolean isValid = true;

    if(year < 1 || year > 99)
     {
      isValid = false;
     }

    return isValid;
}

public static boolean validOption (char conversion){
    boolean isValid = true;

    if(conversion != 'y' || conversion != 'n')
     {
      isValid = false;
     }

    return isValid;
}

The first part, with regards to the year, and with it's associated method, it loops fine if the input is wrong.

The second part, 'goUS', the method's remotely the same thing, checking the char if it's y/n - it works fine with regards to the check and all, but after hitting OK with the correct display of the date, it starts the goUS loop again, and the answers start to concatenate itself from the previous display, and so on. I want the app to just end after displaying the date.

Any help pointing out my flaw would be greatly appreciated.

2
  • It would help a lot if you showed us the code of your validOption method. Commented Jul 25, 2013 at 11:54
  • Sorry, I've updated the code with it above. Commented Jul 25, 2013 at 12:13

3 Answers 3

1

Well, your validOption checks for lower case characters, but you pass it upper case characters.

The most flexible solution would be to normalise the case before comparing to expected values, such as

public static boolean validOption (char conversion) {
    conversion = Character.toLowerCase(conversion)
    return conversion == 'y' || conversion == 'n';
}
Sign up to request clarification or add additional context in comments.

Comments

0

It seems the problem is because of case of input character, you are checking with lower-case in validOption and checking with upper-case in the main method. I believe your input is upper case, and validOption returns false.

Change validOption to:

public static boolean validOption (char conversion) {
    return conversion == 'Y' || conversion == 'N';
}

or even use java.lang.Character toLower or toUpper methods.

2 Comments

What is the conversion then? Can you print it on the console?
Got it working with your return solution - thank you very much :)
0

Your validOption() does not work correctly. The working of the rest of the loop is independent of that, but the termination depends on the correct return value.

You check lower case characters there, but uppercase elsewhere. You change the checked character to uppercase just before the validity check. Change the characters to uppercase in validOption() too.

1 Comment

@user2516662 I updated the answer to include what is wrong with validOption()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.