[英]Reading text from .text file and storing in array
所以基本上我只知道如何讀取整數並將它們存儲在數組中,但是整數和單詞呢? 這是我的代碼,用於從較早的時間開始僅對數組中的整數進行處理,方法為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]);
}
}
}
我不討論您的代碼,但這是一個如何同時讀取數字和單詞的示例:
//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語句(“ else”語句取決於文本文件中包含數字和字母的大小和填充程度),但是當數字后跟隨String時,請使用else語句,類似於else語句。我提到的代碼; 當有字符串時,只需使用textfileName.hasNext()讀取該字符串,而無需任何條件。 有任何疑問,請不要猶豫!
這是一個小代碼段,可以幫助您,我已經根據您的代碼進行了開發。
我對數組的處理略有不同,但是我只是想指出正確的方向,而不是為您完成工作。
這將創建兩個數組: 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();
}
}
}
希望這可以幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.