0

Most of the examples I find in my search for the answer are embedded in complex code. I'm just trying to have a question program know to accept either upper or lower case responses to a two-answer (binary?) question and then to use either answer to proceed to a singular result. I tried an if/else approach before I found character class methods in my book, but it only helps half way with it's examples.

here's the code:

 import java.util.Scanner;
public class MarriageQuiz
{
public static void main(String[] args)
{   
  char isMarried = 'M';
  char isSingle = 'S';
  String marStat;

  System.out.print("Please enter your Marital Status (M or S) >> ");
  marStat = input.nextLine();

     if(marStat.isUppercase(isMarried | isSingle))
     {
        marStat = input.nextLine();
     }
     else if((marStat.isLowercase(isMarried | isSingle)))
     {
        marStat = marStat.toUpperCase(isMarried | isSingle);
     }
     System.out.print("You are " + marStat);
}
} 

2 Answers2

2

Just convert all input to your expected case (upper or lowercase, you pick one).

No harm done in trying to convert a character to one case that happens to already be in that case. The toUppercase method does not complain when you call it needlessly. So no need to test for case at all.

marStat = marStat.toUppercase() ;
if ( marStat.equals( 'S' ) {
    … do Single stuff
} else if ( marStat.equals( 'M' ) {
    … do Married stuff
} else {
    … handle unexpected input
}

Tip: if running this code a zillion times, consider replacing the use of the char primitive with the Character class to avoid the autoboxing discussed in the Oracle Tutorial.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

isUppercase and isLowercase doesn't work like that at all. And your code makes little sense.

It seems like that you want to compare characters only, but you called nextLine which returns a String. And in the if statements, somehow you are asking for user input again. I don't really know what you're doing here.

I guess you want something like this:

char isMarried = 'M';
char isSingle = 'S';
char marStat;

System.out.print("Please enter your Marital Status (M or S) >> ");
marStat = input.nextLine().charAt(0);

if(marStat == 's' || marStat == 'S')
{
   System.out.println("You are single!");
}
else if(marStat == 'm' || marStat == 'M')
{
   System.out.println("You are married!");
}

I don't think you fully understand how programming works.

Sweeper
  • 213,210
  • 22
  • 193
  • 313