I've tried to define result many different ways, but I just can't get it to work. I know it's just something simple I am missing, but what is it? Can someone point me in the right direction? It currently says 'variable result might not have been initialized'.. but if I take out int result; in the findBestFit, it just shows four more errors saying it cannot find symbol variable result.
import java.util.Scanner;

public class folderSize
{

public folderSize()
{

        System.out.print("Specify the size of the destination folder: ");
        Scanner kboard = new Scanner(System.in);
        int folder = kboard.nextInt();
        System.out.print("Enter the size of the first file: ");
        int size1 = kboard.nextInt();
        System.out.print("Enter the size of the second file: ");
        int size2 = kboard.nextInt();

    int result = findBestFit(size1, size2, folder);

    }
    public int findBestFit(int size1, int size2, int folder)
    {
        int result;

        if (size1 + size2 < folder)
        {
            result = 3;
        }
        else if (size2 < folder)
        {
            result = 2;
        }

        else if (size1 < folder)
        {
            result = 1;
        }

        return result;
    }
}

'variable result might not have been initialized

Give it a value when you define it on line 20 and the compiler will be happy.

You should NOT have two variables with the same name: result. Rename one of them so they have different names.
Using the same name will be a confusion for you and anyone looking at the code.

Okay, but how do I print that result? Every time I try it says result is undefined.

Please post the code and the full text of the error message.

Cannot find symbol variable result, line 17.. If I define it outside of findBestFit, it doesn't make a difference

import java.util.Scanner;

public class Homework_7_18
{

public Homework_7_18()
{

    System.out.print("Specify the size of the destination folder: ");
    Scanner kboard = new Scanner(System.in);
    int folder = kboard.nextInt();
    System.out.print("Enter the size of the first file: ");
    int size1 = kboard.nextInt();
    System.out.print("Enter the size of the second file: ");
    int size2 = kboard.nextInt();

System.out.println(result);

}
public int findBestFit(int size1, int size2, int folder)
{
    int result = 0;

    if (size1 + size2 < folder)
    {
        result = 3;
    }
    else if (size2 < folder)
    {
        result = 2;
    }

    else if (size1 < folder)
    {
        result = 1;
    }

    return result;
}

}

System.out.println(result);

well i cant see where you defined the variable result, so i take it your trying to call the method findBestFit(int size1,int size2int folder) and get the returned value well to do that you create a variable and assign it with the call to the method findBestFit() like this:

    int returnResult=findBestFit(size1, size2, folder);
    System.out.println(returnResult);

I see that the variable: result is defined on line 14.
I do NOT see where result is used on line 17 (it looks like it is used on line 18)

The text of the error message does not make sense. Can you post the FULL text of the error message:

Here is a sample:

TestSorts.java:138: cannot find symbol
symbol  : variable var
location: class TestSorts
         var = 2;
         ^

That was the full error message of my compiler. With Corrupts solution, however, I get a new error..

java.lang.NoSuchMethodError: main
Exception in thread "main"
Process completed.

please post your full code including your test class

The class you are trying to execute with the java command does not have a correct main() method.

This website has the most frustrating user interface, I can't edit a single post. I tried to add a main method, but I get the error non-static method findBestFit(int,int,int) cannot be referenced from a static context

import java.util.Scanner;

public class Homework_7_18
{

public static void main(String[] args)
{

    System.out.print("Specify the size of the destination folder: ");
    Scanner kboard = new Scanner(System.in);
    int folder = kboard.nextInt();
    System.out.print("Enter the size of the first file: ");
    int size1 = kboard.nextInt();
    System.out.print("Enter the size of the second file: ");
    int size2 = kboard.nextInt();

    int returnResult = findBestFit(size1, size2, folder);
    System.out.println(returnResult);


}
public int findBestFit(int size1, int size2, int folder)
{
    int result = 0;

    if (size1 + size2 < folder)
    {
        result = 3;
    }
    else if (size2 < folder)
    {
        result = 2;
    }

    else if (size1 < folder)
    {
        result = 1;
    }

    return result;
}

}

import java.util.Scanner;

import java.util.Scanner;

public class Homework_7_18 {

    public static void main(String[] args) {
        System.out.print("Specify the size of the destination folder: ");
        Scanner kboard = new Scanner(System.in);
        int folder = kboard.nextInt();
        System.out.print("Enter the size of the first file: ");
        int size1 = kboard.nextInt();
        System.out.print("Enter the size of the second file: ");
        int size2 = kboard.nextInt();
        int returnResult = findBestFit(size1, size2, folder);
        System.out.println(returnResult);
    }

    public static int findBestFit(int size1, int size2, int folder) {
        int result = 0;
        if (size1 + size2 < folder) {
            result = 3;
        } else if (size2 < folder) {
            result = 2;
        } else if (size1 < folder) {
            result = 1;
        }
        return result;
    }
}

just had to add the static modifier to findBestFit

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.