Search code examples
javaoopbluej

Executing program gives int cannot be converted into java.lang.string


I am currently using BlueJ (forced to by the module tutor, I hate it) and I'm having an error come up every time I attempt to execute the code.

incompatible types: java.lang.String cannot be converted into int

My code is as follows:

public class middle 
{
public static void main (String[] args)
{
    String numeroUno = args[0];
    String numeroDos = args[1];
    String numeroTres = args[2];

    double num1 = Double.parseDouble(args[0]);
    double num2 = Double.parseDouble(args[1]);
    double num3 = Double.parseDouble(args[2]);

    middle(num1, num2, num3);
}

public static void middle(double n1, double n2, double n3)
{
    double [] values = {n1, n2, n3};
    double newarr;
    boolean sorted = false;

    while(!sorted)
    {
        sorted = true;
        for(int i=0; i<values.length-1; i++)
        {
            if(values[i] > values[i+1])
            {
                double swapsies = values[i+1];
                values[i+1] = values[i];
                values[i] = swapsies;
                sorted = false;
            }
        }
    }

    System.out.print(values[1] + " is between " + values[0] + " and " + values[2]);

}
}

Firstly my question is where have I made the error, but secondly, is there another way to structure this code i.e completely rewrite it to achieve the same thing. I'm not really used to writing code this way and I'm having a hard time with OOP. The object of this particular exercise is to write code that will return the middle value number from what the user has input.

Thanks!


Solution

  • BlueJ's parameter-interfaces that are used to call methods or constructors are designed to take arguments in java-syntax. So for example the parameter window for main would expect input in the form:

    {"2" , "3" , "4"}
    

    Note the double-quotes just like in java. In fact this principle is extensible for any kind of constructor. For example one could as well give {new String("2"), new String("3"), ... as arguments.