-1

Here is my customer.csv file:

1, Ali,1203456789, Normal
2, Siti,134567890, Normal
3, Bob,1568980765, Normal

I want to change the Normal status of the name I enter to Cased but my code seems got something wrong.And here is my code:

public static void main(String[] args) throws IOException{
    Scanner input = new Scanner(System.in);
    System.out.println("Please enter the customer you want to flag as Cased:");
    String flagCus = input.nextLine();
    ArrayList<String> customersFlagged = new ArrayList<String>();
    List<String> lines = Files.readAllLines(Paths.get("customer.csv"));
    for (int i = 0; i < lines.size(); i++) {
        String[] items = lines.get(i).split(",");
        if (items[1] == flagCus){
            String enterList = items[0] + "," + items[1] + "," + items[2] + "," + "Cased";
            customersFlagged.add(enterList);
        } else{
            String enterList = items[0] + "," + items[1] + "," + items[2] + "," + items[3];
            customersFlagged.add(enterList);
        }
    }

I think the problem is the line if (items[1] == flagCus) ones but I am not sure where got wrong , I have been try to add a " " before my flagCus when doing the if statement but it still goes wrong. Can somebody help me check this code? Thank you for your attention.

Edit:I should have change the code (items[1] == flagCus) to (items[1].equals(" " + flagCus).Thank you guys for help.

  • possible duplicate of [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – geocodezip Jan 09 '21 at 04:06
  • It's kind of funny, but nobody here has suggested to use any CSV parser. Technically, they suggest you to check string equality using the `equals` method, but it is making easy earnments for a question that falls into one of the most classic Java questions ever. My friendly suggestion: please use appropriate tools to solve your problems. (neither `ArrayList` full of the records, nor `Files.readAllLines` that may be a performance killer, nor index-looped iteration (for an ArrayList though) over `.iterator()` or `for`, nor even `.split(",")` that breaks on `,`-containing values). – terrorrussia-keeps-killing Jan 09 '21 at 10:40

3 Answers3

2

When comparing two objects as opposed to primitive types, use .equals() not ==. So:

items[1].equals(flagCus);
Adrian Russo
  • 546
  • 4
  • 16
2

To check equal String, use "string".equals("other") instead.

0

The Strings in the file have a space at the beginning (you are splitting on the commas).

1, Ali,1203456789, Normal

Either remove those from the input data or call:

if (items[1].trim().equals(flagCus)){

(as others have indicated in their answers, use .equals to compare String objects.

complete code:

public static void main(String[] args) throws IOException{
        Scanner input = new Scanner(System.in);
        System.out.println("Please enter the customer you want to flag as Cased:");
        String flagCus = input.nextLine();
        ArrayList<String> customersFlagged = new ArrayList<String>();
        List<String> lines = Files.readAllLines(Paths.get("customer.csv"));
        for (int i = 0; i < lines.size(); i++) {
            String[] items = lines.get(i).split(",");
            if (items[1].trim().equals(flagCus)){
                String enterList = items[0] + "," + items[1] + "," + items[2] + "," + "Cased";
                customersFlagged.add(enterList);
            } else{
                String enterList = items[0] + "," + items[1] + "," + items[2] + "," + items[3];
                customersFlagged.add(enterList);
            }
        }
geocodezip
  • 158,664
  • 13
  • 220
  • 245