Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert String to int, but both are strings?

I am just messing around with some exercises in a book for a Java summer class because I am a little ahead, meaning this is not homework. I'm getting an error message stating you cannot convert from String to int but both are Strings, one is a variable and one an Array.

It's this line I'm having trouble with... select = items[select];

public class CarpentryChoice {

    public static void main(String[] args) {
        String items [] = {"Table", "Desk", "Chair", "Couch", "Lazyboy"};
        int price [] = {250, 175, 125, 345, 850};

        String select;

        Scanner scan = new Scanner(System.in);

        System.out.println("Please enter an item to view it's price: ");
        select = scan.nextLine();

        for(int i=0; i < items.length; i++) {
            select = items[select];
        }       
    }
}
like image 803
NoobCoderChick Avatar asked Jun 29 '15 04:06

NoobCoderChick


People also ask

How do you fix a string that Cannot be converted to int?

The most direct solution to convert a Java string to an integer is to use the parseInt method of the Integer class: int i = Integer. parseInt(myString); parseInt converts the String to an int , and throws a NumberFormatException if the string can't be converted to an int type.

Can you convert strings to ints?

Use Integer.parseInt() to Convert a String to an Integer This method returns the string as a primitive type int. If the string does not contain a valid integer then it will throw a NumberFormatException.

Can string be converted to integer in Python?

To convert, or cast, a string to an integer in Python, you use the int() built-in function. The function takes in as a parameter the initial string you want to convert, and returns the integer equivalent of the value you passed.

Can we convert string to int in Java?

We can convert String to an int in java using Integer.parseInt() method. To convert String into Integer, we can use Integer.valueOf() method which returns instance of Integer class.


2 Answers

Because select is a String variable, it cannot be used as an index in an array.

select = items[select];

I believe you meant to use the index value i in your for loop (where i is 0 to items.length). Something like

select = items[i];

However, based on your comments below, I believe you really wanted

int select = scan.nextInt();
System.out.println("You selected: " + items[select]);

Based on your edit, you could do it with two arrays and two loops. Something like,

String items[] = { "Table", "Desk", "Chair", "Couch", "Lazyboy" };
int price[] = { 250, 175, 125, 345, 850 };
Scanner scan = new Scanner(System.in);
int pos = -1;
outer: while (pos == -1) {
    System.out.println("Please enter an item to view it's price: ");
    String select = scan.nextLine();
    for (int i = 0; i < items.length; i++) {
        if (items[i].equalsIgnoreCase(select.trim())) {
            pos = i;
            break outer;
        }
    }
}
if (pos != -1) {
    System.out.printf("The price is %d%n", price[pos]);
}

But a Map would (in my opinion) be a better solution (it's certainly more efficient). Like,

String[] items = { "Table", "Desk", "Chair", "Couch", "Lazyboy" };
int[] price = { 250, 175, 125, 345, 850 };
Map<String, Integer> map = new HashMap<>();
for (int i = 0; i < items.length; i++) {
    map.put(items[i], price[i]);
}
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter an item to view it's price: ");
while (scanner.hasNextLine()) {
    String item = scanner.nextLine().trim();
    if (map.containsKey(item)) {
        System.out.printf("The price is %d%n", map.get(item));
    }
    System.out.println("Please enter an item to view it's price: ");    
}
like image 191
Elliott Frisch Avatar answered Oct 17 '22 10:10

Elliott Frisch


item is an array of string.

The array will be accessed by indexes (the values must be integer, since it is not an associative array like we have in php).

You are trying to access the value of an array using the string as index which is not possible and this is why you are getting conversion error.

change

   select = items[select]; // <--- select is string, must be int here

to

 select = items[Integer.valueOf(select)]; //select is an integer value now

This will convert the string value into integer and pass it to the array, you will be accessing the string array with integer index now.

like image 21
Danyal Sandeelo Avatar answered Oct 17 '22 09:10

Danyal Sandeelo