-6

Using the codes below, this lets me input codes, name and description. However, after entering purchase. The problem is that It can't display the details I want to show using the right code.

import java.util.Scanner;
public class array {
    public static void main(String[] args) {
        Scanner a = new Scanner(System.in);
        int x;
        String q;
        String ans;
        String itemCounts = "";
        String name="",descriptions = "";

        do {
            System.out.print("Item code:");
            itemCounts += "" + a.next() + "\n";

            System.out.print("Item name:");
            name += "" + a.next() + "\n";

            System.out.print("des:");
            descriptions += "" + a.next() + "\n";

            System.out.println("Do you want to add more? yes or no:");
            ans = a.next();
        } while (ans.equals("yes"));


        System.out.print("enter 1 to purchase :");
        x = a.nextInt();

        if(x==1){

            System.out.print("enter code:");
            q = a.next();

            if(q==itemCounts){

                String[] b = itemCounts.split("\n");
                String[] nm = name.split("\n");
                String[] des = descriptions.split("\n");

                for (int i = 0; i < b.length; i++) {

                    System.out.println("Item name:" + nm[i]);
                    System.out.println("Item description:" + des[i]);

                }
            }
        }

    }
}
Shikiryu
  • 10,180
  • 8
  • 49
  • 75
Raymond
  • 25
  • 6

1 Answers1

2

q==itemCounts
should be
q.equals(itemCounts)

Because q and itemCounts both are String. == tests for reference equality and .equals() tests for value equality.

Also itemCounts += "" + a.next() + "\n"; should be itemCounts += a.next();

This will help : How do I compare strings in Java?

Community
  • 1
  • 1
Ajinkya
  • 22,324
  • 33
  • 110
  • 161