Sub-Heading Here

im working with time in and time out app which compute all of the hours that you've been working.
and i got this error.! i dont know how to fix these. plss help me im a beginner.

error: variable th1 might not have been initialized
error: variable rh1 might not have been initialized
error: variable hh1 might not have been initialized

import java.util.Scanner;
public class fasdsga {



     public static void main(String[] args) {

            int ti,to,hh1,rh1,hh2,rh2;
            String th,th1;

            Scanner x = new Scanner(System.in);
            System.out.println("Time in:");
            ti=x.nextInt();
            System.out.println("Time out:");
            to=x.nextInt();
            System.out.println("Time in:");
            th=x.next();

            if(th.equals("y")){
                hh1=(to-(ti+100))/100;
                System.out.println("Holiday Hours:"+hh1);

            }
            else if(th.equals("n")){
                rh1=(to-(ti+100))/100;
                System.out.println("Holiday Hours:"+rh1);
            }
                else{
                System.out.println("......");
            }

        System.out.println("Time in:");
            ti=x.nextInt();
            System.out.println("Time out:");
            to=x.nextInt();
            System.out.println("Time in:");
            th=x.next();

            if(th.equals("y")){
                hh2=((to-(ti+100))/100)+hh1;
                System.out.println("Holiday Hours:"+hh2);

            }
            else if(th1.equals("n")){
                rh2=((to-(ti+100))/100)+rh1;
                System.out.println("Holiday Hours:"+rh2);
            }
            else{
                System.out.println("......");
            }
        }
    }

Means what it says. The compiler attempts to check all the paths through your code to make sure you always initialise variables before you try to use their values. It has seen that there are some combinations of the if tests where this may be a problem.
You fix it by ensuring that your variables are always initialised no matter what the results of the if tests may be. The simplest solution is usually to initialise them when you declare them, so they have a "default" value, eg
int x = 0; String s = "";

 int ti,to,hh1,rh1,hh2,rh2;
            String th,th1;

you add a value to these values by nextInt() (and such methods) but, the compiler will keep in mind that this might not always do what you intend it to do.
nextInt will read the next input until the first space, parse it to an int, and store it there, and it will do this as soon as you've pressed enter. but, what if you press enter without having entered a value? you will not have initialized those values, yet try to work with them.

that's why it's important to, as JamesCherrill pointed out, provide default values.

thanks guys ill dont forget what ive learned here. I must put a default value so that these error will not appear, every variable must have a default value(ill never forget this :D)

i swear if i got better onto these profesion ill help other, like you guys helping people. thnx

not every variable will always need a default value, but you must assign a value in such a way that it is impossible to come into a situation where you'll want to perform an action on a variable that hasn't been given a value yet.

yes sir. all of the variable with such a computation has to bee set a variable or it will not run. but i try sir to not put a default value for the string and it works. meaning in such computation they should be initialize variables before it performs such an action . thanks sir :D..

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.