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
Related Q & A:
- How to use hadoop for text file?Best solution by Stack Overflow
- How to delete row from text file?Best solution by Stack Overflow
- How to extract information from text file in Python?Best solution by Stack Overflow
- Is there a limit on the size of a new file or a text file?Best solution by Stack Overflow
- I need help with writing up the Java Code.Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.