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

Java: How to store string values from a text file?

  • so i have this text file that is formatted like this ObjectA, name1, name2 ObjectB, name3, name4 ObjectC, name5, name6 i am having trouble trying to save the names and the objects. how would i save these string values with commas that separate them??

  • Answer:

    You can use the next() method of the Scanner class. It will get each word delimited by a white-space. Scanner scan = new Scanner("your file") ; String s ; while (scan.hasNext() ) { s = scan.next() ; /* Then you want to remove the comma */ s[s.length] = '\0' ; /* and then insert it into whatever data structure you want */

Cliff at Yahoo! Answers Visit the source

Was this solution helpful to you?

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.