从 Java 中的文本文件中的字符串中查找数字
Find numbers from a string, from a text file in Java
import javax.swing.JOptionPane;
import java.util.Scanner;
import java.io.FileReader;
import java.util.Arrays;
public class Part2{
public static void main(String[] args){
String[] holdPosition = new String[30];
String FileToBeSearched = JOptionPane.showInputDialog("Enter the file name to be searched: ");
String WordToBeSearched = JOptionPane.showInputDialog("Enter the word to be searched: ");
try{
FileReader searchedFile = new FileReader(FileToBeSearched);
Scanner scannedFile = new Scanner(searchedFile);
int i = 0;
String NumOfLines = "";
while(scannedFile.hasNext()){
String currentWord = scannedFile.next();
holdPosition[i] = currentWord;
if(currentWord.equalsIgnoreCase(WordToBeSearched)){
NumOfLines += holdPosition[i-1]+" "+currentWord+" "+scannedFile.next()+"\n";
Arrays.fill(holdPosition, null);
}
i++;
}
scannedFile.close();
JOptionPane.showMessageDialog(null,NumOfLines);
}catch(Exception except){}
System.exit(0);
}
}
这是我当前的代码。我不知道如何测试给定的字符串是否代表数字,我需要一个 try 块,在该块中我将文件中的每个字符串传递给 Double.parseDouble()。我想 return 文本文件中的每个数字,以及它前后的字符串。
我不确定我是否理解你的问题,但你是否只需要一个小的帮助方法,例如:
public static boolean isDouble(String word) {
boolean isDouble = false;
try {
Double.parseDouble(word);
isDouble = true;
} catch (NumberFormatException nfe) {
// empty on purpose, as most of the time, input will not be a number!
}
return isDouble;
}
现在您可以简单地使用该方法来检查从扫描仪收到的每个输入字符串。如果方法 returns 正确,你知道,你得到了一个数字。
如果您的代码也应该知道那个数字,您可以稍微修改一下:
public static Double isDouble(String word) {
Double isDouble = null;
... 使用 null 向呼叫者表明您的输入不是数字。
但是返回null真的不是神作。
提示:我的代码只是为了让您继续使用而写下来的;当心错别字或其他微妙的错误。它旨在让您自己解决问题。
import javax.swing.JOptionPane;
import java.util.Scanner;
import java.io.FileReader;
import java.util.Arrays;
public class Part2{
public static void main(String[] args){
String[] holdPosition = new String[30];
String FileToBeSearched = JOptionPane.showInputDialog("Enter the file name to be searched: ");
String WordToBeSearched = JOptionPane.showInputDialog("Enter the word to be searched: ");
try{
FileReader searchedFile = new FileReader(FileToBeSearched);
Scanner scannedFile = new Scanner(searchedFile);
int i = 0;
String NumOfLines = "";
while(scannedFile.hasNext()){
String currentWord = scannedFile.next();
holdPosition[i] = currentWord;
if(currentWord.equalsIgnoreCase(WordToBeSearched)){
NumOfLines += holdPosition[i-1]+" "+currentWord+" "+scannedFile.next()+"\n";
Arrays.fill(holdPosition, null);
}
i++;
}
scannedFile.close();
JOptionPane.showMessageDialog(null,NumOfLines);
}catch(Exception except){}
System.exit(0);
}
}
这是我当前的代码。我不知道如何测试给定的字符串是否代表数字,我需要一个 try 块,在该块中我将文件中的每个字符串传递给 Double.parseDouble()。我想 return 文本文件中的每个数字,以及它前后的字符串。
我不确定我是否理解你的问题,但你是否只需要一个小的帮助方法,例如:
public static boolean isDouble(String word) {
boolean isDouble = false;
try {
Double.parseDouble(word);
isDouble = true;
} catch (NumberFormatException nfe) {
// empty on purpose, as most of the time, input will not be a number!
}
return isDouble;
}
现在您可以简单地使用该方法来检查从扫描仪收到的每个输入字符串。如果方法 returns 正确,你知道,你得到了一个数字。
如果您的代码也应该知道那个数字,您可以稍微修改一下:
public static Double isDouble(String word) {
Double isDouble = null;
... 使用 null 向呼叫者表明您的输入不是数字。 但是返回null真的不是神作。
提示:我的代码只是为了让您继续使用而写下来的;当心错别字或其他微妙的错误。它旨在让您自己解决问题。