File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Need help with read method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help with read method" Watch "Need help with read method" New topic
Author

Need help with read method

Logan Saturnius
Greenhorn

Joined: May 27, 2004
Posts: 18
Hi,
I've been starting to go through my programs by doing one small part at a time so i dont run into as many errors :/
can someone please look over my open/read method?
im getting 1 error when i attempt to compile it ive marked in code where it is
it says this when i try to compile


Prog3test.java:28: openread(java.io.File) in Prog3test cannot be applied to (java.lang.String)
String in = openread("input.txt");
^
-thanks

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

It doesn't seem possible that this error could have come from this code. The error message says that "openread()" wants a java.io.File argument, and that you're passing it a String. But in the code you're showing, while you are indeed passing in a String, the function accepts a String as an argument, so all should be well.

Make sure all your editor buffers are saved, and delete all your .class files, and make sure you know which version of your file you're compiling, and see if the problem doesn't just go away.


[Jess in Action][AskingGoodQuestions]
Logan Saturnius
Greenhorn

Joined: May 27, 2004
Posts: 18
argh sorry, i had multiple command prompts up and must have written the wrong error, its saying

Prog3test.java:28: non-static method openread(java.lang.String) cannot be referenced from a static context
String in = openread("input.txt");

(but if i take away the the 'static' in the main function it brings this error instead.)

Prog3test.java:23: variable line might not have been initialized
return(line);


:[
[ May 31, 2004: Message edited by: Logan Saturnius ]
Wai Hung
Greenhorn

Joined: Apr 15, 2004
Posts: 23
You need to create an instance of Prog3test and call openread() from that instance.
e.g. Prog3test testObj = new Prog3test();
String in = testObj.openread(...);
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

OK. First, openread() needs to be declared static just as main is. Second, since line is first assigned inside a try block, it's possible that it never gets assigned at all, which is why reading its value in your finally block is giving an error. You just need to initialize line when you declare it; initializing it to "null" is fine.
Logan Saturnius
Greenhorn

Joined: May 27, 2004
Posts: 18
Thankyou so much for your replies! got the first method working properly.

Now I was going to start a new thread for another question, but since this one is still new i'll just post it in here.
I've had a go at another method of the program (Using StringTokenizer, and the Integer.parseInt() method, which i've kinda never used before).
This time I have 4 errors when I try to compile, that i cannot seem to fix. I'll post the errors first and then the code.

-thanks

Prog3test2.java:30: invalid method declaration; return type required
public tokenize(String in)
(I dont understand why i have to return something to the main, when all im doing is setting values to s1,s2,s3)

Prog3test2.java:32: cannot resolve symbol
StringTokenizer st = new StringTokenizer(in);

Prog3test2.java:32: cannot resolve symbol
StringTokenizer st = new StringTokenizer(in);

Prog3test2.java:43: cannot resolve symbol
tokenize(in);


[ May 31, 2004: Message edited by: Logan Saturnius ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Originally posted by Logan Saturnius:

Prog3test2.java:30: invalid method declaration; return type required
public tokenize(String in)
(I dont understand why i have to return something to the main, when all im doing is setting values to s1,s2,s3)


Every Java method declaration must include a return type, even if it's just the "no return value" option "void", like main(). So here you need "public void tokenize..."


Prog3test2.java:32: cannot resolve symbol
StringTokenizer st = new StringTokenizer(in);

Prog3test2.java:32: cannot resolve symbol
StringTokenizer st = new StringTokenizer(in);



StringTokenizer is in the java.util package; you need an "import java.util.StringTokenizer;" at the top of your source file.


Prog3test2.java:43: cannot resolve symbol
tokenize(in);


Because you didn't successfully declare tokenize(), you get an error when you try to call it. Note that tokenize() needs to be static, too, for you to be able to call it from main().
Logan Saturnius
Greenhorn

Joined: May 27, 2004
Posts: 18
Ok I added "static void" to tokenize() and I added the java.util.StringTokenizer package at the top
my import list at top of source file looks like this

import java.io.*;
import java.util.StringTokenizer.*;
import B102.*; (This is just a JAR file from school that we use)

yet i still get the same following two errors on compile

Prog3test2.java:32: cannot resolve symbol
StringTokenizer st = new StringTokenizer(in);

Prog3test2.java:32: cannot resolve symbol
StringTokenizer st = new StringTokenizer(in);

-thanks
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

The ".*" at the end of an import statement means "all the classes in this package," implying here that java.util.StringTokenizer is a package, which of course its not. Go back and look at my previous message and do exactly what it says.
Logan Saturnius
Greenhorn

Joined: May 27, 2004
Posts: 18
Hi again, thanks for the info! got method two working
(By the way im sorry that I keep posting like this, but im learning though hehe)

Anyway I had a go at doing the third method which calculates if a triangle is valid and what type of triangle it is (triggers a switch function in main) and I have 1 error, but I dont understand how its missing a return statement when i am returning "type"
Oh how i wish to do one method without an error!. :roll:
I'll repost the new code
-thanks

Prog3test3.java:41: missing return statement
{


[ June 01, 2004: Message edited by: Logan Saturnius ]
Marco Davids
Greenhorn

Joined: Feb 11, 2004
Posts: 1
Hi Logan,

the reason for the error is that you return statement may not be reachable at the time of execution ...
Because it is in an if...else statement there is no guarantee that it will be reached when you execute the code ...

you must make sure that the return statement will be reach ...
you might want to move the return statement to the last line of your method and outside of the if...else statement


Hope this helps ....
Logan Saturnius
Greenhorn

Joined: May 27, 2004
Posts: 18
Thanks for the reply Marco

I put the return statement outside of the if/else statements and instead of having int type; i changed it to int type=0; then it worked

-thanks
[ June 01, 2004: Message edited by: Logan Saturnius ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help with read method