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

Click For Summary

Discussion Overview

The discussion revolves around a Java programming error related to an "Illegal start of expression" message encountered in a while loop. Participants analyze the code snippet provided and explore potential issues, including syntax errors and string comparison methods.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant identifies an unbalanced parenthesis in the while loop condition as a potential source of the error.
  • Another participant points out that using the != operator for string comparison in Java may not yield the expected results, suggesting the use of the equals method instead.
  • Concerns are raised about the possibility of an error occurring if the index variable j exceeds the length of the string info.
  • A participant proposes an alternative approach using the split method to achieve the same result without a loop, suggesting it may simplify the code.

Areas of Agreement / Disagreement

Participants express differing views on the correct approach to string comparison and the handling of potential index errors. There is no consensus on a single solution, as multiple perspectives and methods are presented.

Contextual Notes

Participants note limitations regarding the handling of string comparisons and potential index out-of-bounds errors, but do not resolve these issues definitively.

Jamin2112
Messages
973
Reaction score
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


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.
 


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

Wow. I'm an idiot.
 


The error is here:
Code:
while (info.substring(j, j))[/color] != " ") {
That right paren shouldn't be there.
 
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.
 


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.
 


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"}
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 21 ·
Replies
21
Views
4K
  • · Replies 12 ·
Replies
12
Views
11K
Replies
1
Views
2K