This is the first time I've posted a question so forgive me if I become vague. I will try my best to be as detailed as possible.

I am creating a card game in Java that compares card hit points. The hit points are accessed from an input file. The input file will look something like this:

69 M 4 4 Dragon Large medieval beast; 35 S 5 5 Alex Rodriguez Yankee slugger;

I would like to print 'Dragon' then below that 'Large medieval beast' on a Scanner window. The problem is:

How do I create a program that will also access the next line 'Alex Rodriguez' 'Yankee slugger'.

I would like to set up the program to insert new lines and then later access those lines using a cardNum (1st number). I call the lines data groups separated by a semi-colon.

I'm thinking that I need to create a while loop to come back and access the next data group. I need to be able to access the last to variables no matter how long or short they are.

Recommended Answers

All 4 Replies

Should I create a loop that searches for the ; then prints all to the next ;

This wouldn't work because I need to assign each set of characters to a variable. I'm working this out in my head and typing it...sorry

I just need to create a loop that accesses each variable and saves it. I already have code to do this once. I just need to add the while loop.

I suggest you simply read the data in as a string and first split it by the semi-colon, then feed the rest of the line to a class that will parse it into member variables.
If you can make your data consistent (either by fixed positions or fixed delimiters), it would really help.

Depending on whether your records will be arranged horizontally or vertically or both, you will need to adjust this loader.
This is by no means perfect, it is just to give you an idea of possibilities.
Part 1:

import java.io.*;
import java.util.*;

class FantasyCards
{
	public static void main(String[] args)
	{
		try
		{
			String strFileName = "c:\\science\\DaniWeb\\FantasyCards\\FantasyCards.txt";
			BufferedReader fileIn = new BufferedReader(new FileReader(strFileName));
			String strData = "";

			while("" != (strData = fileIn.readLine().trim()))
			{
				String[] arr_strData = strData.split(";");

				for(String s : arr_strData)
				{
					CardRec rec = new CardRec(s.trim());


					System.out.println(
						"\nHitPoints=" + rec.getHitPoints() +
						"\nSize=" + rec.getSize() +
						"\nFirstVal=" + rec.getFirstVal() +
						"\nSecondVal=" + rec.getSecondVal() +
						"\nCategory=" + rec.getCategory() +
						"\nDescription=" + rec.getDescription());
				}
			}

			fileIn.close();
		}
		catch(Exception exc)
		{
			System.out.println(exc.getMessage());
		}
	}
}

Part 2:

public class CardRec
{
	private String strHitPoints;
	private String strSize;
	private String strFirstVal;
	private String strSecondVal;
	private String strCategory;
	private String strDescription;

	public String getHitPoints()
	{
		return strHitPoints;
	}

	public String getSize()
	{
		return strSize;
	}

	public String getFirstVal()
	{
		return strFirstVal;
	}

	public String getSecondVal()
	{
		return strSecondVal;
	}

	public String getCategory()
	{
		return strCategory;
	}

	public String getDescription()
	{
		return strDescription;
	}

	public CardRec()
	{
		strHitPoints = "";
		strSize = "";
		strFirstVal = "";
		strSecondVal = "";
		strCategory = "";
		strDescription = "";
	}

	public CardRec(CardRec rec)
	{
		strHitPoints = rec.strHitPoints;
		strSize = rec.strSize;
		strFirstVal = rec.strFirstVal;
		strSecondVal = rec.strSecondVal;;
		strCategory = rec.strCategory;
		strDescription = rec.strDescription;
	}

	public CardRec(String strData)
	{
		String[] arr_strData = strData.split(" ");
		strHitPoints = arr_strData[0];
		strSize = arr_strData[1];
		strFirstVal = arr_strData[2];
		strSecondVal = arr_strData[3];
		strCategory = arr_strData[4];

		// put the rest of the record into the description
		StringBuilder sbDesc = new StringBuilder();
		for(int intLoop=5; intLoop < arr_strData.length; intLoop++)
		{
			sbDesc.append(arr_strData[intLoop]+" ");
		}

		strDescription = sbDesc.toString().trim();
	}

}

Thank you very much. Now the hard part trying to translate it so it fits my code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.