简体   繁体   中英

JAVA, comparing two strings

[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;
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM