Results 1 to 7 of 7
  1. #1
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default Help with File reading

    Hi friends,

    I have a file with mixture of strings and integers like this

    Java Code:
    aaaa 1
    bbbb 2
    cccc
    dddd 3 eeee 4
    I know exactly where the string or an integer is in the file.
    So I need to do something like this. I need to read the string "aaaa" first, then I will read 1, then I'll read "bbbb", then 2 and so on.

    Is there any way in which I can do the above. Something like readNextString() or readNextInteger().

    I know I can use BufferedReader and read lines and do manipulation on each lines. But I am looking for something that we have in C++ like shown below

    Java Code:
        int number;
        char name[10] ;
        ifstream in("test.txt");
        if (in.is_open())
        {
            in >> name; [I][COLOR="YellowGreen"]//gets "aaaa" here[/COLOR][/I]
            in >> number; [COLOR="YellowGreen"][I]//gets 1 here[/I][/COLOR]
            [I][COLOR="YellowGreen"]//and so on[/COLOR][/I]
        }

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default

    Take a look at the Scanner class

  3. #3
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    Oh... how could I forget Scanner. Thanks for reminding. :)
    But, Scanner doesn't have a getString() method nah. So any way to do it?

  4. #4
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by subith86 View Post
    Oh... how could I forget Scanner. Thanks for reminding. :)
    But, Scanner doesn't have a getString() method nah. So any way to do it?
    Try using a BufferedReader and the String class.

    Java Code:
    File file = new File("path");
    BufferedReader bReader = new BufferedReader(file);
    String line = bReader.readLine();
    And use the String class's methods to work with the String you read and grab certain values from it.

    Perhaps the most useful methods for you would be substring() and indexOf().

    Good luck!

  5. #5
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    Quote Originally Posted by Solarsonic View Post
    Try using a BufferedReader and the String class.
    @Solarsonic : As said in my first post, I already use BufferedReader. My purpose is to extract a string and an integer separately.

    I'll put it in simple way here. In one fine I have just one line which is a string-integer pair. I have a String variable and int variable in my code. From the string-integer pair, I need to put the string value in String variable and the integer value in the int variable. The programmer has the information that the string-integer pair will be separated by space.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by subith86 View Post
    Hi friends,

    I have a file with mixture of strings and integers like this

    Java Code:
    aaaa 1
    bbbb 2
    cccc
    dddd 3 eeee 4
    I know exactly where the string or an integer is in the file.
    So I need to do something like this. I need to read the string "aaaa" first, then I will read 1, then I'll read "bbbb", then 2 and so on.

    Is there any way in which I can do the above. Something like readNextString() or readNextInteger().
    As already mentioned if you were using a Scanner, you could use nextInt() to get your number and next() to get a String token and nextLine() to get a String line, but you would need to take care to handle the end of line token. I'm not sure why you dismissed this suggestion out of hand. Otherwise you could grab your lines and split them via String's split method and iterate through the array of String/tokens that are returned.

  7. #7
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    Thanks Fubarable,
    This is exactly what I need. I overlooked next() method. That is giving me the next string in the file.

Similar Threads

  1. Reading file external to jar file
    By nn12 in forum New To Java
    Replies: 6
    Last Post: 02-04-2011, 05:46 AM
  2. reading a file and writing to a file....help!!!!
    By java_prgr in forum New To Java
    Replies: 3
    Last Post: 07-26-2010, 06:53 PM
  3. Replies: 9
    Last Post: 10-20-2009, 10:52 AM
  4. Replies: 3
    Last Post: 05-09-2009, 11:31 PM
  5. Reading from file
    By kiab3000 in forum New To Java
    Replies: 0
    Last Post: 03-14-2009, 06:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •