aspose file tools*
The moose likes Beginning Java and the fly likes try catch finally Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "try catch finally" Watch "try catch finally" New topic
Author

try catch finally

Geoff Vurel
Greenhorn

Joined: Jan 14, 2009
Posts: 12
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
Sheriff

Joined: Sep 28, 2004
Posts: 18846
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
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

Joined: Jan 14, 2009
Posts: 12
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
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: 38881
    
  23
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
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: 38881
    
  23
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

Joined: Jan 16, 2009
Posts: 3
Ok, that makes sense
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: try catch finally