java regex parse - java

Thanks for checking out my question.
Here the user enter the string in the format: "xD xS xP xH". The program takes the string, splits it on the space bar, then uses regex to parse the string. There is an issue with my "final string regex" and I am not sure where.
final String regex = "([0-9]+)[D|d]| ([0-9]+)[S|s]| ([0-9]+)[P|p]| ([0-9]+)[H|h]";
Lastly, the loop prints out only the value for D so I suspect it reaches an error moving to match S or s.
public class parseStack
{
public parseStack()
{
System.out.print('\u000c');
String CurrencyFormat = "xD xS xP xH";
System.out.println("Please enter currency in the following format: \""+CurrencyFormat+"\" where x is any integer");
Scanner scan = new Scanner(System.in);
String currencyIn = scan.nextLine();
currencyFinal = currencyIn.toUpperCase();
System.out.println("This is the currency you entered: "+currencyFinal);
String[] tokens = currencyFinal.split(" ");
final String input = tokens[0];
final String regex = "([0-9]+)[D|d]| ([0-9]+)[S|s]| ([0-9]+)[P|p]| ([0-9]+)[H|h]";
if (input.matches(regex) == false) {
throw new IllegalArgumentException("Input is malformed.");
}
long[] values = Arrays.stream(input.replaceAll(regex, "$1 $2 $3 $4").split(" "))
.mapToLong(Long::parseLong)
.toArray();
for (int i=0; i<values.length; i++)
{
System.out.println("value of i: "+i+ " |" +values[i]+ "|");
}
//pause to print
System.out.println("Please press enter to continue . . . ");
Scanner itScan = new Scanner(System.in);
String nextIt = itScan.nextLine();
}
}

Your regular expression should be [\d]+[DdSsPpHh].
The problem you are having is you split the string into chunks, then you match chunks with a RegEx that matches the original string that you have split.
HOWEVER this answer only addresses a problem in your code. Your routine doesn't seem to cater your expectation. And your expectation is not clear at all.
EDIT
Added the multidigit requirement.

Your regex can be simplified somewhat.
"(?i)(\d+d) (\d+s) (\d+p) (\d+h)"
will do a case-insensitive match against multiple digits ( \d+ )
This can be further simplified into
"(?i)(\d+[dsph])"
which will iteratively match the various groups in your currency string.

First of all your regex looks a bit to complex. You input format is "xD xS xP xH" also you are converting the input to uppercase currencyIn = currencyIn.toUpperCase(); but this isn't the problem.
The problem is
String[] tokens = currencyIn.split(" ");
final String input = tokens[0];
You are splitting the input and only use the first part which would be "xD"
The fixed code would look like:
String currencyIn = scan.nextLine();
currencyIn = currencyIn.toUpperCase();
System.out.println("This is the currency you entered: "+currencyIn);
final String regex = "([0-9]+)D ([0-9]+)S ([0-9]+)P ([0-9]+)H";
if (!currencyIn.matches(regex)) {
throw new IllegalArgumentException("Input is malformed.");
}
long[] values = Arrays.stream(currencyIn.replaceAll(regex, "$1 $2 $3 $4").split(" "))
.mapToLong(Long::parseLong)
.toArray();
for (int i=0; i<values.length; i++) {
System.out.println("value of i: "+i+ " |" +values[i]+ "|");
}

Related

String Tokenization in Java

I have the following code
System.out.println("Enter equation you want to calculate:");
System.out.println("Equation can not have more than two variables");
equation = input.next();
StringTokenizer var = new StringTokenizer(equation,"");
while (var.hasMoreElements())
{
System.out.println(var.nextToken());
}
The user can enter equation like A + B or Alpha + Beta.
I want the tokenizer to seperate the string like
Alpha
+
Beta
But the output I get is just the first element
Alpha.
equation = input.next(); is only going to read the next (space delimited) word, not the entire equation.
Your problem isn't the tokenizer, it's that you're collecting your input incorrectly.
try instead:
equation = input.nextLine();
If your input is "Alpha + Beta" , then the below code results in
Alpha
+
Beta
StringTokenizer var = new StringTokenizer("Alpha + Beta"," ");
while (var.hasMoreElements())
{
System.out.println(var.nextToken());
}
In the case of format A 'operator' B where 'operator' can mean any from +-*/, I'd suggest you to split with the regular expression. Demo at Regex101.
(\w+)([\+\-\*\/])(\w+)
I suggest you first to replace all the spaces to get rid of unnecessary mess. Also don't forget to escape with the double \\;
Let the input be: String input = "Alpha + Beta";
Pattern pattern = Pattern.compile("(\\w+)(\\+)(\\w+)");
Matcher matcher = pattern.matcher(input.replace(" ",""));
String a = null;
String b = null;
String operator = null;
while (matcher.find()) {
a = matcher.group(1);
operator = matcher.group(2);
b = matcher.group(3);
}
Will return Alpha + Beta from the variables a, operator and b.
Try this code
System.out.println("Enter equation you want to calculate:");
System.out.println("Equation can not have more than two variables");
Scanner input = new Scanner(System.in);
String equation = input.nextLine();
StringTokenizer var = new StringTokenizer(equation, " ");
while (var.hasMoreElements()) {
System.out.println(var.nextToken());
}

How can I move the punctuation from the end of a string to the beginning?

I am attempting to write a program that reverses a string's order, even the punctuation. But when my backwards string prints. The punctuation mark at the end of the last word stays at the end of the word instead of being treated as an individual character.
How can I split the end punctuation mark from the last word so I can move it around?
For example:
When I type in : Hello my name is jason!
I want: !jason is name my Hello
instead I get: jason! is name my Hello
import java.util.*;
class Ideone
{
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String input = userInput.nextLine();
String[] sentence= input.split(" ");
String backwards = "";
for (int i = sentence.length - 1; i >= 0; i--) {
backwards += sentence[i] + " ";
}
System.out.print(input + "\n");
System.out.print(backwards);
}
}
Manually rearranging Strings tends to become complicated in no time. It's usually better (if possible) to code what you want to do, not how you want to do it.
String input = "Hello my name is jason! Nice to meet you. What's your name?";
// this is *what* you want to do, part 1:
// split the input at each ' ', '.', '?' and '!', keep delimiter tokens
StringTokenizer st = new StringTokenizer(input, " .?!", true);
StringBuilder sb = new StringBuilder();
while(st.hasMoreTokens()) {
String token = st.nextToken();
// *what* you want to do, part 2:
// add each token to the start of the string
sb.insert(0, token);
}
String backwards = sb.toString();
System.out.print(input + "\n");
System.out.print(backwards);
Output:
Hello my name is jason! Nice to meet you. What's your name?
?name your What's .you meet to Nice !jason is name my Hello
This will be a lot easier to understand for the next person working on that piece of code, or your future self.
This assumes that you want to move every punctuation char. If you only want the one at the end of the input string, you'd have to cut it off the input, do the reordering, and finally place it at the start of the string:
String punctuation = "";
String input = "Hello my name is jason! Nice to meet you. What's your name?";
System.out.print(input + "\n");
if(input.substring(input.length() -1).matches("[.!?]")) {
punctuation = input.substring(input.length() -1);
input = input.substring(0, input.length() -1);
}
StringTokenizer st = new StringTokenizer(input, " ", true);
StringBuilder sb = new StringBuilder();
while(st.hasMoreTokens()) {
sb.insert(0, st.nextToken());
}
sb.insert(0, punctuation);
System.out.print(sb);
Output:
Hello my name is jason! Nice to meet you. What's your name?
?name your What's you. meet to Nice jason! is name my Hello
Like the other answers, need to separate out the punctuation first, and then reorder the words and finally place the punctuation at the beginning.
You could take advantage of String.join() and Collections.reverse(), String.endsWith() for a simpler answer...
String input = "Hello my name is jason!";
String punctuation = "";
if (input.endsWith("?") || input.endsWith("!")) {
punctuation = input.substring(input.length() - 1, input.length());
input = input.substring(0, input.length() - 1);
}
List<String> words = Arrays.asList(input.split(" "));
Collections.reverse(words);
String reordered = punctuation + String.join(" ", words);
System.out.println(reordered);
The below code should work for you
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ReplaceSample {
public static void main(String[] args) {
String originalString = "TestStr?";
String updatedString = "";
String regex = "end\\p{Punct}+|\\p{Punct}+$";
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(originalString);
while (matcher.find()) {
int start = matcher.start();
updatedString = matcher.group() + originalString.substring(0, start);<br>
}
System.out.println("Original -->" + originalString + "\nReplaced -->" + updatedString);
}
}
You need to follow the below steps:
(1) Check for the ! character in the input
(2) If input contains ! then prefix it to the empty output string variable
(3) If input does not contain ! then create empty output string variable
(4) Split the input string and iterate in reverse order (you are already doing this)
You can refer the below code:
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String originalInput = userInput.nextLine();
String backwards = "";
String input = originalInput;
//Define your punctuation chars into an array
char[] punctuationChars = {'!', '?' , '.'};
String backwards = "";
//Remove ! from the input
for(int i=0;i<punctuationChars.length;i++) {
if(input.charAt(input.length()-1) == punctuationChars[i]) {
input = input.substring(0, input.length()-1);
backwards = punctuationChars[i]+"";
break;
}
}
String[] sentence= input.split(" ");
for (int i = sentence.length - 1; i >= 0; i--) {
backwards += sentence[i] + " ";
}
System.out.print(originalInput + "\n");
System.out.print(input + "\n");
System.out.print(backwards);
}
Don't split by spaces; split by word boundaries. Then you don't need to care about punctuation or even putting spaces back, because you just reverse them too!
And it's only 1 line:
Arrays.stream(input.split("\\b"))
.reduce((a, b) -> b + a)
.ifPresent(System.out::println);
See live demo.

Output the integers in a String separately and output it afterwards

The user will input a string and the program should recognize the integers.
If the user inputs Hello12 3 it should output:
The integral numbers are:
id1 12
id2 3
but in my code, it outputs
The integral numbers are:
int1
int2
int3
int4
int5
int6 12
int7 3
How should i fix it?
My code:
import java.util.*;
public class LexicalAnalyzer {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
String str;
int j=0;
System.out.println("Lexical Analyzer for Algebraic Expressions\n");
System.out.print("Enter the String: ");
str = input.nextLine();
System.out.println("String length is: "+str.length());
System.out.println("\nThe integral numbers are: \n");
String intNum = str.replaceAll("[^0-9]", " ");
String[] intSplit = intNum.split(" ");
for(int i=0;i<intSplit.length;i++){
System.out.println("int"+(i+1)+" "+intSplit[i]);
}
}
}
You are replacing every non-numeric character with a space. You need to replace it with "" so that there are no extra spaces. Then you split on space and you will get the desired result. Also, you need to preserve the spaces between numbers by using [^0-9\\s] so that the spaces are not replaced by "" (empty String).
Replace:
String intNum = str.replaceAll("[^0-9]", " ");
with:
String intNum = str.replaceAll("[^0-9\\s]", "");
Instead of wasting time (CPU resources) on modifying the string, just search for what you want:
Matcher m = Pattern.compile("[0-9]+").matcher(str);
for (int i = 1; m.find(); i++)
System.out.println("int" + i + " " + m.group());

How to find new line characyter

I want to know how can i read the contents of a file, character by character?
I tried this code
Scanner sc = new Scanner(new BufferedReader(newFileReader("C:\\saml.txt")));
while(sc.hasNext())
{
String s=sc.next();
char x[]=s.toCharArray();
for(int i=0;i<x.length;i++)
{
if(x[i]=='\n')
System.out.println("hello");
System.out.println(x[i];
}
I want to give input in file as:
"pure
world
it
is"
I want the output
"pure
hello
world
hello
it
hello
is
hello"
Two options:
Compare the string read against System#lineSeparator. If you use Java 6 or prior, use System.getProperty("line.separator");:
String s = sc.next();
if (System.lineSeparator().equals(s)) {
//...
}
Use Scanner#nextLine that will return a String until it finds a line separator character(s) (it will consume the line separator character(s) for you and remove it), then split the string by empty spaces and work with every string between spaces.
String s = sc.nextLine();
String[] words = s.split("\\s+");
for (String word : words) {
//...
}
Note that in any of these cases you don't need to evaluate each character in the String.
I would suggest using Scanner.nextLine and either using String.join(java 8+) or going for the manual method:
Without a trailing "hello":
while (sc.hasNext()){
String[] words = sc.nextLine().split(" ");
String sentence = words[0];
for(int i = 0; ++i < words.length;)
sentence += " hello " + words[i];
System.out.println(sentence);
}
With a trailing "hello":
while (sc.hasNext()){
for(String w : sc.nextLine().split(System.getProperty("line.separator")))
System.out.print(w + " hello ");
System.out.println();
}

How to re-prompt a user for input if they enter a string with multiple whitespaces

So i am wondering how I can check a string that has been inputed by the user for multiple spaces, and if it does have multiple spaces between words, i want the user to be re-prompted for input. I am using a do- while loop. here is the format:
Scanner scanner01 = new Scanner(System.in);
String inputLast;
do {
System.out.println("Enter a valid LAST name: ");
inputLast = scanner01.nextLine();
} while()
For explanation how to replace multiple spaces with single space read how-to-replace-2-or-more-spaces-with-single-space-in-string.
One solution would be this (based on this answer):
Scanner scanner01 = new Scanner(System.in);
String inputLast;
do {
System.out.println("Enter a valid LAST name: ");
inputLast = scanner01.nextLine();
if(inputLast.length() - inputLast.replaceAll(" ", "").length() <= 1){
break;
}
} while(true);
String str = "Sam ple";
Pattern pattern = Pattern.compile("\\s{2,}");
Matcher matcher = pattern.matcher(str);
boolean check = matcher.find();

Categories

Resources