Results 1 to 10 of 10
  1. #1
    Mokomi is offline Member
    Join Date
    May 2011
    Posts
    32
    Rep Power
    0

    Default Reading nextInt() then nextLine() form a file.

    I'm making a database to hold all information about item/spells/races/npcs/etc.
    I am able to have it read the file, but In order to keep it organized in the file itself for easy editing/searching in the txt file I need to add letters and numbers to the file. Atm i'm trying to organize my "race" page
    1: Human
    2: Mutant
    3: Phill
    etc.
    (the file currently looks like
    Human
    Mutant
    Phill) it's all very not organized)
    it errors out because I "need" both the int (1,2,3) and the race name (Human,Mutant,Phill) but my while loop will not allow me to have both
    i
    Java Code:
    mport java.util.*;
    import java.io.*;
    
    public class start {
    
    public static void main(String[] args) throws IOException 
    {
    int count = 0;
    
    String race ="0";
    
    Scanner fileScan = new Scanner (new File("Races.txt"));
    while (fileScan.hasNext())
    {
    race = fileScan.nextLine();
    System.out.print(count+1+": "+race+"   ");
    count++;
    }
    }
    }
    Wondering if there is a way to "skip" over a line until it hits a specific new line. or another way i should be looking at this problem.

    Thank you in advance!

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default Re: Reading nextInt() then nextLine() form a file.

    You question is a bit vague.

    If I was to give you a file and you must write a program to read it but I give you no idea of what the contents are, how are you supposed to read it? You cannot. You have to have some idea of what the format the data is in. What format is your file in?

  3. #3
    Mokomi is offline Member
    Join Date
    May 2011
    Posts
    32
    Rep Power
    0

    Default Re: Reading nextInt() then nextLine() form a file.

    O i'm sorry. thought i had a description already.
    file is Races.txt.
    PHP Code:
    Human
    Mutant
    Half-Mutant
    Phill
    Cirgie
    (not going into details beyond this)
    What I want to go is
    PHP Code:
    1: Human
    2: Mutant
    3: Half-Mutant
    4: Phill
    5: Cirgie

    using a while loop to read all the .txt works for now (and the counter shows what number they are) But the .txt file is going to get much bigger. I wanted to know if there was a way to either
    A: Have it read nextInt(); then nextLine(); so i can keep it organized in the file itself and still read correctly
    B: find a way read the race itself but not the "1: " (would be even better if i can get it to read just the race themselves. Would be able to put in descriptions benifits etc all in one file.

  4. #4
    Mokomi is offline Member
    Join Date
    May 2011
    Posts
    32
    Rep Power
    0

    Default Re: Reading nextInt() then nextLine() form a file.

    Also want to add
    If you know a better way I should be looking at this (like hey did you know you can work java into excel and just tell it what cells you want to read) I would be forever happy with you.

  5. #5
    Neilos is offline Member
    Join Date
    Aug 2011
    Posts
    10
    Rep Power
    0

    Default Re: Reading nextInt() then nextLine() form a file.

    Regular expressions?

  6. #6
    Neilos is offline Member
    Join Date
    Aug 2011
    Posts
    10
    Rep Power
    0

    Default Re: Reading nextInt() then nextLine() form a file.

    Or use of a delimiter.

    Check out StringTokenizer() // obsolete apologies

    Try String.split http://download.oracle.com/javase/1....a.lang.String)
    Last edited by Neilos; 09-05-2011 at 05:20 AM.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default Re: Reading nextInt() then nextLine() form a file.

    The best way would be to store your data in a database.

    I'm still unclear as to what you are trying to achieve. The point I was trying to make earlier is that you can write a Java program to read a text file as long as you know what the format is. If you write a program that exports the data out to a text file in a known format and then you can read it back in using that same known format. By the way it would be wise to keep the format uniform. For example:
    String
    int
    int
    int

    String
    int
    int
    int
    int
    int

    String
    int
    int
    etc

    Or
    String
    int int int

    String
    int int int int int

    String
    int int

    Or whatever you like. And not

    String
    int
    String

    int
    String
    int
    int

    String String
    int int

    That would too hard to read back in.

  8. #8
    Mokomi is offline Member
    Join Date
    May 2011
    Posts
    32
    Rep Power
    0

    Default Re: Reading nextInt() then nextLine() form a file.

    solved the original problem! I found that if i just say next(); it completely fixes my problem of String then an int while in the while loop. my format right now is.

    Java Code:
    fileScan.next();
    fileScan.next();
    fileScan.next();
    fileScan.next();
    fileScan.next();
    fileScan.next();
    fileScan.next();
    fileScan.next();
    fileScan.next();
    fileScan.next();
    while (fileScan.hasNext())
    {try{race=fileScan.next();
    
    }
    catch(Exception e){
    }
    
    System.out.print(race+" ");
    count++;
    }
    }
    }
    race=fileScan.next();
    how i have the text file.
    PHP Code:
    #  Race  Perception	Physical	Magic	Manipulation	Precistion	Reaction	Agility	Focus
    1::human	    0	                1		-1	 0	                0       		0		0	0
    2::mutant	    0	               -1		1	 0	                0	        	0		0	0
    3::phill	    0	               0		0	-1	                0		      +1		0	0
    I have another class that shows and organizes it all. I might have to do another .txt file to add descriptions and a third .txt file to show all the "outside of stat" problems and benifits. going to make a new thread asking how to do just that.(Search for a word of a phrase)
    Why? well when in combat situations i'm going to have to look up the personal changes. like lets say a check to see that phill deals 1 more damage vs mutants. or something like that.
    Last edited by Mokomi; 09-05-2011 at 10:27 PM.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default Re: Reading nextInt() then nextLine() form a file.

    You may think you have solved your problem but it is far from perfect. Why are you reading a bunch of data only to throw it away? What happens if the amount of data changes?

  10. #10
    Mokomi is offline Member
    Join Date
    May 2011
    Posts
    32
    Rep Power
    0

    Default Re: Reading nextInt() then nextLine() form a file.

    You may think you have solved your problem but it is far from perfect. Why are you reading a bunch of data only to throw it away? What happens if the amount of data changes?
    temp solution for sure! exactly! I would love it so if I ever add a new race (believe me I am) I don't have to go into 5 different .txt files to add them all. I would love to know if I'm able to do a word or "phrase" search in .txt for lets say human. Then the next 10 numbers will be the stats array then any other effects array. sadly I currently do not know how to end this other stats array. some are longer than others. (like humans will have aura and need less sleep while mutants need a lot of sleep/food but have a higher mana regen etc.) would hate to do a .txt file for every different stat.

    Regular expressions?
    don't know what that is exactly going to check it out or look it up tomorrow.
    Or use of a delimiter.

    Check out StringTokenizer() // obsolete apologies

    Try String.split http://download.oracle.com/javase/1....a.lang.String)
    doing a quick glance. I'm trying to see stringtokenizer. seems intresting. the example they give separates each word. might find a way to separate from each "row" or a phrase of some kind.

Similar Threads

  1. nextint() Method !
    By M7MD in forum Threads and Synchronization
    Replies: 6
    Last Post: 10-25-2010, 06:13 AM
  2. Very Bad problem with nextInt()
    By usiyalla in forum New To Java
    Replies: 2
    Last Post: 07-25-2010, 01:44 AM
  3. Help in reading values from html form in java
    By ichkoguy in forum Advanced Java
    Replies: 7
    Last Post: 03-16-2009, 07:45 AM
  4. Reading data form Excel using Jdbc (example)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-13-2008, 11:23 AM
  5. Replies: 0
    Last Post: 11-27-2007, 10:23 AM

Posting Permissions

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