Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 132,642 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,094 people online right now. Registration is fast and FREE... Join Now!




Initilization Error

 
Reply to this topicStart new topic

Initilization Error

Amy.A
post 11 Oct, 2008 - 05:33 AM
Post #1


New D.I.C Head

*
Joined: 11 Oct, 2008
Posts: 5

Hi....

This is my code...

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");}
        }
    }


I received this error:

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


What's the wrong??

This post has been edited by Amy.A: 11 Oct, 2008 - 05:59 AM
User is offlineProfile CardPM

Go to the top of the page

Gloin
post 11 Oct, 2008 - 06:53 AM
Post #2


On MeD.i.Cation

Group Icon
Joined: 4 Aug, 2008
Posts: 717



Thanked 46 times
My Contributions


case 1:
fun(num11,num22);
System.out.println("The sum is: "+ (num11 + num22));
break;


When you get to this part of the program, the variables num11 and num22 have not yet been initialized.

A few rows before, you declare the variables.

float num11, num22;

But you never give them a value.

Just write,

float num11 = 0;
float num22 = 0;

That will remove the warnings, although it may not give you the output you want.
User is offlineProfile CardPM

Go to the top of the page

Gloin
post 11 Oct, 2008 - 07:06 AM
Post #3


On MeD.i.Cation

Group Icon
Joined: 4 Aug, 2008
Posts: 717



Thanked 46 times
My Contributions


Read through your program. Rather unconventional way of writing an input procedure. It's actually possible that the values you assign to num1 and num2 will only be valid within the method. I.e num11 and num22 will remain zero after the method call.

If that happens, you can change the method to look like this:
CODE

static float fun(int i) throws IOException
{
            BufferedReader read = new BufferedReader( new InputStreamReader(System.in));
            String strNum;
            
            System.out.print("Enter number " + i + ": ");
            strNum = read.readLine();
            num = Float.parseFloat(strNum);
            return num;  
}


call the method with

num11 = fun(1);
num22 = fun(2);

I would have used the Scanner class instead though and its nextFloat-method.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 04:38AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month