vendredi 8 janvier 2021

If statement not working despite conditions being met. JAVA [closed]

So I am taking some information from a CSV file, it has information for Games and CDs, the information for it is a Game or CD is stored as 'term7'. In the end I want it to print the information of the Game or CD differently depending on which one it is. However no matter what I do the if statement is never met so only the else is ran. Even if I print the value of term7 before the if statement and is shown as "CD", the the if, will still not run. Here is my code, thanks.

package com.company;

import java.io.File;
import java.util.Scanner;

class discSearch {
    private static Scanner keyboard;

    public static void main() {

        System.out.println(" ");
        System.out.println("Enter Name of Record:");

        String filepath = "theFile.csv";
        Scanner ST = new Scanner(System.in);
        String searchTerm = ST.nextLine();

        readRecord(searchTerm, filepath);
    }

    public static void readRecord(String searchTerm, String filePath){
        boolean found = false;
        String term1 = ""; String term2 = ""; String term3 = ""; String term4 = ""; String term5 = ""; String term6 = ""; String term7 = null;

        try{
            keyboard = new Scanner(new File(filePath));
            keyboard.useDelimiter("[,\n]");

            while (keyboard.hasNext() && !found){
                term1 = keyboard.next();
                term2 = keyboard.next();
                term3 = keyboard.next();
                term4 = keyboard.next();
                term5 = keyboard.next();
                term6 = keyboard.next();
                term7 = keyboard.next();


                if(term1.equalsIgnoreCase(searchTerm)){
                    found = true;
                }
            }


            if(found){
                if ("CD".equals(term7)){
                    System.out.println("Title: " + term1 + ",  Artist: " + term2 + ",  Genre: " + term3 + ",  Release Year: " + term4 + ",  Number of Songs: " + term5 + ",  Duration: " + term6 + " Minutes");
                }
                else {
                    System.out.println("Title: " + term1 + ",  Genre: "  + term2 + ",  Release Date: " + term3 + ",  Rating: " + term4 + ",  Platform: " + term5);
                }
            }
            else{
                System.out.println("Not Found");
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire