New to java trying to use try catch finally. With try I am looking to make sure the string length is more than 1 character. I have something wrong but not sure what it is. Please assist.
So what compiler error are you getting? Hint: go and look through the String class for its length member.
I presume you are trying that out to see what happens if you get a NullPointerException. It's always a good idea to write silly bits of code and see what happens if . . .
Jeff Burel
Greenhorn
Joined: Jan 14, 2009
Posts: 12
posted
0
Newbie.java:5: illegal start of expression
for (int i = 0; i< x.length; i+) {
^
Newbie.java:5: not a statement
for (int i = 0; i< x.length; i+) {
^
2 errors
Vikas Kapoor
Ranch Hand
Joined: Aug 16, 2007
Posts: 1374
posted
0
You have used length function in two different ways. (Line 5 and 6) If your only goal is to try to find a length that you should use String#length function.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
4
posted
0
Check the correct spelling of the postfix increment operator, maybe here. That will disclose another error when corrected, I expect.
Andrew Alexander
Greenhorn
Joined: Jan 16, 2009
Posts: 3
posted
0
Newbie.java:5: illegal start of expression
for (int i = 0; i< x.length; i+) {
^
Newbie.java:5: not a statement
for (int i = 0; i< x.length; i+) {
^
2 errors
Your first error is due to x.length. To call the length method, you have to use parenthesis after length. Something like i<x.length();
Your second error is in fact due to what Campbell said. When you want to increment an int variable in a for loop, you have to use ++.
Here is your code with the two errors fixed:
GL,
Andrew
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
4
posted
0
Welcome to JavaRanch ( ) Andrew, and thank you, but (read the heading of the beginners' forum contents page) we prefer not to give "straight" answers. We think people learn a lot more if they work out the solution for themselves.