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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.