Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何仅从.txt文件中有选择地读取数字和/或单词?_Java_File_Linked List_Fileinputstream - Fatal编程技术网

Java 如何仅从.txt文件中有选择地读取数字和/或单词?

Java 如何仅从.txt文件中有选择地读取数字和/或单词?,java,file,linked-list,fileinputstream,Java,File,Linked List,Fileinputstream,我有一个简单的.txt文件(“theFile.txt”),格式如下,其中左栏是行号,右栏是单词: 5 today 2 It's 1 " 4 sunny 3 a 6 " 对于这个txt文件,我将使用两种不同的方法来每个获取仅数字和仅字符串,再加上另一种方法来扫描文件,并将每个行号和单词放入一个双链接列表DLL: String fileName = "theFile.txt"; public int getNumberOnly() {

我有一个简单的.txt文件(“theFile.txt”),格式如下,其中左栏是
行号
,右栏是
单词

5     today
2     It's
1     "
4     sunny
3     a
6     "
对于这个txt文件,我将使用两种不同的方法来每个获取数字和字符串,再加上另一种方法来扫描文件,并将每个
行号和
单词放入一个双链接列表
DLL

  String fileName = "theFile.txt";

  public int getNumberOnly() {
       int lineNumber;

       //code to only get the lineNumber but NOT the words
       //This is as far as I got and I need help on this part

       return lineNumber;
  }

  public String getWordsOnly() {
       String words;

       //code to only get the words but NOT the lineNumber
       //This is as far as I got and I need help on this part

       return words;
  }

  public void readAndPrintWholeFile(String fileName){

          String fileContents = new String();
          File file = new File("theFile.txt");
          Scanner scanner = new Scanner(new FileInputStream(fileName));
          DLL<T> list = new DLL<T>();

         //Print each lineNumber and corresponding words for example
         // 5   Today
         // 2   It's

         while (scanner.hasNextLine())
         {
              fileContents = scanner.nextLine();
              System.out.println(list.getNumbersOnly() + " " +  list.getWordOnly());  
              //prints the lineNumber then space then the word
         }
  }

  //I already have all DLL accessors and mutators such as get & set next/previous nodes here, etc.
String fileName=“theFile.txt”;
public int getNumberOnly(){
整数行号;
//代码仅获取行号,而不获取单词
//这是我所能做的,我需要这方面的帮助
返回行号;
}
公共字符串getWordsOnly(){
字符串;
//代码仅获取单词,而不获取行号
//这是我所能做的,我需要这方面的帮助
返回单词;
}
public void readAndPrintWholeFile(字符串文件名){
String fileContents=新字符串();
File File=新文件(“theFile.txt”);
Scanner Scanner=新扫描仪(新文件输入流(文件名));
DLL列表=新的DLL();
//例如,打印每个行号和相应的单词
//5今天
//2是
while(scanner.hasNextLine())
{
fileContents=scanner.nextLine();
System.out.println(list.getNumberOnly()+“”+list.getWordOnly());
//打印行号,然后是空格,然后是单词
}
}
//我已经有了所有的DLL访问器和变异器,比如get&set next/previous节点,等等。
我一直在研究如何为
getNumberOnly()
getWordOnly()

我已经尽力做到这一点了。感谢您的帮助。

公共静态void readAndPrintWholeFile(字符串文件名)引发FileNotFoundException{
public static void readAndPrintWholeFile(String filename) throws FileNotFoundException {
    String fileContents;
    File file = new File(filename);
    Scanner scanner = new Scanner(new FileInputStream(file));
    Map<String, String> map = new HashMap<>();

    while (scanner.hasNextLine()) {
        try {
            fileContents = scanner.nextLine();
            String[] as = fileContents.split(" +");
            map.put(as[0], as[1]);
            System.out.println(as[0] + " " + as[1]);
        } catch (ArrayIndexOutOfBoundsException e) {
            //If some problam with File formate e.g. number without word
        }
    }
}
字符串文件内容; 文件=新文件(文件名); Scanner Scanner=新扫描仪(新文件输入流(文件)); Map Map=newhashmap(); while(scanner.hasNextLine()){ 试一试{ fileContents=scanner.nextLine(); 字符串[]as=fileContents.split(“+”); map.put(as[0],as[1]); System.out.println(作为[0]+“”+as[1]); }捕获(阵列索引边界外异常e){ //如果有文件格式的问题,例如没有单词的数字 } } }
你可以用很多方法做到这一点,其中之一就是倾听。 Hera我没有实现
getNumberOnly()
getWordsOnly()
方法,我没有DLL实现,所以将数据放在
map
HashMap
)中。

公共静态void readAndPrintWholeFile(字符串文件名)抛出FileNotFoundException{ 字符串文件内容; 文件=新文件(文件名); Scanner Scanner=新扫描仪(新文件输入流(文件)); Map Map=newhashmap(); while(scanner.hasNextLine()){ 试一试{ fileContents=scanner.nextLine(); 字符串[]as=fileContents.split(“+”); map.put(as[0],as[1]); System.out.println(作为[0]+“”+as[1]); }捕获(阵列索引边界外异常e){ //如果有文件格式的问题,例如没有单词的数字 } } }
你可以用很多方法做到这一点,其中之一就是倾听。 Hera我没有实现
getNumberOnly()
getWordsOnly()
方法,我没有DLL实现,所以将数据放在
map
HashMap
)中。

您需要将“fileContents”作为参数传递给函数

public int getNumberOnly(String fileContents) {
   int lineNumber;
   //Get position of space
   int spacePos = fileContents.indexOf(" ");
   //Get substring from start till first space is encountered. Also trim off any leading or trailing spaces. Convert string to int via parseInt
   lineNumber = parseInt(fileContents.subString(0,spacePos).trim());
   return lineNumber;
}

public String getWordsOnly(String fileContents) {
   String words;
   int spacePos = fileContents.indexOf(" ");
   //Get substring from first space till the end
   words = fileContents.subString(spacePos).trim();
   return words;
}
您需要将“fileContents”作为参数传递给函数

public int getNumberOnly(String fileContents) {
   int lineNumber;
   //Get position of space
   int spacePos = fileContents.indexOf(" ");
   //Get substring from start till first space is encountered. Also trim off any leading or trailing spaces. Convert string to int via parseInt
   lineNumber = parseInt(fileContents.subString(0,spacePos).trim());
   return lineNumber;
}

public String getWordsOnly(String fileContents) {
   String words;
   int spacePos = fileContents.indexOf(" ");
   //Get substring from first space till the end
   words = fileContents.subString(spacePos).trim();
   return words;
}

把这两种方法结合起来有什么意义?这是家庭作业吗?这是一个练习——我在这里提问,因为我已经用尽了我所知道的一切。数字和单词是否在所有行中都以空格分隔?@RahulSingh据我所知,这只是一个简单的.txt文件,所以唯一的分隔符是数字和单词之间的空格,然后是每行后面的“新行”word@RahulSingh对它们由空格统一分隔。将这两种方法放在一起有什么意义?这是家庭作业吗?这是一个练习——我在这里提问,因为我已经用尽了我所知道的一切。数字和单词是否在所有行中都以空格分隔?@RahulSingh据我所知,这只是一个简单的.txt文件,所以唯一的分隔符是数字和单词之间的空格,然后是每行后面的“新行”word@RahulSingh对它们由空格统一分隔。