[Java] Can't figure out this Illegal start of expression error

In summary, you are trying to use the split function on a string that is too long. You can either use a loop to iterate through the string or use the split function to get the first element.
  • #1
Jamin2112
986
12
[Java] Can't figure out this "Illegal start of expression" error

From my Java code:

while (info.substring(j, j)) != " ") {
name += info.substring(j, j);
j++;
}


gives an "illegal start of expression" error
 
Technology news on Phys.org
  • #2


Jamin2112 said:
From my Java code:

while (info.substring(j, j)) != " ") {
name += info.substring(j, j);
j++;
}


gives an "illegal start of expression" error
Assuming this is a verbatim copy/paste from your code, you seem to have an unbalanced parenthesis.
 
  • #3


jbunniii said:
Assuming this is a verbatim copy/paste from your code, you seem to have an unbalanced parenthesis.

Wow. I'm an idiot.
 
  • #4


The error is here:
Code:
while (info.substring(j, j)[color="red"])[/color] != " ") {
That right paren shouldn't be there.
 
  • #5
Can't figure out this "Illegal start of expression" error

One other thing to note: != is not doing what you probably want for Strings. Strings aren't a primitive type in Java, so the != operator is comparing the objects references. That operation evaluates to true only if the two strings are not the same object. You should use the equals method: !a.equals(b) to test if the value of the strings a and b are not equal.
 
  • #6


gbeagle said:
One other thing to note: != is not doing what you probably want for Strings. Strings aren't a primitive type in Java, so the != operator is comparing the objects references. That operation evaluates to true only if the two strings are not the same object. You should use the equals method: !a.equals(b) to test if the value of the strings a and b are not equal.
You are also going to hit an error when the value of j is larger than the length of info.
 
  • #7


Also an alternative simpler way to do the same thing without looping is:

Code:
name = info.split(" ")[0];

The split function will split the String on " " returning an array of Strings. Getting the first element in the array gives you everything up to the first space.

(Ex: if info is "foo bar baz" then info.split(" ") returns {"foo", "bar", "baz"}
 

FAQ: [Java] Can't figure out this Illegal start of expression error

1. What does the "Illegal start of expression" error mean?

The "Illegal start of expression" error in Java means that there is a syntax error in your code. This usually occurs when there is a missing or incorrect symbol, keyword, or punctuation. It can also indicate that there is a problem with the structure of your code, such as missing curly braces or incorrect indentation.

2. How do I fix the "Illegal start of expression" error?

To fix the "Illegal start of expression" error in Java, you will need to carefully review your code and check for any missing or incorrect symbols, keywords, or punctuation. You should also check the structure of your code and make sure it follows the correct formatting rules, such as using curly braces to enclose blocks of code and proper indentation.

3. Why am I getting an "Illegal start of expression" error even though my code looks correct?

If you are getting an "Illegal start of expression" error in Java even though your code looks correct, it could be due to a mistake in a previous line of code. Often, a small error in one line of code can cause issues in subsequent lines. Make sure to carefully review your code from the beginning to ensure that all syntax is correct.

4. Can the "Illegal start of expression" error be caused by a typo?

Yes, the "Illegal start of expression" error in Java can be caused by a simple typo. If you misspell a keyword or use the wrong punctuation, it can result in this error. Make sure to double-check your code for any typos or spelling errors.

5. Are there any tools to help identify and fix "Illegal start of expression" errors in Java?

Yes, there are several tools that can help identify and fix "Illegal start of expression" errors in Java. IDEs such as Eclipse and IntelliJ have built-in error checking features that can highlight syntax errors in your code. You can also use online code validators or code review tools to identify any errors in your code.

Similar threads

Replies
2
Views
1K
Replies
3
Views
2K
Replies
6
Views
2K
Replies
1
Views
1K
Replies
4
Views
3K
Replies
5
Views
2K
Replies
7
Views
2K
Replies
12
Views
9K
Back
Top