-2

I'm trying to write a Java application that reads a text file. Suppose I have a text file beg.txt which contains text:

I am a beginner

When the user enters word number 4, the program has to print word 'beginner'.

How can I do this in Java, please?

janhink
  • 4,744
  • 2
  • 27
  • 37
RaviRokz
  • 1
  • 1
  • http://stackoverflow.com/questions/12287254/displaying-specific-words-in-a-txt-file-using-a-scanner – NoobEditor Mar 04 '14 at 09:34
  • and https://www.google.de/search?q=cat+in+perl&ie=utf-8&oe=utf-8&rls=org.mozilla:en-US:official&client=firefox-a&channel=sb&gfe_rd=ctrl&ei=4ZwVU8bqGsTd8QOlxICgCQ&gws_rd=cr#channel=sb&q=print+word+from+a+text+file+using+java&rls=org.mozilla:en-US:official&safe=off – NoobEditor Mar 04 '14 at 09:35
  • It would probably be helpful if you explained what you've done so far, so people can clarify any specific points that you're not sure about and focus on those rather than repeating the things you already understand. [Matt Gemmell's article](http://mattgemmell.com/what-have-you-tried/) may provide some further insight into the benefits of thinking about what you've tried, and what you do and don't already know. – Edd Mar 04 '14 at 09:58

4 Answers4

0

First give a try before asking this.

Just for your help. Try following steps, this is not the only way.

  1. Read your file
  2. Split string to a string array using space
  3. Print array[your choice - 1]
NoobEditor
  • 14,239
  • 13
  • 66
  • 102
0
   BufferedReader br = null;
   String[] str;

    try {
           String sCurrentLine;
           StringBuilder sb = new StringBuilder();

           br = new BufferedReader(new FileReader("C:\\testing.txt"));

           while ((sCurrentLine = br.readLine()) != null) {
                sb.append(sCurrentLine);
           }
           str = sb.toString.split(" ");

    } catch (IOException e) {
        e.printStackTrace();
    }

if user enters 4 then you can use array 'str' like this :

  String result = str[userEnteredValue - 1];

Note: the above code will work only when the file will contain space delimitted characters.

NoobEditor
  • 14,239
  • 13
  • 66
  • 102
Android Killer
  • 17,140
  • 11
  • 61
  • 86
0

Well, the basic process will be something like the following:

  1. Load the text file
  2. Get user input
  3. Process text file with parameters from user

Step 1 will depend on which version of Java you're using. If Java 7, I'd look at nio2. Java 6 has other options. Or you could you Guava or Apache Commons. Since the processing required is minimal, I would store the output of this step as a simple String.

Getting the user input can be done in a number of ways, but one option is to use a Scanner.

Finally, processing the file can be done by using String.split() with a simple regex and then picking the correct element from the resulting array.

Community
  • 1
  • 1
Edd
  • 3,510
  • 3
  • 23
  • 33
0
File read=new File("D:\\Test.txt");
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(read),Charset.forName("UTF-8")));
        String news = reader.readLine();
        String[] records = news.split(" ");

if your input is 4 and get records[4]

Maheshbabu Jammula
  • 327
  • 1
  • 2
  • 11