-1

I am making a simple text based card game for fun/practice with parsing files. I have a plain text file with all the specs for the card. My cards are split with "##########". They are multilined. For now I simply want to be able to pull up the entirety of any ONE card whenever I want. For example, Player picks character 1, so I pull up Card 1 Only? How ?

EXAMPLE: 
##########
CARD 1
Character Name: 
Something Else:
##########
CARD 2
Character Name: 
Something Else:
##########
Character Name: 
Something Else:
##########

HOW CAN I ACTUALLY SPLIT THE CARDS SO THAT I CAN JUST ASK THE USER WHICH CARD. I don't want to have to read the lines and print the way I did. It is rather cumbersome and convoluted. My NEW ATTEMPT: ArrayList listForCard1 = new ArrayList(); Integer selected_card = 1;

        try {

            String line;
            FileReader fR = new FileReader("MyText.txt");
            BufferedReader br = new BufferedReader(fR);
            int x = 0;
            Integer card = 1;
            while ((line = br.readLine()) != null) {
                ALines[x] = line;
                x++;
                if (line.contains("##########")) {
                    if ( card == selected_card) {
                       listForCard1.add(br.readLine());
                      // System.out.println(br.readLine());
                       break;
                    } else {
                       card++;
                    }
                }

            }
            System.out.println("Done");


            System.out.println(ALines[0]);
            System.out.println(ALines[1]);
            System.out.println(ALines[2]);
            System.out.println(ALines[3]);
            System.out.println(ALines[4]);
            System.out.println(ALines[5]);
            System.out.println(ALines[6]);


        } catch (IOException e) {
            e.printStackTrace();
        } 
}
StreamingBits
  • 137
  • 11

1 Answers1

1

try this

        ArrayList<String> listForCard1 = new ArrayList<String>();
    Integer selected_card = 1;

    BufferedReader br = null;   
    try {

        String line;

        br = new BufferedReader(new FileReader("MyText.txt"));
        Integer card = 1;
        while ((line = br.readLine()) != null) {
            if (line.contains("##########")) {
                if ( card == selected_card) {
                   listForCard1.add(br.readLine());
                   listForCard1.add(br.readLine());
                   listForCard1.add(br.readLine());
                   break;
                } else {
                   card++;
                }
            }

        }
        System.out.println("Done");
        for (String s : listForCard1) {
            System.out.println(s);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)
                br.close();

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

select the card by setting selected_card.. we count how many #####'s we see when we have seen enough we read out that card and stop reading the file.. then print it from the arraylist

clancer
  • 613
  • 4
  • 10
  • problem. Can't preserve the file. I want to preserve the format. It's not preserving the cards line by line – StreamingBits Mar 03 '14 at 00:01
  • This specific code is not modifying the file, what are you referring to? do you later need to write the information in listForCard1 back to the MyText.txt file? – clancer Mar 03 '14 at 00:07
  • I figured it out. Except it is very innefficient. Right now I did this – StreamingBits Mar 03 '14 at 00:19
  • if you stick to a fixed line size you can skip right to a card using a random access file http://stackoverflow.com/questions/9769874/reaching-a-specific-line-in-a-file-using-randomaccessfile – clancer Mar 03 '14 at 00:23
  • I need to go to each card and print that specific card when prompted. I was hoping to split the file based on the splitting by the ("##########"). I will put my new code up in a sec. – StreamingBits Mar 03 '14 at 00:23