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

  • SEARCH
  • Home
  • 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 9094249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:18:26+00:00 2026-06-16T23:18:26+00:00

Having a problem displaying a string that is declared within an If statement, here

  • 0

Having a problem displaying a string that is declared within an If statement, here is the code:

import java.util.Scanner;

// This program reads a temp and prints its current state
public class P03_01_2 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        // get temp
        System.out.print("Enter a temperature: ");
        if (in.hasNextDouble()) {
            double temp = in.nextDouble();

            // determine temp type and determine state

            System.out.print("Enter C for Celcius or F for Fahrenheit: ");
            String type = in.nextLine();
            if (type.equals("c") || type.equals("C")) {
                if (temp <= 0) {
                    String state = "frozen";
                } else if (temp >= 100) {
                    String state = "gaseous";
                } else {
                    String state = "water";
                }
            }
            if (type.equals("f") || type.equals("F")) {
                if (temp <= 32) {
                    String state = "frozen";
                } else if (temp >= 212) {
                    String state = "gaseous";
                } else {
                    String state = "water";
                }
            } else if ((!type.equals("c") || !type.equals("C") || !type.equals("f") || !type.equals("F"))) {
                System.out.print("Not valid input.");
                System.exit(0);

            } else {
                System.out.println("Not valid input.");
                System.exit(0);
            }

            System.out.println("The state of the water is " + state);

        }
    }
}

I have tried putting “String state = null before” the first if statement, but keep getting errors…….Thank you.

  • 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-16T23:18:27+00:00Added an answer on June 16, 2026 at 11:18 pm

    Try:

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
    
        // get temp
        System.out.print("Enter a temperature: ");
        if (in.hasNextDouble()) {
            double temp = in.nextDouble();
    
            // determine temp type and determine state
    
            System.out.print("Enter C for Celcius or F for Fahrenheit: ");
            String type = in.nextLine();
            if (type.equals("c") || type.equals("C")) {
                if (temp <= 0) {
                    String state = "frozen";
                } else if (temp >= 100) {
                    String state = "gaseous";
                } else {
                    String state = "water";
                }
            }
            String state = null;
            if (type.equals("f") || type.equals("F")) {
                if (temp <= 32) {
                    state = "frozen";
                } else if (temp >= 212) {
                    state = "gaseous";
                } else {
                    state = "water";
                }
            } else if ((!type.equals("c") || !type.equals("C") || !type.equals("f") || !type.equals("F"))) {
                System.out.print("Not valid input.");
                System.exit(0);
    
            } else {
                System.out.println("Not valid input.");
                System.exit(0);
            }
    
            System.out.println("The state of the water is " + state);
    
        }
    }
    

    state has to be in scope… Be aware that it might now be null. This will not cause an error though…

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

Sidebar

Related Questions

I'm having a problem displaying an AlertDialog from within the onOptionsItemSelected method. That is,
Im having a problem with removing non-utf8 characters from string, which are not displaying
I'm having a problem displaying data from a function to text box within a
I'm having a really weird problem with a Google Chart that I'm displaying on
I'm having a problem displaying selected value with Html.DropDownListFor. I have a view that
I am having a very strange problem with pound signs displaying incorrectly (or not
hey there, Im having problems displaying my results in this program but the program
Im having problem in my UIscrollView ,this is what I have done: Whenever a
This problem is probably not specific to MFMailComposeViewController, but that is where I am
I'm having a problem with &. Basically I haven't been able to escape this

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.