0

I have been playing around and testing out some of the stuff I learned, and this isn't working for me for some reason. It's only mid-app, but I keep running it during development to make not have a thousand problems pile up by the time I'm done. It should be able to run as is though.

import java.util.Scanner;

public class Speed {
    public void speedAsker(){
        Scanner scan = new Scanner(System.in);

        System.out.println("Should we use: 1.KMPH or 2. MPH");
        int s1 = scan.nextInt();
        if(s1==1){
            String j1 = "KMPH";
            System.out.println("We will be using Kilometres for this calculation.");
        }if(s1 ==2){
            String j1 = "MPH";
            System.out.println("We will be using Miles for this calculation.");
        }else{
            System.out.println("That is an invalid input, you must choose between 1 or 2.");
        }
        System.out.println("What speed is your vehicle going in?");
        int d1 = scan.nextInt();
        System.out.println("Your vehicle was going at " + d1 + j1 + ".");
    }
}

This i what I got for output. The launcher class is just literally launching this class, I'm just doing it for good practice. The issue I'm having is trying to label j1 based on answer and use it in my output afterwards.

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
j1 cannot be resolved to a variable
at Speed.speedAsker(Speed.java:28)
at Launcher.main(Launcher.java:7)

Thanks in advance.

2
  • 1
    Not to be neat picking, but if you use abbreviations teachers and employers will hate you. Also, instead of strings for "MPH" you could use an enum, docs.oracle.com/javase/tutorial/java/javaOO/enum.html Commented May 28, 2013 at 20:19
  • Very cool, will study that up. Commented May 28, 2013 at 20:41

2 Answers 2

7

You declare it outside, and then define it inside the if/else

String j1;
if(s1==1){
    j1 = "KMPH";
    System.out.println("We will be using Kilometres for this calculation.");
}if(s1 ==2){
    j1 = "MPH";
    System.out.println("We will be using Miles for this calculation.");
}else{
    j1 = null;
    System.out.println("That is an invalid input, you must choose between 1 or 2.");
}
Sign up to request clarification or add additional context in comments.

2 Comments

In the else block why set j1 = null?
NoProblem! and @Freiheit, the two values that make sense are null or the empty string "". Honestly since it's an error state it probably would be best to throw an exception. But in the context i thought null was more appropriate
5

Declare your string outside the for loop, and assign it inside.

For example:

String j1;

if(s1==1){
    j1 = "KMPH";
    System.out.println("We will be using Kilometres for this calculation.");
}if(s1 ==2){
    j1 = "MPH";
    System.out.println("We will be using Miles for this calculation.");
}else{
    j1 = "";
    System.out.println("That is an invalid input, you must choose between 1 or 2.");
}

...

System.out.println("Your vehicle was going at " + d1 + j1 + ".");

Note that Java requires that local variables have definite assignment before they're used. The declaration "String j1" above does not provide a default value, so the else clause must either provide one or exit abnormally.

You can also provide a default value in the declaration:

 String j1 = "";

4 Comments

In the else block why set j1 = ""?
I was trying to make the minimum modification to the original code to answer the explicit question. You're right that the method should exit abnormally in the else case.
That is something I should definitely implement, I'll look up a way to set up an abnormal exit.
You can throw an exception if the function cannot complete successfully. If the caller can handle the exception, the caller can catch it. Otherwise, it will terminate the thread, which in your case is the main thread.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.