简体   繁体   中英

incompatible types error when providing a String to a method which requires a String

i am trying to pass a String value into a method which accepts Strings, however i am getting the error "incompatible types". i have tried hard coding a int value to see what error it gives which i expected it to:

found int; required: java.lang.String.

changed the method to accept File instead which errors:

found String; required: java.io.File.

ergo, i am feeding a string where a string should be. but i don't understand where i am going wrong. (and i have changed it back to feed String and accept String)

any feedback is welcome. thanks in advance :)

import java.io.*;
import java.util.*;
public class Test
{
    private ArgsReader inputFile;
    private String filename;
    private List<Pallet> entryBayQueue;
    /**
     * Constructor for objects of class Test
     */
    public Test()
    {
        inputFile = new ArgsReader();
        entryBayQueue = new LinkedList();
    }

    /**
     * methods
     */
    public void run(String[] args)
    {
        if(args.length > 0) //launched if you gave an argument
        {
            String line;
            filename = args[0];
            System.out.println(filename.getClass().getSimpleName()); //outputs string


            line = inputFile.stringFileReader(filename); //call method containing reading capability to read and store contents in line

            // ******* here is where the error occurs

            //System.out.println(line); //line = null here
            StringTokenizer st = new StringTokenizer(line);//tokenize line's contents
            while(st.hasMoreTokens())
            {
                System.out.println(st.nextToken());
                int serialNum = 0;
                switch(st.nextToken())
                {
                    case "A": 
                        {
                            serialNum++;
                            Pallet almondPallet = new Pallet(1,serialNum); //create new almond pellet and assign serial number
                            entryBayQueue.add(almondPallet); //adds pallet to the end of the list
                            break;
                        }
                }
            }

        }
        else //launched when you didn't provide an argument
        {
            filename = null;
            Console console = System.console();
            filename = console.readLine("file to read from: ");
            inputFile.stringFileReader(filename); //call method containing reading capability
        }
    }
}

// ******this is the implementation of stringFileReader

public void stringFileReader(String filename)
    {
        try
        {            
            input = new FileReader(filename); //open file for reading (string filename)
            buffReader = new BufferedReader(input); // read a line at a time

            line = buffReader.readLine(); //read 1st line
            while (line != null)
            {
                lineNum++;
                System.out.println(line);
                line = buffReader.readLine(); //read next line
            }

        }
        catch (IOException e){System.out.println("caught IOException");}
public void stringFileReader(String filename)

was supposed to be

public String stringFileReader(String filename)

method was not returning values when call is expecting.

thanks anyone who was helping.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM