JAVA________What am i missing???And were does it go???? My CODE!!!!! import java
ID: 3585984 • Letter: J
Question
JAVA________What am i missing???And were does it go????
My CODE!!!!!
import java.util.Scanner;
public class ParseStrings {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String s, first, last;
while (true) {
System.out.print(" Enter input string: ");
s = keyboard.nextLine();
// checks if user enter q or Q
if(s.compareTo("q") == 0 || s.compareTo("Q") == 0)
break; // stop and exit the program
while (s.indexOf(",") == -1) { // checks if any comma is present
System.out.println("Error: No comma in string");
System.out.print("Enter input string: ");
s = keyboard.nextLine();
}
// extract the first word
first = s.substring(0, s.indexOf(",")).trim();
// extract the lsat word
last = s.substring(s.indexOf(",") + 1, s.length()).trim();
System.out.println("First word: " + first);
System.out.println("Second word: " + last);
}
return;
}
}
Final output!!!
1. Compare output Jill, Allen Input Your output starts with Enter input string: Fi Expected output starts with Enter input string: 2. Compare output 0/2 Jill Allen JillAllen Input Jill Allen Enter input string: Error: No comma in string Enter input string: Error: No comma in string Your output starts with Enter input string: Error: No comma in string Enter input string: Error: No comma in string Enter input string: Exc Enter input string: Error: No comma in string Enter input string: Error: No comma in string Expecte d output starts with Enter input string Error: No comma in string Enter input string:Explanation / Answer
Modified code is in bold.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String s, first, last;
while (true) {
// removed at starting
System.out.print("Enter input string: ");
s = keyboard.nextLine();
// checks if user enter q or Q
if("q".equals(s)|| "Q".equals(s))
break; // stop and exit the program
while (s.indexOf(",") == -1) { // checks if any comma is present
System.out.println("Error: No comma in string");
System.out.print("Enter input string: ");
s = keyboard.nextLine();
}
// extract the first word
first = s.substring(0, s.indexOf(",")).trim();
// extract the lsat word
last = s.substring(s.indexOf(",") + 1, s.length()).trim();
System.out.println("First word: " + first);
System.out.println("Second word: " + last);
System.out.println();
}
return;
}
}
SAMPLE OUTPUT
- While taking input, you used which shouldn't be there
- Program isn't getting stopped when q is entered. So, used equals method which returns boolean value
- Need to print a new line printing each output
Modified code is in bold.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String s, first, last;
while (true) {
// removed at starting
System.out.print("Enter input string: ");
s = keyboard.nextLine();
// checks if user enter q or Q
if("q".equals(s)|| "Q".equals(s))
break; // stop and exit the program
while (s.indexOf(",") == -1) { // checks if any comma is present
System.out.println("Error: No comma in string");
System.out.print("Enter input string: ");
s = keyboard.nextLine();
}
// extract the first word
first = s.substring(0, s.indexOf(",")).trim();
// extract the lsat word
last = s.substring(s.indexOf(",") + 1, s.length()).trim();
System.out.println("First word: " + first);
System.out.println("Second word: " + last);
System.out.println();
}
return;
}
}
SAMPLE OUTPUT
Enter input string: Jill, Allen First word: Jill Second word: Allen Enter input string: Golden , Monkey First word: Golden Second word: Monkey Enter input string: Washington, DC First word: Washington Second word: DC Enter input string: q
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.