从.text文件中读取文本并存储在数组中

时间:2015-02-21 06:12:24

标签: java arrays file

所以基本上我只知道如何读取整数并将它们存储在一个数组中,但是整数和单词呢?这是我的代码,用于从早期的数组中完成整数,方法是readFileAndReturnWords。我怎么能改变它来读取整数和单词呢?

import java.util.Scanner;
import java.util.InputMismatchException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;

  public class WordsNsuch { 

     public static void main(String [ ] commandlineArguments){
        //Error message when arg is blank
        if(commandlineArguments.length == 0) {
        System.err.println("You did not enter anything");
        System.exit(0);
        }


         String[] array = readFileAndReturnWords(commandlineArguments[0]);
         sortAndPrintTheArray(array, commandlineArguments[0]);               
      }
      //RIGHT HERE GUYS
    public static String readFileAndReturnWords(String filename){
      String[] temp = new String[10000];
      int i = 0;    
      //connects file
      File file = new File(filename);
      Scanner inputFile = null;

     try{

          inputFile = new Scanner(file);

         }
          //When arg is mistyped
      catch(FileNotFoundException Exception1) {
          System.out.println("File not found!");
          System.exit(0);      
     }


     //counts the amount of strings
    if (inputFile != null) {

    try {

      while (inputFile.hasNext()) {
        try {
          temp[i] = inputFile.nextInt();//This is a problem
          i++;
        } catch (InputMismatchException Exception2) { 
          inputFile.next();
        }
      }
    } 
     finally {
      inputFile.close();
    }
    String[] array = new String[i];
    System.arraycopy(temp, 0, array, 0, i);
    return array;
  }
  return new String[] {};
 }

  public static void sortAndPrintTheArray(String [] array, String filename){
       Sorting.display = false;
       Sorting.insertionSort(array, 0, array.length-1);//figure out how to get the last word later

      System.out.println("ASCII listing of words in file:\"" + filename + "\" = " + array.length);   
      for (int i = 0; i < array.length; i++) {
      System.out.println("index = " + i + "," + " element = " + array[i]);

      }   
   }    
 }      

2 个答案:

答案 0 :(得分:1)

我没有进入你的代码,但这里有一个如何读取数字和单词的例子:

//while there's anything including numbers and words
    while(textfile.hasNext())
    {
       //if there's a number, read it!
       if(textfile.hasNextInt())
           {
               int number = textfile.nextInt(); //it can be double, float..
           }
       else 
            textfile.next()
       String word1 = textfile.hasNext();
       String word2 = textfile.hastNext();

       f(textfile.hasNextInt())
           {
               int number2 = textfile.nextInt(); //it can be double, float..
           }
       else 
            textfile.next()
       .......
    }

任何时候如果有一个号码,请尝试if-else语句(&#34;否则&#34;语句取决于你的文本文件的数量和字母有多大和多满)但是当一个数字后面跟着一个字符串使用else语句类似于我提到的代码中的else语句;当有一个字符串时,只需使用textfileName.hasNext()读取任何条件。有任何疑问,请不要犹豫!

答案 1 :(得分:1)

这是一个可以帮助您的小代码片段,我已将其基于您的代码。

我会略微区别地处理数组,但我只是想指出你正确的方向,而不是为你工作。

这将创建两个数组: ints strs 一个染整整数和一个包含字符串。

int[] ints = new int[10000];
String[] strs = new String[10000];
int i = 0;
int j = 0;

File file = new File(filename);
Scanner inputFile = null;

try {
  inputFile = new Scanner(file);
} catch(FileNotFoundException Exception1) {
  System.out.println("File not found!");
  System.exit(0);      
}

try {
  while (inputFile.hasNext()) {
    if (inputFile.hasNextInt()) {
      ints[i++] = inputFile.nextInt();
    } else {
      strs[j++] = inputFile.next();
    }
  }
}

希望这有帮助!