Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-31-2004, 09:25 AM   PM User | #1
Johnny Redburn
New to the CF scene

 
Join Date: Apr 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Johnny Redburn is an unknown quantity at this point
Java questions.

hi,
I've been starting to go through my programs by doing one 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

Code:
import java.io.*;
import B102.*;

class Prog3
{
          public String openread(File inputfile)
          {
          System.out.println("Opening File: " + inputfile.getName());
          BufferedReader input = null;
          String line;    
          try {
              input = new BufferedReader(new FileReader(inputfile));
              while((line = input.readLine())!= null)
                {
                break;
                }
          } catch(IOException in) {
                  System.out.println("There has been an error.");
          } finally { // closing file, even on exception
                try { input.close(); }
                catch (Exception e) {}
                }
            return(line);
          }
          
          public static void main(String[] args)
          {
          	String in = openread("input.txt");  *******error here*******
          }
}
Johnny Redburn is offline   Reply With Quote
Old 05-31-2004, 10:24 AM   PM User | #2
shmoove
Regular Coder

 
Join Date: Dec 2003
Posts: 367
Thanks: 0
Thanked 0 Times in 0 Posts
shmoove is an unknown quantity at this point
Your openread method accepts a File argument but you are sending it a string. I'm not that well-versed in J2SE but from looking at your code it looks like openread should be accepting a String:
Code:
public String openread(String inputfile)
shmoove

Edit:
One more thing. If you make that change then this line:
Code:
System.out.println("Opening File: " + inputfile.getName());
will give you an error, so you'll probably want to change it to:
Code:
System.out.println("Opening File: " + inputfile);
shmoove is offline   Reply With Quote
Old 05-31-2004, 12:35 PM   PM User | #3
Johnny Redburn
New to the CF scene

 
Join Date: Apr 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Johnny Redburn is an unknown quantity at this point
Thankyou for your reply,

however i am still getting the same error as i stated above :[
any other suggestions?

-thanks in advance
Johnny Redburn is offline   Reply With Quote
Old 06-01-2004, 12:00 AM   PM User | #4
Antoniohawk
Senior Coder

 
Join Date: Aug 2002
Location: Kansas City, Kansas
Posts: 1,518
Thanks: 0
Thanked 2 Times in 2 Posts
Antoniohawk will become famous soon enough
The object that you're returning shouldn't be in parentheses should it?
Code:
return(line);
...
return line;
Try the following code.
Code:
import java.io.*;
import B102.*;

class Prog3
{
          public String openread(File inputfile)
          {
          System.out.println("Opening File: " + inputfile.getName());
          BufferedReader input = null;
          String line;    
          try {
              input = new BufferedReader(new FileReader(inputfile));
              while((line = input.readLine())!= null)
                {
                break;
                }
          } catch(IOException in) {
                  System.out.println("There has been an error.");
          } finally { // closing file, even on exception
                try { input.close(); }
                catch (Exception e) {}
                }
            return line;
          }
          
          public static void main(String[] args)
          {
                File inputFile = new File("input.txt");
          	String in = openread(inputFile);
          }
}

Last edited by Antoniohawk; 06-01-2004 at 12:05 AM..
Antoniohawk is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:21 AM.


Advertisement
Log in to turn off these ads.