Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this program, is there any way for the program to read all the outputs toget

ID: 3723644 • Letter: F

Question

For this program, is there any way for the program to read all the outputs together in one line? Like saying" Here is your new string aaabbbcccddd."

import java.util.Scanner;

public class ReadWordsTest {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

String word=new String();

do {

System.out.println("Enter the word (Done to quit): ");

word = scan.next();

}while(!word.equalsIgnoreCase("Done"));

}

}

Output:

Enter the word (Done to quit):
aaa
Enter the word (Done to quit):
bbb
Enter the word (Done to quit):
ccc
Enter the word (Done to quit):
ddd
Enter the word (Done to quit):
Done

Explanation / Answer

ReadWordsTest.java

import java.util.Scanner;

public class ReadWordsTest {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

String word=new String();

String newString = "";

System.out.println("Enter the word (Done to quit): ");

do {

word = scan.next();

if(!word.equalsIgnoreCase("Done")) {

newString+=word;

}

}while(!word.equalsIgnoreCase("Done"));

System.out.println("Here is your new string "+newString);

}

}

Output:

Enter the word (Done to quit):
aaa bbb ccc dddd Done
Here is your new string aaabbbcccdddd

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote