Why does Java 8's split not produce the last token [duplicate] - java

This question already has answers here:
Java String split removed empty values
(5 answers)
Closed 6 years ago.
I would expect the following Java code to split a string into three items:
String csv = "1,2,";
String[] tokens = csv.split(",");
System.out.println(tokens.length);
However, I am only getting two items.
I must admit that I did not analyze this very deeply, but it seems counter-intuitive to me. Both Python and C# generate three items, as follows, in Python:
def test_split(self):
line = '1,2,'
tokens = line.split(",")
for token in tokens:
print('-' + token)
-1
-2
-
and in C#:
[Test]
public void t()
{
string s = "1,2,";
var tokens = s.Split(',');
foreach (var token in tokens)
{
Console.WriteLine("-" + token);
}
}
-1
-2
-
What am I missing?
This is Java 1.8.0_101.

Use overloaded version of the method:
tokens = line.split(",", -1)

The documentation is clear on this behavior:
This method works as if by invoking the two-argument split method with
the given expression and a limit argument of zero. Trailing empty
strings are therefore not included in the resulting array.

Related

Java 8, updating element in array [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
I have trouble with my code. I need to replace element in Array if condition is true.
Inputs are:
dashedCapital = "------";
input = "a";
capital = "Warsaw";
Code should check if capital contains input and if yes replace "-" in dashedCapital to character from input at specified position:
public static String changeDashedCapital(String dashedCapital, String input, String capital){
String[] capitalArray = capital.split("");
String[] dashedCapitalArray = dashedCapital.split("");
String[] character = input.split("");
for(int i = 0; i < capitalArray.length; i++){
//System.out.println(i);
//System.out.println(capitalArray[i] + character[0] + dashedCapitalArray[i]);
if(capitalArray[i] == character[0]){
dashedCapitalArray[i] = character[0];
}
}
String result = Arrays.toString(dashedCapitalArray);
System.out.println(result);
return result;
}
Result is "------" but should be "-a--a-". What's going wrong?
John, thanks for your reply, it was helpful.
I edited my method so it's look like this now:
public static String changeDashedCapital(String dashedCapital, String input, String capital){
for(int i = 0; i < capital.length(); i++){
if(capital.charAt(i).equals(input.charAt(0))) {
String new_dashed = dashedCapital.substring(0,i)+input.charAt(0)+dashedCapital.substring(i);
System.out.println(new_dashed);
}
}
return "OK:";
Now i get this error:
GetWord.java:69: error: char cannot be dereferenced
if(capital.charAt(i).equals(input.charAt(0))) {
^
1 error
I don't understand why it's wrong. I using a equals() function. I also tried "==" operator but then nothing happens. What does it mean "char cannot be dereferenced"? How I could compare single chars from string with another chars from another string?
The reason it is not working is because your if for character equality is never true. You’re comparing strings of length 1 and not characters. You can quickly fix by changing if be using the string comparing function .equals()
if(capitalArray[i].equals(character[0])){
...
}
However, you should change your code and not just use this fix. Don’t split your stings into arrays, just use the .charAt() method to get a character at a particular index.

How to substring a String containing 4 bytes characters? [duplicate]

This question already has answers here:
How to get the substring that contains the first N unicode characters in Java
(2 answers)
Closed 5 years ago.
I have a String that could contain 4 bytes characters. For example:
String s = "\uD83D\uDC4D1234\uD83D\uDC4D";
I also have a size that I should use to get a substring from it. The size is in characters. So let's say that size is 5, so I should get the first 4 bytes character along with "1234".
Directly using substring as s.substring(0, 5) gives the wrong result returning the first character and just "123".
I could manage to get the right result using code points this way:
String s = "\uD83D\uDC4D1234\uD83D\uDC4D";
StringBuffer buf = new StringBuffer();
long size = 5;
s.codePoints().forEachOrdered(charInt -> {
if(buf.codePoints().count() < size) {
buf.appendCodePoint(charInt);
}
});
I bet there should be a way better and more efficient code to achieve this.
You can use offsetByCodePoints in order to help find the index of the character following 5 code points, and then use that as the second parameter to substring:
String s = "\uD83D\uDC4D1234\uD83D\uDC4D";
String sub = s.substring(0, s.offsetByCodePoints(0, 5));
Ideone Demo

why no type mismatch error in string split method when run in loop? [duplicate]

This question already has answers here:
What Does The Colon Mean In Java?
(5 answers)
Closed 7 years ago.
I have a string handling related question
split() - The return type is String[]
In this for loop we are storing the split value in a String literal
for (String retval: Str.split("-"))
Why doesn't it give a type mismatch error like in the code below?
String Str1 = "abfg-hjddh-jdj";
String Str = Str1.split("-");
String Str = Str1.split("-");
gives error because split returns an array, so the correct syntax is:
String[] Str = Str1.split("-");
In a for-each loop
for (String retval : Str.split("-"))
For each loop : indicates you will be iterating an array, collection or list of Strings, so no error is trhown
Examples:
for (int retval : string.split("-")) // error,
ArrayList<Books> books;
for (Book book : books) // correct
Set<Integer> integers;
for (Integer mInt : integers) // correct
for (String mInt : integers) // incorrect!!!
LAST BUT NOT LEAST: variables in Java should start with LOWERCASE, please check Code Conventions.

Parsing Java String type Error

What i'm trying to do is incorporate interface methods to complete a task given the variables inside of a string. The string i'm given, "s" can be made up numbers, +, -, and * symbols. The integer return is fairly easy as all i'm doing is returning an integer interface method of that int. However, for the other 3 symbols, I need to recursively incorporate a method to find the left and right nodes. I've posted my code below...
public static Expression parseString( String s ) {
String[] parse = s.split("\\s+");
String[] parsecopy;
Expression exp1;
Expression exp2;
if(s == null) {
return null;
}
if(parse[0].equals("+")) {
exp1 = parseString(parse[0]);
parsecopy = Arrays.copyOfRange(parse, 2, parse.length);
exp2 = parseString(parsecopy);
return new AddExpression(exp1, exp2);
}
else if() {
The problem - So my code creates a copy of the original string to find the next item in that string. I do this by using the Array function, copyOfRange(). However, when I want to call exp2 = parseString(parsecopy), i'm receiving an error because parseString takes in a string argument which has to be of the type String[]. The reason i'm trying to get parsecopy instead of parsecopy[0] is because parsecopy wouldn't create an endless loop and I would actually be able to iterate through the string.
Error code - The method parseString(String) in the type Parse is not applicable for the arguments (String[])
if(parse[0].equals("+")) {
exp1 = parseString(parse[0]);
parsecopy = Arrays.copyOfRange(parse, 2, parse.length);
exp2 = parseString(parsecopy);
return new AddExpression(exp1, exp2);
}
exp1 = parseString(parse[0]);
you are doing recursive calling here.
Since the parameter you pass to split is a regex, you can simply do:
String[] ss = "12121+34234 *23423 -123123 *123123-12312+1231231-123123".split("\\s?[\\+\\-\\*]\\s?");
this way you split your string wherever you got a +, - , or * (possibly with a whitespace after or before).
And please do the null-check of the string before split it :D
Hope it helps.
It seems like you want to check parse[1] equals "+" rather than parse[0].
You would expect 1 + 2 rather than + 1 2.

How to properly use a java substring in an if test? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I'm reading a file line-by-line with Java and parsing the data in each line for a report. Periodically, there will be a URL that's been written to a line, and I want to bypass that line. I need to set up an if...then that tests for the presence of a URL -- in this specific case, I define that as the first four bytes of the line reading http. If the condition is true, read the next line in the file, but if the condition is false, send the current line to the parser for report output.
Simple, no? I'm running into issues where I cannot get my if..then statement to accurately recognize a true condition. My only clue is that when my inputString is equal to the test value, it passes as true. Beyond that...I can't spot anything out of the ordinary.
public class testClass
{
public static void main(String args[])
{
String inputString1 = "http://www.google.com/r";
String inputString2 = "google";
String inputString3 = "google search";
String inputString4 = "http";
parseInput(inputString1);
parseInput(inputString2);
parseInput(inputString3);
parseInput(inputString4);
}
//
private static void parseInput(String inputString8) {
int httpFlag;
if (inputString8.substring(0,4) == "http")
{ httpFlag = 1467; } else
{ httpFlag = 10644; }
System.out.println(inputString8+" "+inputString8.toLowerCase().substring(0,4)+" "+httpFlag);
}
}
Here's my output:
http://www.google.com/r http 10644
google goog 10644
google search goog 10644
http http 1467
I am expecting 1467 for both inputString1 and inputString4.
The console appears to display my input and substring properly, and nothing in the Java Documentation suggests that my substring would be a char array or anything apart from a string, so I can't figure out why http as the first four bytes of a 4-byte string is "different" from the first four bytes of a (4+x)-byte string. What am I missing?
Compare strings with equals, not ==. There's nothing special about the fact the string you're comparing is the result of calling substring.
use if (inputString8.substring(0,4).equals("http")).
== operator checks if the two expressions are the same object, you want to check if they have the same value.
== tests to see if two things are the same object. If you want to compare strings char by char use the equals () method of the String class.

Categories