Reputation: 113
Hello Please i need some help in Java. Since i have been trying to output lines from a file using Scanner but i dont succeed. This is how the file content looks like :
4
5
3->1->25
1->0->12
2->0->-5
0->1->0
2->1->7
The first thing i tried to do was to output information starting from the third line and it worked perfectly well. This is the code:
public static void main(String[] args) throws Exception {
Scanner scanner = null;
BufferedReader in = null;
String line = null;
try{
scanner = new Scanner(new BufferedReader(new FileReader("graphe.txt")));
//in = new BufferedReader(new FileReader("graphe.txt"));
scanner.useDelimiter("->");
while (scanner.hasNextLine()){
int value = scanner.nextInt();
scanner.skip(scanner.delimiter());
int value2 = scanner.nextInt();
scanner.skip(scanner.delimiter());
String value3 = scanner.nextLine();
int value3Int = Integer.parseInt(value3);
System.out.println(value + " - > " + value2 + " cost " + value3Int);
}
}catch (IOException e){
e.printStackTrace();
}finally {
if (scanner != null){
scanner.close();
}
}
}
}
But then when i inserted values 4 and (first and second line) i tried to figure how to deal with it. The first thing i did was to try to use the if condition to see if the delimeter exist and if it doesn't i print it out :
public static void main(String[] args) throws Exception {
Scanner scanner = null;
BufferedReader in = null;
String line = null;
try{
scanner = new Scanner(new BufferedReader(new FileReader("graphe.txt")));
//in = new BufferedReader(new FileReader("graphe.txt"));
scanner.useDelimiter("->");
while (scanner.hasNextLine()){
String find = scanner.nextLine();
if (!(find.contains("->"))){
System.out.println(find);
}
else {
int value = scanner.nextInt();
scanner.skip(scanner.delimiter());
int value2 = scanner.nextInt();
scanner.skip(scanner.delimiter());
String value3 = scanner.nextLine();
int value3Int = Integer.parseInt(value3);
System.out.println(value + " - > " + value2 + " cost " + value3Int);
}
}
}catch (IOException e){
e.printStackTrace();
}finally {
if (scanner != null){
scanner.close();
}
}
}
}
But it didn't work like expeceted and i had this error as output
4
5
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
1 - > 0 cost 12
at java.base/java.util.Scanner.next(Scanner.java:1594)
0 - > 1 cost 0
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at com.tpTG.LectureGrapheMatrice.main(LectureGrapheMatrice.java:25)
Process finished with exit code 1
Please i know i can deal with it but i've been thinking over and over but i can't figure how to. Please help thanks.
Upvotes: 2
Views: 1197
Reputation: 271515
I think a regex replaceAll
for each line might do it.
// no need to set delimiter
while (scanner.hasNextLine()) {
String line = scanner.nextLine().replaceAll("^((?:\\+|-)?\\d+)->((?:\\+|-)?\\d+)->((?:\\+|-)?\\d+)$",
"$1 -> $2 costs $3");
System.out.println(line);
}
If the input matches the pattern, you format it to "x -> y costs z", otherwise replaceAll
won't do anything and you output the same line.
If you want the three values, you can access the captured values with Matcher.group
,
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
Matcher m = Pattern.compile("((?:\\+|-)?\\d+)->((?:\\+|-)?\\d+)->((?:\\+|-)?\\d+)").matcher(line);
if (m.matches()) {
// m.group(1), m.group(2) and m.group(3) are the three values as strings. You can convert them to ints yourself.
System.out.println(m.group(1) + " -> " + m.group(2) + " costs " + m.group(3));
} else {
System.out.println(line);
}
}
Upvotes: 1