This is my code

import java.lang.Math; 
import java.io.*; 

public class TestDfunction { 

static void fun(float num1, float num2) throws IOException  
        { 
            BufferedReader read = new BufferedReader( new InputStreamReader(System.in)); 
         
            String strNum1, strNum2; 
             
            System.out.print("Enter the first number: ");  
            strNum1 = read.readLine(); 
            num1 = Float.parseFloat(strNum1); 
             
            System.out.print("Enter the second number: "); 
            strNum2 = read.readLine(); 
            num2 = Float.parseFloat(strNum2); 
         
        } 

   
static void menu() 
{ 
         
        System.out.println("Please choose the type of operation needs to be computed by pressing the corresponding number: ");
        System.out.println("----------------------------------------------------------------------------------------------");         
        System.out.println("                                    1.        Addition"); 
        System.out.println("                                    2.        Subtraction"); 
        System.out.println("                                    3.        Multiplication"); 
        System.out.println("                                    4.        Division"); 
        System.out.println("                                    5.        Remainder"); 
        System.out.println("                                    6.        Minimum"); 
        System.out.println("                                    7.        Maximum"); 
        System.out.println("----------------------------------------------------------------------------------------------");     
}       

    public static void main(String[] args) 
         
    { 
     
        menu();     
         
        try{ 
         
        BufferedReader read = new BufferedReader( new InputStreamReader(System.in)); 
         
            float num11, num22; 
            String strChoose; 
            int choose; 
             
            strChoose = read.readLine(); 
            choose = Integer.parseInt(strChoose); 
            System.out.println(""); 
         
        while ((choose <1) || (choose >8)) 
        { 
        System.out.println(); 
        System.out.println("The number you have entered is invalid..."); 
        menu(); 
        strChoose = read.readLine(); 
        choose = Integer.parseInt(strChoose);     
        } 
         
         
        switch (choose) 
        {     
             
        case 1:      
            fun(num11,num22); 
            System.out.println("The sum is: "+ (num11 + num22)); 
            break; 
             
         
        case 2:      
            fun(num11,num22); 
            System.out.println("The diferrence is: " + (num11 - num22)); 
            break; 
             
        case 3: 
            fun(num11,num22); 
            System.out.println("The product is: " + (num11 * num22)); 
            break; 
         
        case 4:      
            fun(num11,num22); 
            System.out.println("The quotion is: " + (num11 / num22)); 
            break; 

        case 5:      
            fun(num11,num22); 
            System.out.println("The remainder is: " + Math.IEEEremainder(num11,num22)); 
            break; 
             
        case 6:      
            fun(num11,num22); 
            System.out.println("The smaller value is: " + Math.min(num11,num22)); 
            break; 
             
        case 7:          
            fun(num11,num22); 
            System.out.println("The greater value is: " + Math.max(num11,num22)); 
            break; 
             
        case 8: 
             System.exit(0);                     
        } 
         
            }         
             
        catch (IOException e) {System.out.println("IO Error");} 
        } 
    }

Errors:
variable num11 might not have been initialized
variable num22 might not have been initialized

Dani AI

Generated

Two things are happening in this thread that cause the compiler complaint and the unintended behavior noted by in 's example.

First, Java requires local variables to be definitely assigned before they are read; the compiler will refuse to compile if it can see a path where a local is used without a guaranteed value. Second, assigning to primitive parameters inside a helper method does not change the caller’s locals because Java is pass-by-value for primitives (so the method is updating its own copies, not the originals). hinted at both issues; the program needs a way to get the read values back to main so the switch can use them.

Two practical approaches that fix the root cause:

  • Make the input helper return both numbers (recommended). For example, have a method return a two-element float[] or a small value object; call it before using the numbers and assign the returned values to num11 and num22.
  • Use a mutable holder the caller provides (an array or a simple container object) and let the helper fill its fields. Declaring the numbers as fields of the class will also work but reduces modularity; initializing the locals at declaration merely silences the compiler and does not solve the logic that the helper must actually supply the intended input.

Additional notes: validate parsing with try/catch for NumberFormatException, check for divide-by-zero in the division case, and consider Scanner or try-with-resources for cleaner input handling. For authoritative background on the language rules that matter here see the Java Language Specification on definite assignment and Oracle’s tutorial on how Java passes arguments to methods: JLS: Definite Assignment and Passing arguments to methods (Oracle Tutorial).

Recommended Answers

All 2 Replies

The error is pretty self explanatory. You never gave the variables a value.

float num11, num22;

That creates them but it does not initialize them with a value. If you give them a starting value, such as zero, your error is fixed; however your program does not work due to a logic error. Your method fun reads the values but never returns them. I think you should be able to figure it out from there.

Ahaaa

Will check that

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.