Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Reading the content of a file into 2 different structures

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reading the content of a file into 2 different structures

    hello guys!
    I'm trying to split the text of a file into 2 fields and store part of it in a string array and the other part in a map.
    public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		try{
    		FileInputStream fstream = new FileInputStream("QT.txt");
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
     
            while ((strLine = br.readLine()) != null) {
                String[] splitOut = strLine.split("  ");
                for (String token : splitOut)
                    System.out.println((token));
            }
            in.close();
        }catch (Exception e){
        //Catch exception if any
      System.err.println("Error: " + e.getMessage());
    }
     
    	}
    Any suggestions of how I can do that?


  2. #2
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Reading the content of a file into 2 different structures

    What have you tried to do? Show the code and explain, why doesn't it work.

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading the content of a file into 2 different structures

    Hey! Thanx for ur attention!
    Actually, as I explained before, I'm trying to split the content of a .text to an ID and VALUE. Then store the VALUE in a string array, while the ID and Value should also be stored in a map. So far, I managed to split the content of .text, and populate the map with both values and ID. Also I could store each VALUE string in an individual array. The only thing that is not working for me is that I want all the VALUE strings to be stored in a single array instead of different arrays. Here is my code:

    public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		 String []STRINGS;
    		Map<String, String> ID_MAP = new HashMap<>();
    		try{
    		FileInputStream fstream = new FileInputStream("QT.txt");
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
     
            while ((strLine = br.readLine()) != null) {
                String[] splitOut = strLine.split(":");
                String str = splitOut[1];
                final String[] STRING = str.split("//");
                 ID_MAP.put(splitOut[0], splitOut[1]);

  4. #4
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Reading the content of a file into 2 different structures

    I'm not sure whether I've understood you correctly, but if you confused by the fact that each iteration of while loop creates it's own array, then you can just read all the file to a single string and only then operate on that single string. Or you can use a single List<String> to accumulate values from different iterations, and then use Collections.toArray() (if I'm not mistaken).

  5. #5
    Junior Member
    Join Date
    Jun 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading the content of a file into 2 different structures

    Thanx so much!!
    I tried what u just recommended. I split the text in the .text into 2 parts, assigned the different parts to different array lists. Now for the part that I needed to use as a String array, I converted the array list to a string array. But now the issue is that my string array needed to be final in order to be used by the method i'm using to get my final output. Any idea about how to add values from the array list to the final string array?

  6. #6
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Reading the content of a file into 2 different structures

    If you need just to create a new array from a list, then you can use List.toArray(). If you need to merge an existing array with the list's contents, you can create a new list and dump everything from an array and other list into it and then convert it to array. Or you can convert the list into array, create big enough array to contain all the desired data, and with the help of System.arraycopy() populate it with your two arrays' data.

  7. #7
    Junior Member
    Join Date
    Jun 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading the content of a file into 2 different structures

    Thanx so much!! It perfectly worked :-)

  8. #8
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Reading the content of a file into 2 different structures

    You're welcome.

Similar Threads

  1. File Structures and Format
    By sci4me in forum Java Theory & Questions
    Replies: 3
    Last Post: March 10th, 2013, 05:44 PM
  2. Reading XML content and storing in variable
    By learn_java in forum Java SE APIs
    Replies: 1
    Last Post: November 16th, 2012, 05:23 AM
  3. Replacing File Content
    By asheshrocky in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 30th, 2012, 09:48 PM
  4. Content in text file to MS Word file help!
    By ComputerSaysNo in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 27th, 2011, 11:39 AM
  5. reading content of the text file, splitting it into strings
    By Dodo in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: January 6th, 2011, 07:57 PM