How to search for a particular string in a text file using java?

Java: Need help storing String values from text file?

  • so i have these two text files: text file 1: objectA, name1, name2 objectB, name3, name4 text file 2: objectA name1, name2, name3, name4 objectB name5, name6, name7, name8 I am trying to store all the names for objectA from textfile 1 and 2 into an array and all the names for objectB from textfile 1 and 2 into an array so essentially i want like objectA[ ] to have all the names from line 1 of both text files

  • Answer:

    Scanner F1=new Scanner(new File("textfile1")); Scanner F2=new Scanner(new File("textfile2")); String A, B, C, D; A=F1.nextLine(); B=F2.nextLine(); C=F1.nextLine(); D=F2.nextLine(); //I did not understand objectA etc in the file is it also to be taken a strings? StringTokenizer S1=new StringTokenizer(A,","); StringTokenizer S2=new StringTokenizer(B,","); Now, S1.countTokens() gives total strings in the String A, simlarly S2.countTokens(); This total will tell now many strings of ObjectA you want? while(S1.hasMoreTokens()) { each S2.nextToken() gives you a string which you can store the way you like. }

Cliff at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.