[edited} ok it, let me reformulate. numvol 45 , contents of 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 still can't fix problem, return 543. im trying return line when equals numvol check first number of line.
i'm having trouble comparing 2 strings. let's have .csv file following content: 54;a;b;c;de , numvol value 54. method should returning 54 reason doesnt enter in "if" , 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 string numvol value in while (scanner.hasnext()){ line = scanner.next(); if(line.equals(snumvol)){ scanner.close(); return line; } } scanner.close(); return line; }
the problem you've told scanner use ; delimiter, it's not using whitespace delimiter anymore. token being tested against "45" isn't "45", it's "456\n45" (the end of previous line, newline, , beginning of next line), isn't match.
change usedelimiter line use both semicolons , whitespace delimiters:
scanner.usedelimiter("[;\\s]"); ...and scanner sees "456" , "45" separately, , matches "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 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 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 find answer problem walk through code debugger , watch value of line, or (if reason don't have debugger?!), insert system.out.println("line = " + line); statement loop see being compared. instance, if insert system.out.println("line = " + line); above line = scanner.next(); line above , use ";" 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(";"); // <== 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 problem.
Comments
Post a Comment