File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

try catch finally

 
Geoff Vurel
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.





[HENRY: Added Code Tags]
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have something wrong but not sure what it is. Please assist.


It generally helps if you provide more information -- like what error the compiler is showing when you try to compile it.

Henry
 
Campbell Ritchie
Sheriff
Pie
Posts: 47281
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 . . .
 
Geoff Vurel
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1374
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47281
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the correct spelling of the postfix increment operator, maybe here. That will disclose another error when corrected, I expect.
 
Andrew Alexander
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47281
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Andrew Alexander
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, that makes sense
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic