This question already has answers here:
java - after splitting a string, what is the first element in the array?
(4 answers)
Closed 7 years ago.
I wrote the following Java code for testing the split() method in the String API.
import java.util.Scanner;
public class TestSplit {
public static void main(String[] args) {
String str = "10 5";
String[] integers = str.split(" ");
int numOfInt = integers.length;
for (int i = 0; i < numOfInt; i++) {
System.out.println(integers[i]);
}
}
}
I noticed that the above code gives me an output of
10
5
which is to be expected.
However, if I change the contents of str to " 10 5" then I get
10
5
as output. I don't understand why the output is different from the one above. If I am splitting str by using " " as a delimiter, then I thought that all " " will be ignored. So what is the extra space doing in my output?
EDIT: I tried " 10 5" and got
10
5
as output.
If you have a delimiter as the first character, split will return an empty String as the first element of the output array (i.e. " 10 5".split(" ") returns the array {"","10","5"}).
Similarly, if you have two consecutive delimiters, split will produce an empty String. So "10 5".split(" ") will produce the array {"10","","5"}.
If you wish leading and trailing whitespace to be ignored, change str.split(" "); to str.trim().split(" ");.
Related
This question already has answers here:
Use String.split() with multiple delimiters
(15 answers)
Java Splitting With Math Expression
(6 answers)
Closed 1 year ago.
I am working on a calculator in java. I have a method that parses the user input as a String to a double array. I take in a string, and convert it to a double array with a split method that takes a '+' as a delimiter. This code only works for addition operations. Here is that code:
public static double[] parseInput(String input){
// creates a String array that contains the elements of the input without the plus signs
String [] pieces = input.trim().split("\\+");
// creates a double array the same size as the string array previous to this
double[] nums = new double [pieces.length];
// loops through each index of the string array, parses and places the element into the double array
for(int i = 0; i < nums.length; i++){
nums[i] = Integer.parseInt(pieces[i]);
}
// returns the double array
return nums;
}
I need to make this work for other operations as well, such as subtraction, multiplication, etc... I also need to make it work for when the user does multiple operations in the same line, such as 1+3-2.
I am not worried about the math part of this, only the parsing. Bottom line is this. I need to make an array that contains the numbers being operated on, as well as the operations.
Bonus points if we can get it to work with spaces between numbers. This code currently works for "1+1" but breaks for "1 + 1".
Thank you in advance.
input:
double[] spacesNum = parseInput("4- 8 +3- 12* 34/ 45");
parser:
String [] pieces = input.trim().replaceAll("\\s+", "").split("[-+*/]");
String str = "word1, word2 word3#word4?word5.word6";
String[] arrOfStr = str.split("[, ?.#]+");
for (String a : arrOfStr)
System.out.println(a);
would give:
word1
word2
word3
word4
word5
word6
This should give your desired result
This question already has answers here:
Java String split removed empty values
(5 answers)
Closed 3 years ago.
I am actually working on a software that requires to read text files with some features that won't be explained here. While testing my code, I've found an anomaly which seems to come from the implementation of str.split("\r\n"), where str is a substring of the file's content.
When my substring ends with a succession of "\r\n" (several line breaks), the method completely neglects this part. For example, if I work with the following string:
"\r\nLine 1\r\n\r\nLine 2\r\n\r\n"
, I would like to get the following array;
["", "Line 1", "", "Line 2", "", ""]
, but it returns:
["", "Line 1", "", "Line 2"]
The String.split() Javadoc only notifies this without explaining:
... Trailing empty strings are therefore not included in the resulting array.
I cannot understand this asymmetry; why did they neglect empty string at the end, but not at the beginning?
The Javadocs explain why it works the way it does; you'd have to ask them why they chose this default implementation. Why not just call split(regex, n) as per the docs? Using -1 does what you say you want, just like the docs imply.
class Main {
public static void main(String[] args) {
String s = "\r\nLine 1\r\n\r\nLine 2\r\n\r\n";
String[] r = s.split("\\r\\n", -1);
for (int i = 0; i < r.length; i++) {
System.out.println("i: " + i + " = \"" + r[i] + "\"");
}
}
}
Produces:
i: 0 = ""
i: 1 = "Line 1"
i: 2 = ""
i: 3 = "Line 2"
i: 4 = ""
i: 5 = ""
You missed the part of the doc that explains the therefore, which states:
This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero.
Looking at the referenced two-arg doc shows
If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.
So this is just not the special case you want. Call with a negative integer instead:
str.split("\r\n", -1)
It's unclear why the authors thought 0 would be a more popular use-case than -1, but it doesn't really matter since the option you want exists.
This question already has answers here:
How to split a string with any whitespace chars as delimiters
(13 answers)
Closed 5 years ago.
I'm looking for a way to convert a string to an array and strip all whitespaces in the process. Here's what I have:
String[] splitArray = input.split(" ").trim();
But I can't figure out how to get rid of spaces in between the elements.
For example,
input = " 1 2 3 4 5 "
I want splitArray to be:
[1,2,3,4,5]
First off, this input.split(" ").trim(); won't compile since you can't call trim() on an array, but fortunately you don't need to. Your problem is that your regex, " " is treating each space as a split target, and with an input String like so:
String input = " 1 2 3 4 5 ";
You end up creating an array filled with several empty "" String items.
So this code:
String input = " 1 2 3 4 5 ";
// String[] splitArray = input.split("\\s+").trim();
String[] splitArray = input.trim().split(" ");
System.out.println(Arrays.toString(splitArray));
will result in this output:
[1, , , , , , , , 2, 3, 4, , , , , , 5]
What you need to do is to create a regex that greedily groups all the spaces or whitespace characters together, and fortunately we have this ability -- the + operator
Simply use a greedy split with the whitespace regex group
String[] splitArray = input.trim().split("\\s+");
\\s denotes any white-space character, and the trailing + will greedily aggregate one or more contiguous white-space characters together.
And actually, in your situation where the whitespace is nothing but multiples of spaces: " ", this is adequate:
String[] splitArray = input.trim().split(" +");
Appropriate tutorials for this:
short-hand character classes -- discusses \\s
repetition -- discusses the + also ? and * repetition characters
Try:
String[] result = input.split(" ");
This question already has answers here:
How to extract numbers from a string and get an array of ints?
(13 answers)
Closed 4 years ago.
I have a string like this 3x^2 I want to extract the first and the second number and store them in an array. and if they didn't exist they should be considered as 1.
EDIT :
For example in string x the first and the second number are 1
or in string 3x the second number is 1. I think it should be clear now.
Just get digits with the Regex:
String str = "3x^2";
String pattern = "(\\d+)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(str);
ArrayList<Integer> numbers = new ArrayList<>();
Find with Matcher all numbers and add them to the ArrayList. Don't forget to convert them to int, because m.group() returns the String.
while (m.find()) {
numbers.add(Integer.parseInt(m.group()));
}
And if your formula doesn't contain the second number, add there your desired default item.
if (numbers.size<2) {
numbers.add(1);
}
Finally print it out with:
for (int i: numbers) {
System.out.print(i + " ");
}
And the output for 3x^2 is 3 2.
And for the 8x it is 8 1.
if the numbers are allways separated by x^, just split the string using this separator
String[] splitted = "3x^2".split("x\\^");
I'm trying to create a Java program that is able to follow the order of operations when infix expressions are entered. To simplify input I've decided it would be best to split the entire String by using a regex expression. I've been able to split everything except the 6 3 into their own String values in the splitLine array. My SSCCE attempt at this is as follows:
String line = "6 + 5 + 6 3 + 18";
String regex = "(?<=[-+*/()])|(?=[-+*/()])"; //Not spitting 6 3 correctly
String[] splitLine = line.split(regex);
for (int i=0; i<splitLine.length; i++) {
System.out.println(splitLine[i]);
}
Output:
6
+
5
+
6 3 //Error here
+
18
Expected Output:
6
+
5
+
6 //Notice
3 //these
+
18
I've tried and tried to modify my regex expression and have been unsuccessful. Could anyone tell me why my regex expression isn't splitting the 6 and the 3 into their own Strings in the splitLine array?
Edit: Just wanted to point out that I am doing this for fun, it is not for any sort of school work, etc. I just wanted to see if I could write a program to perform simple infix expressions. I do agree that there are better ways to do this and if the expression were to become more complicated I would run into some issue. But unfortunately this is how my book recommended I approach this step.
Thanks again for all of the quick comments and answers!
try this : (?<=[-+*/()])|(?=[-+*/()]|\\s{2,})
you can try adding a space in the regex, this will also split when there is 2 or more space as in this case 6 and 3 is separated by space, 6 3 will also be separated. this regex will spit the string if more than 2 space is matched. You can change the minimum number of space as \s{min,} in the regex
Following regex would match your current input.
\s(\w+|[-+*/()])
The gist is to search for a whitespace followed by a word or specific character from your list.
Output
6
+
5
+
6
3
+
18
You can use something like that maybe?
String line = "6 + 5 + 6 3 + 18";
String regex = "(?<=[-+*/() ])|(?=[-+*/() ])"; //Added space to character class
String[] splitLine = line.split(regex);
for (int i=0; i<splitLine.length; i++) {
if (splitLine[i].trim().equals("")) // Check for blank elements
continue;
System.out.println(splitLine[i].trim());
}
Or you can split your string on (?<=\\d)\\s+(?=\\d) to get 6 + 5 + 6 and 3 + 18.