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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

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!

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Mathematical functions
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education