I am trying to find a specific string in a txt file.
here is what i have using java lists:
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class constantb {
static String filePath = "C:myiflepath";
public static Double findDouble(String name) throws IOException{
String value;
Scanner constScanner = new Scanner(filePath);
List<String> list=new ArrayList<>();
while(constScanner.hasNextLine()){
list.add(constScanner.nextLine());
}
value = list.get(list.indexOf(name)+1);
System.out.println(value);
constScanner.close();
return Double.parseDouble(value);
}
}
When I run this I get a number format exception problem.
In the txt file it searches for a name and right below that name in the file is what I want to return as a double.
here is the txt file:
help please:
double hi
9
I have tried removing the list part of the file:
import java.util.Scanner;
public class constantc{
static String filePath = "C:myfilepath";
public static Double findDouble(String name){
Scanner scanner = new Scanner(filePath);
String value = scanner.nextLine();
while(scanner.hasNextLine()){
value = scanner.nextLine();
if(name == scanner.nextLine()){
scanner.nextLine();
value = scanner.nextLine();
break;
}else{
scanner.nextLine();
}
}
scanner.close();
return Double.parseDouble(value);
}
}
That did not work because I got the same error.