0

I have a program that does several different things, one of them being this method to find an item in the file and display the attributes of that file (there are 3) in the row. For some reason, this is only printing out the first three strings in the file, instead of finding and printing the item I search for.

public static void findItem() throws FileNotFoundException {
 Scanner kb = new Scanner(System.in);

    FileReader reader = new FileReader("read_record.txt");
    Scanner fin = new Scanner(reader);
    System.out.println("Enter the sku of the dvd you wish to find.");
    String dvdSku = kb.next();


    while (fin.hasNext()){
        String nextString = fin.next();
        if (nextString == dvdSku); {
            String skuToFind = nextString;
            String titleToFind = fin.next();
            String lengthToFind = fin.next();
            System.out.printf("%-10s %-15s %10s %n",skuToFind,titleToFind,lengthToFind);
            break;
        }
    }
} 
Zong
  • 6,160
  • 5
  • 32
  • 46
Corey
  • 264
  • 2
  • 10

1 Answers1

2

Use equals or equalsIgnoreCase for comparing strings.

if (nextString.equals(dvdSku)){

Also remove the break statement that you have unless you want to stop on first match.

Sajal Dutta
  • 18,272
  • 11
  • 52
  • 74