Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Java questions.

  1. #1
    New to the CF scene
    Join Date
    Apr 2004
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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*******
              }
    }

  • #2
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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);

  • #3
    New to the CF scene
    Join Date
    Apr 2004
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thankyou for your reply,

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

    -thanks in advance

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •