92.0k views
4 votes
Write a program to input a character (ch)convert the character into its opposite case. print the entered character in the new character the program should reject if the elector entered is not an alphabet A to z or a to z your program should continue as long as the user wants.


write \: in \: java
​

User Flabdablet
by
4.1k points

1 Answer

3 votes

Answer:

import java.util.Scanner;

import java.lang.*;

class Main {

public static void main(String[] args) {

boolean exit = false;

Scanner reader = new Scanner(System.in);

while(!exit) {

char c = '?';

while( !Character.isLetter(c)) {

System.out.print("Enter a letter or * to exit: ");

c = reader.next().charAt(0);

if (c == '*') {

exit = true;

break;

}

if (!Character.isLetter(c)) {

System.out.printf("Error: %c is not a letter!\\", c);

}

}

if (Character.isLowerCase(c)) {

System.out.printf("%c in uppercase is %c\\", c, Character.toUpperCase(c));

} else if (Character.isUpperCase(c)) {

System.out.printf("%c in lowercase is %c\\", c, Character.toLowerCase(c));

}

}

reader.close();

}

}

Step-by-step explanation:

I decided to use the asterisk (*) as exit character.

User AmeliaBR
by
4.3k points