ask user for a string. Characters that are number, are doubled and replaced with the sum. The characters that are upper are replaced with lower and lower are replaced with upper. It should keep asking user for string until user input Q to end the program.
Max Function
Statistical function is of many categories. One of them is a MAX function. The MAX function returns the largest value from the list of arguments passed to it. MAX function always ignores the empty cells when performing the calculation.
Power Function
A power function is a type of single-term function. Its definition states that it is a variable containing a base value raised to a constant value acting as an exponent. This variable may also have a coefficient. For instance, the area of a circle can be given as:
Im stuck on this java code. The code is supposed to ask user for a string. Characters that are number, are doubled and replaced with the sum. The characters that are upper are replaced with lower and lower are replaced with upper. It should keep asking user for string until user input Q to end the program.
example 1:
original: 3rD
converted: 6Rd
ex 2:
original: 6sJ
converted: 3Sj
(when a value is doubled and its than 9, the two digits are added. so 6 * 2 is 12. And then add the two digits. so 1 + 2 = 3.)
I'm having problem with the code that its not reading where it replace the lower to upper and vice versa.
this is my code so far:
import java.util.Scanner;
public class Convert {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String original, converted;
while (true) {
System.out.print("Original String (input):");
original = input.nextLine();
if (original.equals("q") || original.equals("Q")) {
break;
}
char[] chars = original.toCharArray();
for (int i = 0; i < chars.length; i++) {
char ch = chars[i];
if (Character.isDigit(ch)) {
int d = (int) (ch - '0');
d = d * 2;
if (d > 9) {
int n = 0;
n = d % 10;
d = d + n;
d = d / 10;
d = d + n;
}
chars[i] = (char) ('0' + d);
}
else if (Character.isLetter(ch)) {
if (Character.isLowerCase(ch)) {
ch = Character.toUpperCase(ch);
}
if (Character.isUpperCase(ch)) {
ch = Character.toLowerCase(ch);
}
}
}
converted = new String(chars);
System.out.println("Converted String (output):" + converted);
}
System.out.println("Terminating upon user's request!");
}
}
Thank you for your help!
Trending now
This is a popular solution!
Step by step
Solved in 2 steps