• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Need help with read method

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Logan Saturnius
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic