Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9259379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:38:40+00:00 2026-06-18T12:38:40+00:00

I have the following Java code: import java.util.Scanner; public class C2F_F2C { public static

  • 0

I have the following Java code:

import java.util.Scanner;

public class C2F_F2C {


public static void main(String[] args) {
    boolean isNotValid = false;
    double toConvert;
    do {
        System.out.print("What do you want to convert from Celsius to Fahrenheit? ");
        Scanner in = new Scanner(System.in);
        String toConvertString = in.nextLine();
        isNotValid = false;
        try {
            toConvert = Double.parseDouble(toConvertString);
        }
        catch (java.lang.NumberFormatException e) {
            System.out.println("Error: Not a number");
            isNotValid = true;

        }
    } while (isNotValid);

    double inCelsius = toCelsius(toConvert);
    System.out.println("The value " + toConvert + " in Celsius is " + inCelsius);


}
public static double toCelsius( double fahrenheit ) {

    double celsius = (fahrenheit -32)*(5/9);
    return celsius;


}


}

However, when I run it, it throws the following error:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    The local variable toConvert may not have been initialized
    The local variable toConvert may not have been initialized

    at C2F_F2C.main(C2F_F2C.java:24)

I initialized the variable before the do..while loop, and set the value in the try..catch loop. It appears that the variable has not been set. Sorry if this is a rather basic question, but I can’t seem to figure it out.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:38:42+00:00Added an answer on June 18, 2026 at 12:38 pm

    No, you don’t initialize it before the loop and the try.

    This :

    double toConvert;
    

    is a declaration, not an initialization.

    This :

    double toConvert = 0.0;
    

    is an initialization.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import java.util.*; class HashingDemo { public static void main(String[] args) { Scanner keyboard =
I have following code in Java: import java.util.*; public class longest{ public static void
I have following code: import java.io.*; import java.util.concurrent.* ; public class Example{ public static
I have the following simple Java code: package testj; import java.util.*; public class Query<T>
please have a look at the following code import java.util.ArrayList; import java.util.List; public class
I have the following Java code: import java.util.Arrays; import java.util.Collections; public class Test {
I have the following code: import java.io.PrintStream; import java.io.UnsupportedEncodingException; import java.util.Locale; public final class
I have the following code: import java.util.*; public class SellTransaction extends Transaction { private
I have the following code: import java.util.*; public class Lab7 { /** * @param
I have the following Java code: import java.util.concurrent.*; class Foo{ static Semaphore s =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.