1

Not understanding why the memory location is being printed when I run. When I satisfy the 'if' statement I have in my code, the message I get back is "Your total is [I@5f184fc6". I would appreciate if someone could help get this issue resolved as well as explaining to me to help me understand and not run into this issue again.

import java.util.Arrays;
import java.util.Scanner;
public class steakShop {


 public static void main(String [] args){

   Scanner scanner = new Scanner(System.in);
    String items[] = {"burger", "pizza", "shrimp", "nachos","stir fry"};
         enter code here`int selection[] = {5,10,8,7,9};

            for (String it: items) {

               System.out.println("Would you like to order a " + it + "?");
               String itemSelected = scanner.nextLine();
                  if (itemSelected.equals("yes"))
                   System.out.println("Your total is " + selection + ".");

}

} }

Thanks for all comments and answers!

Mo Mack
  • 13
  • 3
  • You should use `Arrays.toString(selection)` when printing contents of an Array in Java. – Fullstack Guy Aug 07 '19 at 16:46
  • thanks for your response. Perhaps I should have went more in depth, whenever I do add Arrays.toString(selection). The result is instead of printing the number amount associated with the one choice, java prints the Entire selection array. It prints "Your total is [5, 10, 8, 7, 9]". I would like it to print "Your total is 5" for the first question, and the associated number so on. – Mo Mack Aug 07 '19 at 16:52
  • @MoMack `selection` is an array, and you're printing it, so why are you confused that the entire array is being printed? – Andreas Aug 07 '19 at 17:10

2 Answers2

2
for (int i = 0; i < items.length; i++ ) {
    System.out.println("Would you like to order a " + items[i] + "?");
    String itemSelected = scanner.nextLine();
    if (itemSelected.equals("yes"))
        System.out.println("Your total is " + selection[i] + ".");
}
ifly6
  • 5,003
  • 2
  • 24
  • 47
Kavitha Karunakaran
  • 1,340
  • 1
  • 17
  • 32
0

You access an array element by referring to an index number. For "Your total is 5" you have to write:

System.out.println("Your total is " + selection[0] + ".");`
Legorooj
  • 2,646
  • 2
  • 15
  • 35
Arne
  • 121
  • 1
  • 3