Results 1 to 2 of 2
  1. #1
    carl is offline Member
    Join Date
    Jul 2007
    Posts
    35
    Rep Power
    0

    Default Error: cannot be applied to (java.lang.String)

    Hi, I've been starting to go through my programs by doing one part at a time so i don't run into as many errors.
    Can someone please look over my open/read method?
    I'm getting 1 error when i attempt to compile it I've marked in code where it is
    It says this when i try to compile

    Java Code:
    Prog3test.java:28: openread(java.io.File) in Prog3test cannot be applied to (java.lang.String)
    String in = openread("input.txt");
    Java 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*******
              }
    }
    Thanks.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    // This method takes a File
    public String openread(File inputfile)
    
    // Here you are sending a String to a method
    // that takes a File. The types do not match.
    String in = openread("input.txt");
    
    // You could do
    String in = openread(new File("input.txt"));

Similar Threads

  1. Replies: 2
    Last Post: 11-26-2010, 03:12 AM
  2. Replies: 2
    Last Post: 09-18-2008, 08:30 AM
  3. Replies: 1
    Last Post: 08-02-2007, 05:07 PM
  4. Replies: 1
    Last Post: 08-01-2007, 07:10 AM
  5. Can't convert java.lang.String to int.
    By Albert in forum AWT / Swing
    Replies: 2
    Last Post: 07-13-2007, 06:05 PM

Posting Permissions

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