Note that there are some explanatory texts on larger screens.

plurals
  1. POReading certain sentences from datafile.txt in java
    primarykey
    data
    text
    <p>In my program, it reads a file called datafile.txt... inside the datafile.txt is a random 3 lines of words. What my program does is reads the file the user types in and then they can type in a Line # and Word # and it will tell them the word that is in that location.. for example..</p> <p>What is the file to read from?</p> <p>datafile.txt</p> <p>Please enter the line number and word number (the first line is 1).</p> <p>2 2</p> <p>The word is: the</p> <p>My problem is that my program reads the 3 lines in the txt doc as 0, 1 ,2 and the words start from 0. So to read the first word in the first line they would have to type 0,0 instead of 1,1. What I am trying to do is make it work so they can type 1,1 instead of 0,0. Not sure what my problem is right now, here is my code....</p> <pre><code>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&lt;String&gt; file = new ArrayList&lt;String&gt;(); 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&lt;String&gt; lines = new ArrayList&lt;String&gt;(); 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&lt;String&gt; items, int index) throws checkException { throw new checkException("Not Found"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload