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 11 of 11

Thread: Reading From File Problem

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reading From File Problem

    I'm trying to read the inventory for a game in a text file. The inventory looks like this:
    Sword 5
    Shield 5 10
    Cake
    Sword 7
    Shield 1 5

    I need to read from a file and as I'm reading I need to know if the line read was a Sword, Shield or a Cake. Depending on what the item is I will do different things with it. It is important to know the numbers that follow the item. For example, shields have 2 ints, but swords only 1 int. The ints are the stats of the item.

    Scanner read = new Scanner(new File("inventory.txt"));
    while (read.hasNext()){        
    infile.findInLine("Shield");
    }

    Please help


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Reading From File Problem

    A couple of ways to do it:
    If you read in each line in the file as a String, you could look at the start of the String for the item name and then use that item name to control how the rest of the String was processed.
    Or you could just read the next token from the file, test what kind of item it was and then have logic for item to read in the rest of the data for that item.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading From File Problem

    How do I look at the start of a String?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Reading From File Problem

    Use of the the String class's methods: index of or starts with for example. Read the API doc for how to use them and for other choices.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading From File Problem

    Obviously I don’t understand them, hence why I am looking for help. Thanks though.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Reading From File Problem

    Which method(s) are you having problems with? Can you post some sample code.

    There is lots of sample code on the forum. Do a Search for the method(s) you want samples for.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading From File Problem

    Get off my post troll. I know you read above, so you know which methods I am having problems with.
    Last edited by Neobs; April 5th, 2012 at 08:42 PM.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Reading From File Problem

    I take it you want some one to write some code for you.
    If you can't ask a question and explain what you're having problems you'll have a hard time getting anyone to help you.

    Good luck.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following 2 Users Say Thank You to Norm For This Useful Post:

    javapenguin (April 7th, 2012), KevinWorkman (April 5th, 2012)

  10. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Reading From File Problem

    Quote Originally Posted by Neobs View Post
    Get off my post troll. I know you read my first post, so you know which methods I am having problems with.
    That member you happen to be calling a troll is one of the top contributors to the forum, who has helped countless others and who is trying to help you, in their spare and valuable time, without pay.

    @Neobs. I recommend you read the forum rules, and read the link in my signature entitled getting help, at least if you wish to receive help.

  11. The Following 4 Users Say Thank You to copeg For This Useful Post:

    Beacon (September 21st, 2012), entoke (April 9th, 2012), javapenguin (April 7th, 2012), KevinWorkman (April 5th, 2012)

  12. #10
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Angry Re: Reading From File Problem

    Quote Originally Posted by Neobs View Post
    Get off my post troll. I know you read above, so you know which methods I am having problems with.
    The only one trolling is you!!!



    I've reported your rudeness. (I saw you put down Norm in another thread too. Norm's a nice guy. Don't pick on him.)

    Also, the API is here:

    String API that Norm was talking about
    Last edited by javapenguin; April 7th, 2012 at 11:33 PM.

  13. #11
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: Reading From File Problem

    I would recommend using a BufferedReader to read each line and then using String.split("\\s+") to separate each section. You can then evaluate the values on each line in their own respective arrays to your liking.

    BufferedReader br = new BufferedReader(new FileReader(//file here));
    String line = null;
    ArrayList<String> lines = new ArrayList<String>();
    while((line = br.readLine()) !=null)
        lines.add(line);
     
    for(String s:line) {
        String[] parts = s.split("\\s+");
        //Do whatever you need to do.
    }

    Treat others with respect and they will respect you as well. Maybe they will even help you...

    Myself and many others on this forum have been very grateful for Norm's helpfulness and desire for you to actually UNDERSTAND the solution to your problem, and I'm not about to let you treat him that poorly. I will, however, still offer my own help.
    Last edited by bgroenks96; April 8th, 2012 at 10:37 PM.

Similar Threads

  1. Reading XML File using DOMParser and have problem with accessing xml
    By optiMystic23 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 21st, 2012, 02:22 PM
  2. [SOLVED] Problem in File Reading...
    By Mr.777 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 20th, 2011, 07:24 AM
  3. Problem reading correctly from a file
    By stavrakas in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 6th, 2011, 08:09 AM
  4. Problem in reading image file
    By ramhanuman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 3rd, 2011, 02:46 PM
  5. Problem on reading file in Java program
    By nathanernest in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: April 13th, 2009, 08:14 AM

Tags for this Thread