2

Sorry if this sounds dumb, but I can't seem to figure out how to use the variable that I defined in the If statements.

import java.util.Scanner;

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



        //Prompt for Enchantment Type
        System.out.println("Armor/Tool/Weapon");

        //Wait for response
        String input1 = scanner.nextLine();

        if(input1 == "Armor"){
            int type = 0;
        }
        else if(input1 == "Tool"){
            int type = 1;
        }
        else if(input1 == "Weapon"){
            int type = 2;
        }
        else {
            int type = 3;
        }

        //This is where I need to access the type int
        if(type == 1){

        }
    }
}

I can't figure out how to use the type string outside of it's block. I know that I'm supposed to read on scope and stuff, but I'd really like someone to just explain it to me.

6 Answers6

3

If you want to use variables outside an if-statement then you need to declare it outside the if-statement.

Also you don't use == to compare String objects for contents equality, that only compares their identity. Use the .equals() or .equalsIgnoreCase() method instead.

Make everything you can final, that way you know what you are dealing with.

import java.util.Scanner;

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

        //Prompt for Enchantment Type
        System.out.println("Armor/Tool/Weapon");

        final int type;

        //Wait for response
        String input1 = scanner.nextLine();

        if("Armor".equalsIgnoreCase(input1)){
            type = 0;
        }
        else if("Tool".equalsIgnoreCase(input1)){
            int type = 1;
        }
        else if("Weapon".equalsIgnoreCase(input1)){
            type = 2;
        }
        else {
            type = 3;
        }

        //This is where I need to access the String type
        if(type == 1){

        }
    }
}

A better way to do this is to switch on an Enum type.

public enum Item
{
    ARMOR,
    TOOL,
    WEAPON
}

Then you can convert your input1 into the Item enum and switch on that.

final Item item = Item.valueOf(input1.toUpperCase());

switch(item) {
   case(ARMOR):
     // do stuff here 
     break;
   case(TOOL):
     // do stuff here 
     break;
   case(WEAPON):
     // do stuff here 
     break;
   default:
     // do something with unrecognized input

This removes the magic numbers that you have in your program right now.

vallentin
  • 23,478
  • 6
  • 59
  • 81
3
  1. Declare the variable outside of the if's scope
  2. Use .equals to compare strings

Fixed code:

String input1 = scanner.nextLine();
int type; // scope

if(input1.equals("Armor")){
    int type = 0;
}
else if(input1.equals("Tool")){
    int type = 1;
}
else if(input1.equals("Weapon")){
    int type = 2;
}
else {
    int type = 3;
}

//This is where I need to access the String type
if(type == 1){

}

Note that you can also use a switch statement (only in Java 7!):

switch(input1) {
case "Armor":
    type = 0;
    break;
case "Tool":
    type = 1;
    break;
...

Also, in your case you could try indexOf:

import java.util.Arrays;

List<String> types = Arrays.asList("Armor", "Tool", "Weapon");
int type = types.indexOf(input1);
if (type == -1) type = 3; // if it's not found
tckmn
  • 57,719
  • 27
  • 114
  • 156
1

You should define the type outside the if condition

    int type = 0;
    if(("Armor".equalsIgnoreCase(input1)){
        type = 0;
    }
    else if("Tool".equalsIgnoreCase(input1)){
        type = 1;
    }
    else if("Weapon".equalsIgnoreCase(input1)){
        type = 2;
    }
    else {
       type = 3;
    }

    if(type == 4) {

    }
sanbhat
  • 17,522
  • 6
  • 48
  • 64
1

The scope of a variable is within the block it is defined in. So define the variable out of if blocks and use it.

int type = 0;
if(input1 == "Armor"){
}else if(input1 == "Tool"){
   type = 1;
 }
 else if(input1 == "Weapon"){
    type = 2;
  }
  else {
     type = 3;
  }

Note: Also use String equals method to compare strings instead of ==. Equals method checks whether the content of two strings are same, whereas == checks for objects equality.

Update your if and else blocks something like this:

int type = 0;
if("Armor".equals(input1)){
}else if("Tool".equals(input1){
   type = 1;
 }
 else if("Weapon".equals(input1)){
    type = 2;
  }
  else {
     type = 3;
  }
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

You are not supposed to access from out of scope. Just declare it in one of the enclosing scopes.

mvw
  • 5,075
  • 1
  • 28
  • 34
0

your code has multiple errors.

  1. you should define the type variable outside of the loop
  2. you should compare Strings with equals

import java.util.Scanner;

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



        //Prompt for Enchantment Type
        System.out.println("Armor/Tool/Weapon");

        //Wait for response
        String input1 = scanner.nextLine();
        int type;

        if(input1.equals("Armor")){
            type = 0;
        }
        else if(input1.equals("Tool")){
            type = 1;
        }
        else if(input1.equals("Weapon")){
            type = 2;
        }
        else {
            type = 3;
        }

        //This is where I need to access the String type
        if(type == 1){

        }
    }
}
thegrinner
  • 11,546
  • 5
  • 41
  • 64
Philipp Sander
  • 10,139
  • 6
  • 45
  • 78