8

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];
        }       
    }
}
1
  • 1
    The line select = items[select] isn't want you want, since arrays require an integer index. Commented Jun 29, 2015 at 4:56

6 Answers 6

10

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: ");    
}
Sign up to request clarification or add additional context in comments.

8 Comments

Oh ok that makes sense. Could you explain to me why it's stating you cannot convert from String to int if they're both strings though? Also, the point of this is to have the user input a string of one of the furniture pieces and the output would be the price. So in that case would I want a For each loop instead you think?
@user3862586 Because a String[] is not a String, and you cannot access an array based on a String index. Based on your comment, I htink you really wanted to read an int from the user anyway.
Oh I think I understand but, no I want to have the user enter a String from the array of furniture and it'll return the price of that furniture.
Then you'll need a Map. An array doesn't work that way.
You can do it with two arrays, but I think it would be easier with a Map as stated earlier. I'd suggest looking at a HashMap. You can map each piece of furniture to its price directly, instead of having to worry about indexes.
|
3

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.

Comments

3
select = items[select];

When you retrieving item from the array using index you have to give the int value. so its better you change your code as below.

int index = Integer.parseInt(select);
select = items[index];

If you have time follow below link to know further about the Arrays. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Comments

0

Because items[] is a list, not a Map. It needs to be indexed by a number.

for(int i=0; i < items.length; i++) {
    select = items[i];

Comments

0
String items [] = {"Table", "Desk", "Chair", "Couch", "Lazyboy"};
int price [] = {250, 175, 125, 345, 850};

String select = null;
int selectOption;
Scanner scan = new Scanner(System.in);

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

for(int i=0; i < items.length; i++) {
    select = items[selectOption];
}       
System.out.println(select);

The error is occurred because, the "String select" is used to store user input which is a number, and later during the loop, the furniture name is stored.So, on the next iteration, the items[] of furniture name is called.Which will throw an error.

Comments

0

Here's a short example that demonstrates how to do what you want. I also fixed the grammar for you: it's spelled its.

Map<String, Integer> priceMap = new HashMap<String, Integer>() 
{{ 
    put("Table",   250); 
    put("Desk",    175); 
    put("Chair",   125); 
    put("Couch",   345); 
    put("Lazyboy", 850); 
}};

Scanner scanner = new Scanner(System.in);
System.out.println("Please enter an item to view its price: ");
String item = scanner.nextLine().trim();

if (priceMap.containsKey(item)) 
{
    System.out.printf("The price is %d", priceMap.get(item));
}

Comments

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.