Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Not initializing

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Location
    Cedar Rapids,Iowa
    Posts
    12
    My Mood
    Sick
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Not initializing

    Hi, I am stuck on my last method, no problem with the first two. Error lines are on lines 44(chill), 86(temperature), 86(windSpeed). All are "may not have initialized". I do not know why the first two are good but the last method is wrong.

     
    import java.util.Scanner;
    import java.io.Console;
     
    class WeatherProgram{
     
    	public static void main(String[]args){
     
    		double fahrenheit;
    		double celsius;
    		double temperature;
    		double windSpeed;
    		double chill;
    		int reply;
    		String reply2 = "y";
    		Console c = System.console();
    		Scanner scan = new Scanner(System.in);
     
    		while(reply2.equals("y")){
    			System.out.println("Welcome to Weather Conversion. Please choose one of the following numbers: "+"\n");
    			System.out.println("1. Fahrenheit to Celsius");
    			System.out.println("2. Celsius to Fahrenheit");
    			System.out.println("3. Windchill");
    			System.out.println("4. Quit");
    			System.out.println(" ");
    			reply = scan.nextInt();
     
    			if (reply == 1){
    				System.out.println("Please input fahrenheit temperature: ");
    				fahrenheit = scan.nextDouble();
    				fToC(fahrenheit);
    			}
     
    			if (reply == 2){
    				System.out.println("Please input celsius temperature: ");
    				celsius = scan.nextDouble();
    				cToF(celsius);
    			}
     
    			if (reply == 3){
    				System.out.println("Please input the temperature in degrees fahrenheit: ");
    				temperature = scan.nextDouble();
    				System.out.println("Please input wind speed in MPH: ");
    				windSpeed = scan.nextDouble();
    				wcCalc(chill);
    			}
    			reply2 = c.readLine("Do you wish to continue Weather Conversion, (y/n)?: ");
    		}
    		System.out.println("Thank you");
    	}
     
    	public static void fToC(double f){
    		double celsius;
    		celsius = ((f-32)*5/9);
     
    		if (celsius<1){
    			System.out.println(f + " Fahrenheit = " + celsius + " Celsius. Temperature is below freezing!");
    		}
    		if (celsius>32){
    			System.out.println(f + " Fahrenheit = " + celsius + " Celsius. It is hot!");
    		}
    		else{
    			System.out.println(f + " Fahrenheit = " + celsius + " Celsius.");
    		}
    	}
     
    	public static void cToF(double c){
    		double fahrenheit;
    		fahrenheit = ((c*9/5)+32);
     
    		if (fahrenheit<33){
    			System.out.println(c + " Celsius = " + fahrenheit + " Fahrenheit. Temperature is below freezing!");
    		}
    		if (fahrenheit>90){
    			System.out.println(c + " Celsius = " + fahrenheit + " Fahrenheit. It is hot!");
    		}
    		else{
    			System.out.println(c + " Celsius = " + fahrenheit + " Fahrenheit.");
    		}
    	}
     
    	public static void wcCalc(double wc){
    		double chill;
    		double temperature;
    		double windSpeed;
     
    		chill = (35.74 + 0.6215 * temperature + (0.4275 * temperature - 35.75) * Math.pow(windSpeed,0.16));
     
    		if (chill<-30){
    			System.out.println("Windchill = " + wc + " Degrees Fahrenheit. Dangerous Windchill!");
    		}
    		else{
    			System.out.println("Windchill = " + wc + " Degrees Fahrenheit");
    		}
    	}
    }


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Not initializing

    Yeah so why don't you initialize them?
    They are not your class variables, so they will not auto instantiate.
    And the error is because you have never assigned the values to them and you try to pass them to some function, so compiler will ofcourse ask you to initialize them.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Location
    Cedar Rapids,Iowa
    Posts
    12
    My Mood
    Sick
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Not initializing

    doesn't lines 41 and 43(temperature = scan.nextDouble; and windSpeed = scan.nextDouble) take user input for variable initialization? And why did the first two work, structure is the same.
    Last edited by Topflyt; November 23rd, 2011 at 01:29 PM.

  4. #4
    Junior Member
    Join Date
    Nov 2011
    Location
    Cedar Rapids,Iowa
    Posts
    12
    My Mood
    Sick
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Not initializing

    OK, I am stuck. This code works and the one above doesn't. I do not know/understand why variable of third method doesn't initialize, but these do.Help...
    import java.util.Scanner;
    import java.io.Console;
     
    class WeatherProgram{
     
    	public static void main(String[]args){
     
    		double fahrenheit;
    		double celsius;
    		int reply;
    		String reply2 = "y";
    		Console c = System.console();
    		Scanner scan = new Scanner(System.in);
     
    		while(reply2.equals("y")){
    			System.out.println("Welcome to Weather Conversion. Please choose one of the following numbers: "+"\n");
    			System.out.println("1. Fahrenheit to Celsius");
    			System.out.println("2. Celsius to Fahrenheit");
    			System.out.println("3. Windchill");
    			System.out.println("4. Quit");
    			System.out.println(" ");
    			reply = scan.nextInt();
     
    			if (reply == 1){
    				System.out.println("Please input fahrenheit temperature: ");
    				fahrenheit = scan.nextDouble();
    				fToC(fahrenheit);
    			}
     
    			if (reply == 2){
    				System.out.println("Please input celsius temperature: ");
    				celsius = scan.nextDouble();
    				cToF(celsius);
    			}
    			reply2 = c.readLine("Do you wish to continue Weather Conversion, (y/n)?: ");
    		}
    		System.out.println("Thank you");
    	}
     
    	public static void fToC(double f){
    		double celsius;
    		celsius = ((f-32)*5/9);
     
    		if (celsius<1){
    			System.out.println(f + " Fahrenheit = " + celsius + " Celsius. Temperature is below freezing!");
    		}
    		if (celsius>32){
    			System.out.println(f + " Fahrenheit = " + celsius + " Celsius. It is hot!");
    		}
    		else{
    			System.out.println(f + " Fahrenheit = " + celsius + " Celsius.");
    		}
    	}
     
    	public static void cToF(double c){
    		double fahrenheit;
    		fahrenheit = ((c*9/5)+32);
     
    		if (fahrenheit<33){
    			System.out.println(c + " Celsius = " + fahrenheit + " Fahrenheit. Temperature is below freezing!");
    		}
    		if (fahrenheit>90){
    			System.out.println(c + " Celsius = " + fahrenheit + " Fahrenheit. It is hot!");
    		}
    		else{
    			System.out.println(c + " Celsius = " + fahrenheit + " Fahrenheit.");
    		}
    	}
    }

  5. #5
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Stressed
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Not initializing

    You are declaring your variables more than once, but I am not sure why you're doing so.
    instead of declaring them inside of main, or inside of any other method, declare them in your class before main.

    import java.util.Scanner;
    import java.io.Console;
     
    class WeatherProgram{
     
    		private double fahrenheit;
    		private double celsius;
    		private double temperature;
    		private double windSpeed;
    		private double chill;
    		private int reply;
    		private String reply2 = "y";
     
    	public static void main(String[]args){
     
    		Console c = System.console();
    		Scanner scan = new Scanner(System.in);
     
    		while(reply2.equals("y")){
    			System.out.println("Welcome to Weather Conversion. Please choose one of the following numbers: "+"\n");
    			System.out.println("1. Fahrenheit to Celsius");
    			System.out.println("2. Celsius to Fahrenheit");
    			System.out.println("3. Windchill");
    			System.out.println("4. Quit");
    			System.out.println(" ");
    			reply = scan.nextInt();
     
    			if (reply == 1){
    				System.out.println("Please input fahrenheit temperature: ");
    				fahrenheit = scan.nextDouble();
    				fToC(fahrenheit);
    			}
     
    			if (reply == 2){
    				System.out.println("Please input celsius temperature: ");
    				celsius = scan.nextDouble();
    				cToF(celsius);
    			}
     
    			if (reply == 3){
    				System.out.println("Please input the temperature in degrees fahrenheit: ");
    				temperature = scan.nextDouble();
    				System.out.println("Please input wind speed in MPH: ");
    				windSpeed = scan.nextDouble();
    				wcCalc(chill);
    			}
    			reply2 = c.readLine("Do you wish to continue Weather Conversion, (y/n)?: ");
    		}
    		System.out.println("Thank you");
    	}
     
    	public static void fToC(double f){
    		// removed the double celcius, as it's already declared in class
    		celsius = ((f-32)*5/9); // check your "5/9" as that's not going to give you a double answer
     
    		if (celsius<1){
    			System.out.println(f + " Fahrenheit = " + celsius + " Celsius. Temperature is below freezing!");
    		}
    		if (celsius>32){
    			System.out.println(f + " Fahrenheit = " + celsius + " Celsius. It is hot!");
    		}
    		else{
    			System.out.println(f + " Fahrenheit = " + celsius + " Celsius.");
    		}
    	}
     
    	public static void cToF(double c){
    		//removed double fahrenheit
    		fahrenheit = ((c*9/5)+32);  // check 9/5 math, fix for double
     
    		if (fahrenheit<33){
    			System.out.println(c + " Celsius = " + fahrenheit + " Fahrenheit. Temperature is below freezing!");
    		}
    		if (fahrenheit>90){
    			System.out.println(c + " Celsius = " + fahrenheit + " Fahrenheit. It is hot!");
    		}
    		else{
    			System.out.println(c + " Celsius = " + fahrenheit + " Fahrenheit.");
    		}
    	}
     
    	public static void wcCalc(double wc){
     //removed all restatements of variables
    		chill = (35.74 + 0.6215 * temperature + (0.4275 * temperature - 35.75) * Math.pow(windSpeed,0.16));
     
    		if (chill<-30){
    			System.out.println("Windchill = " + wc + " Degrees Fahrenheit. Dangerous Windchill!");
    		}
    		else{
    			System.out.println("Windchill = " + wc + " Degrees Fahrenheit");
    		}
    	}
    }

  6. #6
    Junior Member
    Join Date
    Mar 2010
    Posts
    11
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Not initializing

    What Herah wrote will make your code work fine.. declare all the variables in class before any method rather than declare inside each method, by doing that if you call the variable outside of that particular method, compiler will complain because the variable only exist inside that particular method, outside of that method that variable does not exist, so the compiler is complaining about it, follow Herah's advise and that should make it work fine.. Hope this helps!

  7. #7
    Junior Member
    Join Date
    Nov 2011
    Location
    Cedar Rapids,Iowa
    Posts
    12
    My Mood
    Sick
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Not initializing

    Thank you for the tips, I am going to retry it tonight after stuffing turkey down my gullet all day. Happy Thanksgiving!!

  8. #8
    Member ziplague's Avatar
    Join Date
    Nov 2011
    Location
    Sweden
    Posts
    33
    My Mood
    Cheerful
    Thanks
    9
    Thanked 1 Time in 1 Post

    Default Re: Not initializing

    Hi, i tried compiling your code. Had to change all the variables to Private Static or else it says that they are not read locally ... after that it works, but throws an error:
    Exception in thread "main" java.lang.NullPointerException
    This error seems to be for the line where the user is supposed to input y/n

    import java.util.Scanner;
    import java.io.Console;
     
    public class WeatherProgram {
     
    	private static double fahrenheit;
    	private static double celsius;
    	private static double temperature;
    	private static double windSpeed;
    	private static double chill;
    	private static int reply;
    	private static String reply2 = "y";
     
     
    	public static void main(String[]args){
     
    		while(reply2.equals("y")){
     
    			Console c = System.console();
    			Scanner scan = new Scanner(System.in);
     
    			System.out.println("Welcome to Weather Conversion. Please choose one of the following numbers: "+"\n");
    			System.out.println("1. Fahrenheit to Celsius");
    			System.out.println("2. Celsius to Fahrenheit");
    			System.out.println("3. Windchill");
    			System.out.println("4. Quit");
    			System.out.println(" ");
    			reply = scan.nextInt();
     
    			if (reply == 1){
    				System.out.println("Please input fahrenheit temperature: ");
    				fahrenheit = scan.nextDouble();
    				fToC(fahrenheit);
    			}
     
    			if (reply == 2){
    				System.out.println("Please input celsius temperature: ");
    				celsius = scan.nextDouble();
    				cToF(celsius);
    			}
     
    			if (reply == 3){
    				System.out.println("Please input the temperature in degrees fahrenheit: ");
    				temperature = scan.nextDouble();
    				System.out.println("Please input wind speed in MPH: ");
    				windSpeed = scan.nextDouble();
    				wcCalc(chill);
    			}		
     
    			reply2 = c.readLine("Do you wish to continue Weather Conversion, (y/n)?: ");
     
    		}
    		System.out.println("Thank you");
    	}
     
    	public static void fToC(double f){
    		celsius = ((f-32)*5/9); // check your "5/9" as that's not going to give you a double answer
     
    		if (celsius<1){
    			System.out.println(f + " Fahrenheit = " + celsius + " Celsius. Temperature is below freezing!");
    		}
    		if (celsius>32){
    			System.out.println(f + " Fahrenheit = " + celsius + " Celsius. It is hot!");
    		}
    		else{
    			System.out.println(f + " Fahrenheit = " + celsius + " Celsius.");
    		}
    	}
     
    	public static void cToF(double c){
    		fahrenheit = ((c*9/5)+32);  // check 9/5 math, fix for double
     
    		if (fahrenheit<33){
    			System.out.println(c + " Celsius = " + fahrenheit + " Fahrenheit. Temperature is below freezing!");
    		}
    		if (fahrenheit>90){
    			System.out.println(c + " Celsius = " + fahrenheit + " Fahrenheit. It is hot!");
    		}
    		else{
    			System.out.println(c + " Celsius = " + fahrenheit + " Fahrenheit.");
    		}
    	}
     
    	public static void wcCalc(double wc){
    		chill = (35.74 + 0.6215 * temperature + (0.4275 * temperature - 35.75) * Math.pow(windSpeed,0.16));
     
    		if (chill<-30){
    			System.out.println("Windchill = " + wc + " Degrees Fahrenheit. Dangerous Windchill!");
    		}
    		else{
    			System.out.println("Windchill = " + wc + " Degrees Fahrenheit");
    		}
    	}
     
    }
    Last edited by ziplague; November 24th, 2011 at 01:11 PM.

  9. #9
    Member ziplague's Avatar
    Join Date
    Nov 2011
    Location
    Sweden
    Posts
    33
    My Mood
    Cheerful
    Thanks
    9
    Thanked 1 Time in 1 Post

    Default Re: Not initializing

    I've tried for hours to solve the (yes/no) input problem, even though it's way above my level for now, still i learned lots of interesting stuff along the way that halped me to solve some of my own problems , but i found something that i think might help:

    private boolean readYes (String prompt) {
    	string input = ""; //variable declaration: instead of input, you have reply2
    	while (!(input.equals("yes") || input.equals("no"))) {
    		System.out.print(prompt);
    		System.out.flush();
    		input = in.next();
    		input = input.toLowerCase();
    		in.nextLine;
    	}
    	return input.equals("yes");
    }

    hope this helps.

  10. #10
    Member ziplague's Avatar
    Join Date
    Nov 2011
    Location
    Sweden
    Posts
    33
    My Mood
    Cheerful
    Thanks
    9
    Thanked 1 Time in 1 Post

    Default Re: Not initializing

    Hi again )) I changed the code a bit, it's working now. I simply used Scanner instead of Console.

    import java.util.Scanner;
     
    public class WeatherProgram {
     
    	private static double fahrenheit;
    	private static double celsius;
    	private static double temperature;
    	private static double windSpeed;
    	private static double chill;
    	private static int reply;
    	private static int reply2 = 1;	
     
    	public static void main(String[]args){
     
    		while(reply2 == 1){
     
    			Scanner userInputNumber = new Scanner(System.in);
    			Scanner repeat = new Scanner(System.in);
     
    			System.out.println("Welcome to Weather Conversion. Please choose one of the following numbers: "+"\n");
    			System.out.println("1. Fahrenheit to Celsius");
    			System.out.println("2. Celsius to Fahrenheit");
    			System.out.println("3. Windchill");
    			System.out.println("4. Quit");
    			System.out.println(" ");
    			reply = userInputNumber.nextInt();
     
     
    			if (reply == 1){
    				System.out.println("Please input fahrenheit temperature: ");
    				fahrenheit = userInputNumber.nextDouble();
    				fToC(fahrenheit);
    			}
     
    			if (reply == 2){
    				System.out.println("Please input celsius temperature: ");
    				celsius = userInputNumber.nextDouble();
    				cToF(celsius);
    			}
     
    			if (reply == 3){
    				System.out.println("Please input the temperature in degrees fahrenheit: ");
    				temperature = userInputNumber.nextDouble();
    				System.out.println("Please input wind speed in MPH: ");
    				windSpeed = userInputNumber.nextDouble();
    				wcCalc(chill);
    			}
    			System.out.println("Do you wish to continue Weather Conversion? press 1 for yes or 2 for no: ");
    			reply2 = repeat.nextInt();
    		}
    		System.out.println("Thank you");
     
    	}
     
    	public static void fToC(double f){
    		// removed the double celcius, as it's already declared in class
    		celsius = ((f-32)*5/9); // check your "5/9" as that's not going to give you a double answer
     
    		if (celsius<1){
    			System.out.println(f + " Fahrenheit = " + celsius + " Celsius. Temperature is below freezing!");
    		}
    		if (celsius>32){
    			System.out.println(f + " Fahrenheit = " + celsius + " Celsius. It is hot!");
    		}
    		else{
    			System.out.println(f + " Fahrenheit = " + celsius + " Celsius.");
    		}
    	}
     
    	public static void cToF(double c){
    		//removed double fahrenheit
    		fahrenheit = ((c*9/5)+32);  // check 9/5 math, fix for double
     
    		if (fahrenheit<33){
    			System.out.println(c + " Celsius = " + fahrenheit + " Fahrenheit. Temperature is below freezing!");
    		}
    		if (fahrenheit>90){
    			System.out.println(c + " Celsius = " + fahrenheit + " Fahrenheit. It is hot!");
    		}
    		else{
    			System.out.println(c + " Celsius = " + fahrenheit + " Fahrenheit.");
    		}
    	}
     
    	public static void wcCalc(double wc){
     //removed all restatements of variables
    		chill = (35.74 + 0.6215 * temperature + (0.4275 * temperature - 35.75) * Math.pow(windSpeed,0.16));
     
    		if (chill<-30){
    			System.out.println("Windchill = " + wc + " Degrees Fahrenheit. Dangerous Windchill!");
    		}
    		else{
    			System.out.println("Windchill = " + wc + " Degrees Fahrenheit");
    		}
    	}
     
    }
    Last edited by ziplague; November 30th, 2011 at 03:34 AM.

Similar Threads

  1. Initializing Variables
    By Tjstretch in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: October 29th, 2011, 12:37 PM
  2. Nested-If Problem/ Initializing
    By ak120691 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 2nd, 2011, 07:37 PM
  3. initializing a java JApplet
    By j_a_lyons in forum Java Theory & Questions
    Replies: 0
    Last Post: January 8th, 2011, 03:49 PM

Tags for this Thread