私のプログラムでは、datafile.txt というファイルを読み取ります... datafile.txt の中には、ランダムな 3 行の単語があります。私のプログラムは、ユーザーが入力したファイルを読み取り、行番号と単語番号を入力して、その場所にある単語を教えてくれます..たとえば..
読み取るファイルは何ですか?
データファイル.txt
行番号と単語番号(1行目は1)を入力してください。
2 2
言葉は:
私の問題は、プログラムが txt doc の 3 行を 0, 1 ,2 として読み取り、単語が 0 から始まることです。したがって、最初の行の最初の単語を読み取るには、1,1 ではなく 0,0 と入力する必要があります。 . 私がやろうとしているのは、0,0 の代わりに 1,1 を入力できるようにすることです。私の問題が今何なのかわからない、ここに私のコードがあります....
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class readingFile {
/**
* @param args
* @throws IOException
* @throws validException
*/
public static void main(String[] args) throws IOException, checkException
{
System.out.println("Enter file name: " );
Scanner keyboard = new Scanner(System.in);
BufferedReader inputStream = null;
ArrayList<String> file = new ArrayList<String>();
String fileName = keyboard.next();
System.out.println ("The file " + fileName +
" has the following lines below: ");
System.out.println();
try
{
inputStream = new BufferedReader(new FileReader(fileName));
ArrayList<String> lines = new ArrayList<String>();
while(true)
{
String line = inputStream.readLine();
if(line ==null)
{
break;
}
Scanner itemnize = new Scanner(line);
while(itemnize.hasNext())
{
lines.add(itemnize.next());
}
lines.addAll(lines);
System.out.println(lines+"\n");
}
System.out.println("Please enter the line number and word number");
int index1 = keyboard.nextInt();
int index = keyboard.nextInt();
System.out.println("The word is: "+ lines.get(index));
}
catch(FileNotFoundException e)
{
System.out.println("Error opening the file " + fileName);
}
inputStream.close();
}
private static void checkValid(ArrayList<String> items, int index) throws checkException
{
throw new checkException("Not Found");
}
}