-2

I am experiencing this problem in Java. When I run the program it gives me the error: variable_name cannot be resolved to a variable.

if(name.equalsIgnoreCase("...")) {
    System.out.print("...");
    float name1=SIn.readFloat();
} else if(name.equalsIgnoreCase("...")){
    System.out.print("...");
    float name2=SIn.readFloat();
}
try {
    float converted=name1*valueget;    //the error is here with the variable 'name'
    System.out.println(...);
}
Morgan
  • 1
  • 3
  • 1) You didn't declare `name` 2) don't expect to be able to use `name1` and `name2` after you exit their respective `if`s because they would then be out of scope – Federico klez Culloca Apr 06 '21 at 07:22
  • the given snippet is not enough to determine the actual problem. however, from what i can already see, name is a String and cannot be multiplied with something to return a float – Manuel Jain Apr 06 '21 at 07:23
  • 1
    Please read [What is 'scope' in Java?](https://stackoverflow.com/questions/38177140/what-is-scope-in-java) – Federico klez Culloca Apr 06 '21 at 07:23
  • the variable name is declared further up, I have not transcribed it but it is there. – Morgan Apr 06 '21 at 07:23
  • 2
    @Morgan if you're asking about an error that says the variable `name` is not declared, the least you can do is showing us how and where you declared it, don't you think? – Federico klez Culloca Apr 06 '21 at 07:24
  • @FedericoklezCulloca seeing as he's calling equalsIgnoreCase on name already, name must exist somewhere. but I don't understand how he's trying to use it as a numerical value – Stultuske Apr 06 '21 at 07:26
  • Sorry, I spelled it wrong, now I've corrected it – Morgan Apr 06 '21 at 07:26
  • @Morgan then see my first comment. `name1` dies immediately after you declare it because it goes out of scope. Just declare a `float` variable before the first `if` and use that instead of `name1` and `name2`. (or, since you're assigning `name1` and `name2` the same value anyway, just declare the float and assign a value *after* the `if/else if`) Also, you might want to rethink calling a number "name". – Federico klez Culloca Apr 06 '21 at 07:27
  • okey, thanks for help – Morgan Apr 06 '21 at 07:28

1 Answers1

-1

The answer is simple, you are trying to work with variable that doesn't exist. Let's just say, that the first if returns false, then variable name1 never initializes.

if(name.equalsIgnoreCase("...")) {    // doesn't equal
    System.out.print("...");
    float name1 = SIn.readFloat();    // not initialized
} else if(name.equalsIgnoreCase("...")){
    System.out.print("...");
    float name2 = SIn.readFloat();
}
try {
    float converted = name1 * valueget;    // using non-existing variable
    System.out.println(...);
}

What you should do is, declare a variable before that if-else statement and initialize it inside the if-else.

float floatName = 0;   // set it up as zero, because you don't have else statement
if(name.equalsIgnoreCase("...")) {
    System.out.print("...");
    floatName = SIn.readFloat();
} else if(name.equalsIgnoreCase("...")){
    System.out.print("...");
    floatName = SIn.readFloat();
}
try {
    float converted = floatName * valueget;
    System.out.println(...);
}
AP11
  • 617
  • 4
  • 11
  • Your solution is correct, but this: *"Let's just say, that the first if returns false, then variable name1 never initializes."* is not. That's not how it works. Those variables (`name1` and `name2`) are out scope in the context they're used (`name1` inside the `try` block). What you say is true in some other languages, but not in java. – Federico klez Culloca Apr 06 '21 at 07:40