d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Text From File Manipulation
12Next
Add Reply New Topic New Poll
Member
Posts: 2,183
Joined: May 8 2009
Gold: 1.00
Apr 14 2014 07:00pm
I'm working on a Trivia Game project for class. I am having trouble with adding every 5 lines to an object, I understand they need to be in variables. I just don't know how to use
BufferedReader properly with a for loop so it reads the whole file but seperates each object by 5 lines.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 14 2014 07:17pm
personally, unless the file is huge, i prefer reading the file into a List, then perform your logic on the list.
Code
// i recommend using apache's FileUtils.readLines(file) to do this
BufferedReader br = new BufferedReader(new FileReader(filename));
List<String> lines = new ArrayList<String>();
String line = null;
while ((line = br.readLine()) != null) {
lines.add(line);
}
br.close();


then loop through the list, grabbing every 5 lines as properties for each object
Code
List<Badass> badasses = new ArrayList<Badass>(lines.size() / 5);
for (int i = 0; i < lines.size(); i += 5){
String geassPower = lines.get(i+0);
String name = lines.get(i+1);
....
Badass lelouch = new Badass(geassPower, name, ...);
badasses.add(lelouch);
}


something to that effect

/edit
Quote
5 User(s) are reading this topic (2 Guests and 1 Anonymous): Kellye, Minkomonster

inb4 overengineer

This post was edited by carteblanche on Apr 14 2014 07:34pm
Member
Posts: 2,183
Joined: May 8 2009
Gold: 1.00
Apr 14 2014 07:25pm
Thanks, I did something similar but didn't work out like you have written, thanks
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Apr 14 2014 08:01pm
Quote (carteblanche @ Apr 14 2014 08:17pm)
inb4 overengineer


I've been looking up Code Geass, lol.

Quote
Thanks, I did something similar but didn't work out like you have written, thanks


You could post yours. Then we would be able to show you why it isn't working as intended.

This post was edited by Minkomonster on Apr 14 2014 08:02pm
Member
Posts: 2,183
Joined: May 8 2009
Gold: 1.00
Apr 14 2014 08:05pm
It's not that it is not working. It's that I need to do it object oriented. The questions for the trivia game are unspecified and coming from a text file. I will post my github so you can read the instructions and maybe better understand
the idea of what I am looking for.

The code from github has since been deleted and am reworking. It seemed a bit too procedural.

https://github.com/green-brad/triviagame

Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 14 2014 08:12pm
Code
I've been looking up Code Geass, lol.



only lelouch can conquer the world with an army so small it rivals the army from Merlin

Code
It's not that it is not working. It's that I need to do it object oriented. The questions for the trivia game are unspecified and coming from a text file. I will post my github so you can read the instructions and maybe better understand
the idea of what I am looking for.

The code from github has since been deleted and am reworking. It seemed a bit too procedural.

[URL=https://github.com/green-brad/triviagame]https://github.com/green-brad/triviagame[/URL]


so uhh. what happens if there's only 1 question? or 13 questions?

This post was edited by carteblanche on Apr 14 2014 08:13pm
Member
Posts: 2,183
Joined: May 8 2009
Gold: 1.00
Apr 14 2014 08:14pm
all of that was just a baseline, not a finished program. thats why I said it has been deleted
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Apr 14 2014 08:46pm
Quote (Kellye @ Apr 14 2014 09:05pm)
It's not that it is not working. It's that I need to do it object oriented. The questions for the trivia game are unspecified and coming from a text file. I will post my github so you can read the instructions and maybe better understand
the idea of what I am looking for.

The code from github has since been deleted and am reworking. It seemed a bit too procedural.

https://github.com/green-brad/triviagame


You want to think object oriented? It is actually pretty easy. Easiar, and more intuitive than procedural in my opinion. Because we do it intuitively.

What is an object? It is an abstract model of something. An object contains fields which define what they are and methods which define what the object can do.

Let's define some objects.

Your trivia game.

Quote
Write a trivia game that asks two players a set of multiple choice questions.
Players will take turns answering multiple choice questions. When a player
  answers correctly, that player gets 1 point. The player does not lose points
  for answering incorrectly. After all of the questions are answered, display
  the number of points each player earned and declare a winner.


I identified 3 things we can encapsulate as objects above:
Game
Player
Question

Let's abstract them out further.

What is a game? What does it have? What does it do?

A Game has
-Players
- Questions

A Game does
-selects a player to ask a question (game logic, turn based)
-determines a winner

What is a Player? What does it have? What does it do?
A player has
-A name (identifier)
-points

A player does
-answers a question


What is a Question? What does it have? What does it do?
A question has
- multiple choices
-an answer

A question does
-determine if it was answered correctly (a stretch, but go with it)


So can we wrap these objects up?

Code


public class Question {
//a question has question text
private String text;

//a question has multiple choices
private String choices;

//a question has an answer
private String answer;

public Question(String theText, String theChoices, String theAnswer)
{
text = theText;
choices = theChoices;
answer = theAnswer;
}

//a question can determine if its answer is matched
public boolean isCorrect(String guess)
{
return guess.equals(answer);
}

//a question is represented by its text and choices
public String toString()
{
return text + "\n" + choices;
}


}


Code
import java.util.Scanner;

public class Player {
//a player has a name
private String name;
private int points;

public Player(String theName)
{
//set up this player with a name and 0 points
name = theName;
points = 0;
}

//players can answer questions
public String answerQuestion(Question q)
{
Scanner in = new Scanner(System.in);
System.out.println(q);

String answer = in.nextLine();

return answer;

}

//players accumulate points
public void addPoint()
{
points++;
}

//players tell how many points they have
public int getPoints()
{
return points;
}

//players are represented by their name
public String toString()
{
return name;
}

}


Code


public class Game {

//a game has players
private Player[] players;

//a game has questions
private Question[] questions;

public Game()
{
//when a game is created, it must intialize its players and questions

//create 2 players
players = new Player[]{ new Player("Bob"), new Player("Mary")};

//create 5 questions
questions = new Question[] {new Question("What color is the sky?", "A. Blue, B. Red, C. Yellow","A"),
new Question("What color is the grass?", "A. Blue, B. Red, C. Green","C"),
new Question("What color is the Sun?", "A. Blue, B. Yellow, C. Red","B"),
new Question("What color is the Moon?", "A. White, B. Red, C. Yellow","A"),
new Question("What color is the dirt?", "A. Blue, B. Brown, C. Yellow","B")
};

}

//a game asks each player a question until all questions are asked
public void playGame()
{
String answer;

for(int q = 0; q < questions.length; q++)
{
for(int p = 0; p < players.length; p++)
{
//Ask first player a question and judge his answer
System.out.println(players[p] + " please answer this question:");
answer = players[p].answerQuestion(questions[q]);

if(questions[q].isCorrect(answer))
players[p].addPoint();

}

}

if(players[0].getPoints() > players[1].getPoints())
System.out.println(players[0] + " wins the game.");
else if(players[0].getPoints() < players[1].getPoints())
System.out.println(players[1] + " wins the game.");
else
System.out.println("It was a tie");
}

}




Objects.
Member
Posts: 2,183
Joined: May 8 2009
Gold: 1.00
Apr 14 2014 11:35pm
Great reply and reasoning. But that still leaves me befuddled as to how to read the file and for every 5 lines, place each line as a string into an object as a question, which was what this thread was originally about.

The rest I have down from there.

Basically, this is my thought on it.

Trivia Game
------------

Questions in File.txt
Get every 5 lines from File as individual strings
Place those group of individual strings into an object
Place those objects into array
Call random subscript from the array until no more questions are left

-------------

That was my baseline before I even want to think about anything else. I'm stuck at putting every other 5 lines into an object. I'm not sure what kind of method to write.

This post was edited by Kellye on Apr 14 2014 11:38pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Apr 14 2014 11:57pm
Quote (Kellye @ Apr 15 2014 12:35am)
Great reply and reasoning. But that still leaves me befuddled as to how to read the file and for every 5 lines, place each line as a string into an object as a question, which was what this thread was originally about.

The rest I have down from there.

Basically, this is my thought on it.

Trivia Game
------------

Questions in File.txt
Get every 5 lines from File as individual strings
Place those group of individual strings into an object
Place those objects into array
Call random subscript from the array until no more questions are left

-------------

That was my baseline before I even want to think about anything else. I'm stuck at putting every other 5 lines into an object. I'm not sure what kind of method to write.


Code
try
{
Scanner in = new Scanner(new File("Questions.txt"));

String l1,l2,l3,l4,l5;

l1 = in.nextLine();
l2 = in.nextLine();
l3 = in.nextLine();
l4 = in.nextLine();
l5 = in.nextLine();

//assuming you have a Question constructor that takes
//five strings as paramaters
Question q = new Question(l1,l2,l3,l4,l5);

}
catch(FileNotFoundException fnfe)
{
//handle exception
}
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll