0

[EDITED} Ok I get it, let me reformulate. numVol is 45 and the contents of the file is

54;a;23;c;de;56
23;d;24;c;h;456
45;87;c;y;535
432;42;h;h;543

but I still can't fix my problem, with this it return 543. so what im trying to do is return line when its equals numVol but check only the first number of a line.


I'm having trouble comparing two strings. Let's say I have a .csv file with the following content: 54;a;b;c;de and that numVol value is 54. the method should be returning 54 but for some reason it doesnt enter in the "if" and it return "de".

public static String test(int numVol)throws Exception{
    File file = new File("test.csv");
    Scanner scanner = new Scanner(file);
    scanner.useDelimiter(";");
    String line = "";
    String sNumVol = ""+numVol; //create a string with numVol value in it
    while (scanner.hasNext()){
        line = scanner.next();
        if(line.equals(sNumVol)){
            scanner.close();
            return line;
        }
    }
    scanner.close();
    return line;
}
6
  • 3
    "ligne" is a typo, right? You mean to assign it to the "line" that you initialized before the loop? Commented May 5, 2013 at 21:50
  • 1
    yea my bad it's a type. ligne is actually line in french, I wanted to replace all the "ligne" to line so you cant can understand but i missed 1. Commented May 5, 2013 at 21:53
  • 2
    Works fine for me. Looks like it is the file. Commented May 5, 2013 at 21:58
  • 2
    Try putting System.out.println(line) in the loop after the assignment to see what it prints out. Commented May 5, 2013 at 21:59
  • 1
    Check your file, this seems to work for me as well. I confirmed it by putting a println call in the if block. Commented May 5, 2013 at 22:06

1 Answer 1

2

The problem is that now that you've told Scanner to use ; as a delimiter, it's not using whitespace as a delimiter anymore. So the token being tested against "45" isn't "45", it's "456\n45" (the end of the previous line, the newline, and the beginning of the next line), which isn't a match.

Change your useDelimiter line to use both semicolons and whitespace as your delimiters:

scanner.useDelimiter("[;\\s]");

...and then the scanner sees the "456" and the "45" separately, and matches the "45".

This code:

import java.util.*;
import java.io.*;

public class Parse {
    public static final void main(String[] args) {
        try {
            String result = test(45);
            System.out.println("result = " + result);
        }
        catch (Exception e) {
            System.out.println("Exception");
        }
    }

    public static String test(int numVol)throws Exception{
        File file = new File("test.csv");
        Scanner scanner = new Scanner(file);
        scanner.useDelimiter("[;\\s]"); // <==== Change is here
        String line = "";
        String sNumVol = ""+numVol;
        while (scanner.hasNext()){
            line = scanner.next();
            if(line.equals(sNumVol)){
                scanner.close();
                return line;
            }
        }
        scanner.close();
        return line;
    }
}

With this test.csv:

54;a;23;c;de;56
23;d;24;c;h;456
45;87;c;y;535
432;42;h;h;543

Shows this:

$ java Parse
result = 45

The way to find the answer to this problem was simply to walk through the code with a debugger and watch the value of line, or (if for some reason you don't have a debugger?!), insert a System.out.println("line = " + line); statement into the loop to see what was being compared. For instance, if you insert a System.out.println("line = " + line); above the line = scanner.next(); line above and you just use ";" as the delimiter:

import java.util.*;
import java.io.*;

public class Parse {
    public static final void main(String[] args) {
        try {
            String result = test(45);
            System.out.println("result = " + result);
        }
        catch (Exception e) {
            System.out.println("Exception");
        }
    }

    public static String test(int numVol)throws Exception{
        File file = new File("test.csv");
        Scanner scanner = new Scanner(file);
        scanner.useDelimiter(";"); // <== Just using ";"
        String line = "";
        String sNumVol = ""+numVol;
        while (scanner.hasNext()){
            line = scanner.next();
            System.out.println("line = [[" + line + "]]");
            if(line.equals(sNumVol)){
                scanner.close();
                return line;
            }
        }
        scanner.close();
        return line;
    }
}

You see this:

$ java Parse
line = [[54]]
line = [[a]]
line = [[23]]
line = [[c]]
line = [[de]]
line = [[56
23]]
line = [[d]]
line = [[24]]
line = [[c]]
line = [[h]]
line = [[456
45]]
line = [[87]]
line = [[c]]
line = [[y]]
line = [[535
432]]
line = [[42]]
line = [[h]]
line = [[h]]
line = [[543
]]
result = 543

...which helps visualize the problem.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much, I actually found the answer after Varun Madiath and Marco told me that the code worked but I still didn't know what to add to the delimiter to make it work, im really new coding. all i need now is to make it only check the first number of a line. thanks
@BobJ: You're welcome! We were all new to it once, and (for what it's worth) this was non-obvious, the human brain naturally breaks up those CSV lines and sees the 45 on its own, even though the code (naturally) doesn't. The key here really is walking through it with a debugger and seeing what the code is seeing. It's amazing how often doing that makes something obvious. Netbeans, Eclipse, IntelliJ IDEA, or even just JSwat, they all let us walk through code step-by-step...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.