0

EDIT: I guess this thread can be closed, since all my questions have been answered! Thanks to everyone who helped me!

EDIT: I stumbled upon an error at openFileInput("myfilename.txt");. This is the error: The method openFileInput(String) is undefined for the type Game. I read an answer here: Android File I/O openFileInput() undefined, but must admit that I don't quite understand it...

I'm trying to read parts of a text file, till the token ";". This is the code I wrote:

InputStream instream = openFileInput("myfilename.txt");

String line=null;

InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader); 

while((line=buffreader.readLine())!=null){
    String[] parts=line.split(";");
    int intOne = Integer.parseInt(parts([0]);
    int intTwo = Integer.parseInt(parts([1]);
    String strLine = parts([3]);
}

public static void Start(){
    while(there is text in the file){
        // read first line till ';';
        // name that variable intOne;
        // read first line after ';' till next ';';
        // name that variable intTwo;
        // read next line, declare as strLine;
        // System.out.println(strLine);
    }
}

Beneath it is the idea of what it should do. But I have some questions:

  1. Am I right to say that the String[] parts is an array?

  2. I want to have a bigger file, but read only 3 lines per loop. Or could I, when I have a file of 100 lines, read that all at once, then recall them from the parts[]? Or would that take way too much time?

  3. Where should the text file be? When I'm testing in Eclipse, in the project folder? And when I export it inside the jar?

Hope someone can answer my questions!

(My source is: Read specific string from each line of text file using BufferedReader in java, all credits to Gilbert Le Blanc!)

EDIT: When I do this in the file:

Hello,
I am coding;

Will the pars[0] be Hello,, because that's one line, or Hello, I am coding? And will it take the enter with it?

Another EDIT: I wish to create some sort of textbased RPG engine, where you only have to edit the text file, to change the story. For example (in the text file):

30;60;       //Score needed for this piece of the story
Hello!;      // The text
Hi!;5;       // The first possible answer; the score you'll get when you answer this
Shut up!;-5; // The second possible answer; the score you'll get when you answer this
Community
  • 1
  • 1
ikhebgeenaccount
  • 353
  • 6
  • 20
  • 1. Yes, `String[] parts` is a String array with the name `parts`. But keep in mind, there's some issues with your code. `int intOne` is assigned twice. The second assignment replaces the first assignment. I believe you mean `int intSecond` on the second one. Next, `parts([3])` is the fourth index, so you're actually writing into four rows of the array, with the third index being empty. Is this intended? – theGreenCabbage Dec 04 '13 at 18:11
  • why not fileReader for reading character file? – M Sach Dec 04 '13 at 18:12
  • Your actually only reading one line per iteration of the loop. The line is getting split into a String array of words. The code will work for a million lines, doesn't matter as long as the file has then many lines. – Paul Samsotha Dec 04 '13 at 18:12
  • @theGreenCabbage Yeah, type mistake... Thanks! &MSach I dont know how fileReader works... Found this on internet, looked nice, so thought I'd give it a try. &peeskillet But will it take a long while to read 100 lines? – ikhebgeenaccount Dec 04 '13 at 18:13
  • You dont need to use String.parseString on a string array to get a string. The array is already filled with strings. ;) – Ben Dec 04 '13 at 18:14
  • 100 lines is so small that optimizing would provide negligible and questionable speed benefits ;) – theGreenCabbage Dec 04 '13 at 18:15
  • Furthermore, it depends on how your file looks. Could you post a sample of the file you are reading from? What is your decision behind only reading three lines per loop? Is the file just iterations of 3 lines? – theGreenCabbage Dec 04 '13 at 18:16
  • @theGreenCabbage See my edit. With example! – ikhebgeenaccount Dec 04 '13 at 18:21
  • Oh, I see now. Yeah, what you have should work, but `String strLine = parts([3]);` should be `String strLine = parts([2]);` since you want to read the third index as opposed to the fourth. Third index would assign as `Hello!` while the fourth index would assign `Hi!`. – theGreenCabbage Dec 04 '13 at 18:23
  • Yes, that's true, but the numbers are just there for 'testing'. I didn't have the syntax of the file yet, but you have a point! – ikhebgeenaccount Dec 04 '13 at 18:24
  • I'll remember that. I will try and keep the question broader (hope that's the right word). – ikhebgeenaccount Dec 04 '13 at 18:35
  • Okay! I'll erase the previous thought and remember this. EDIT: Just a quick question, for clarification: should it help if I ask about the openFileInput error in a new question? @ZongZhengLi – ikhebgeenaccount Dec 04 '13 at 18:44

1 Answers1

0

What you rly want is reading one char after another. I used some nice BufferedSegmentReader from the framework Smooks, which could be interesting for you. Look at the sourcecode here:

http://grepcode.com/file/repo1.maven.org/maven2/org.milyn/milyn-smooks-all/1.5/org/milyn/edisax/BufferedSegmentReader.java

It reads characters one after another from a stream and puts it into a StringBuffer. There are delimiters to indicate when one "segment" is done reading, and after that you can work with this segment till you tell the BufferedSegmentReader to move on.

I think this rly suits your case and is an approach you are looking for.

Ben
  • 1,157
  • 6
  • 11
  • I'll have a look! But thank you in advance. EDIT: Well, it looks great, but I think I'll stick with the easier stuff. I'm not that experienced with Java yet, but I'll have a closer look at it when I'm more used to Java! – ikhebgeenaccount Dec 04 '13 at 18:23
  • No problem, but after you edited your question I realized it could be that I misunderstood you ;) If its rly for just a text file like you showed as an example, you shouldn't rly bother about performance. Just some hundred of lines is no problem at all for using it with the normal BufferedReader – Ben Dec 04 '13 at 18:26