I'm trying to write a program that will take a string and parse it into two outputs, with the delimiter being a comma. It loops until the user enters the character "q".
ie: Console prompts to enter an input, and user inputs "first, second" and then "q" for the second prompt, and the output will be:
Enter input string:
First word: first
Second word: second
Enter input string:
If there is no comma in the input, it throws an error and prompts again
ie User inputs "first second" and the output will be:
Enter input string:
Error: No comma in string
Enter input string:
Below is what I have so far:
import java.util.Scanner;
public class ParseStrings {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in); // Scanner for standard input
Scanner inSS = null; // Scanner to write input to buffer
String firstWord = ""; // First word string
String secondWord = ""; // Second word string
String userInput = ""; // Input from prompt
int i = 0; // Loop iterator
boolean inputDone = false; // Boolean to repeat while loop
boolean hasComma = false; // Boolean to check for comma
// Do while inputDone is false
while (!inputDone) {
//Prompt user to input a string
System.out.println("Enter input string: ");
// Write string to userInput
userInput = scnr.nextLine();
// Exit program if user inputs q
if((userInput.equals("q"))) {
inputDone = true;
break;
}
else{
// Write userInput to buffer
inSS = new Scanner(userInput);
// Write first word from buffer
firstWord = inSS.next();
// Loop through first word string
for (i = 0; i < firstWord.length(); ++i) {
// If char is a comma, write everything after to secondWord and set inputDone to true
if(firstWord.charAt(i) == ',') {
secondWord = inSS.next();
hasComma = true;
}
}
// If hasComma is false, return error
if (!hasComma){
System.out.println("Error: No comma in string");
}
// Else print first word and second word
else {
System.out.println("First word: " + firstWord);
System.out.println("Second word: " + secondWord);
System.out.println("");
System.out.println("");
}
}
}
return;
}
}
Problems:
Thank you in advance!
Try using string split.
String str="first, second";
String[] arr=str.split(",");
if(arr.length == 2) {
System.out.println("First :" + arr[0]);
System.out.println("Second :" + arr[1]);
} if(arr.length > 2) {
System.out.println("More than 1 comma used.");
} else {
System.out.println("Error. No comma found.");
}
You can use trim() in case your string has any spaces around comma.
So what I've done is probably the laziest and the most rookie way, but it does work.
Use a for loop to check each character of the string for the comma.
char ch;
String str = "";
if(userInput.charAt(userInput.length()-1) == ',')
System.out.println("Error. You must have a comma here.");
else {
for(int i = 0; i < userInput.length(); i++)
{
ch = userInput.charAt(i);
if(ch != ',')
str += ch;
else
{
firstWord = str;
secondWord = userInput.substring(i+1);
break;
}
}
}
If the selected letter is not a comma, it is added to the temporary string.
If it is a comma, then the temporary string becomes the first word, and the second word is the string after the occurrence of the comma.
if(userInput.charAt(userInput.length()-1) == ',')
Handles the exception that may arise if the input is hello ,
, or anything that ENDS with a comma.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.