 |
Cheat Engine The Official Site of Cheat Engine
|
| View previous topic :: View next topic |
| Author |
Message |
NotReallySureWhatGoesHere Expert Cheater
Reputation: -1
Joined: 20 Feb 2012 Posts: 110
|
Posted: Mon Mar 05, 2012 6:44 am Post subject: Quick question regarding reading a file and storing it.. |
|
|
Alright so I'm trying to make a simple quiz game in Java and I'm stuck at how I want to read the file with the questions and answers.
It will be in a format like this: question:answer1:answer2:answer3:answer4:id (then a new line and repeat)
and I'm trying to figure out how I can read this and store these each in to a string.
I started to use a Map<String, Integer> but I can't seem to retrieve from IDs, only the string part.
I don't really want to do a string array either. Is there any other way? I need to keep the id with it so when I check to see if the question has been asked it can do that fast since there might be a lot of questions. Thanks. Any pseudo-code will be great.
EDIT: Here is some of what I have:
| Code: | package com.fbla;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
/**
*
* @author Josh
*/
public class Questionaire {
private Scanner scnr;
private String strQ;
private int id;
private ArrayList<Integer> ids;
private HashMap<String, Integer> map;
public Questionaire(String strFilePath) {
scnr = new Scanner(strFilePath);
ids = new ArrayList<Integer>();
map = new HashMap<String, Integer>();
String str;
while (scnr.hasNext()) {
str = scnr.next();
strQ = str.split(":")[5];
id = Integer.valueOf(str.split(":")[6]).intValue();
map.put(strQ, id);
}
}
public String getQuestion(int qID) {
return null;
}
} |
The getQuestion method is what I want to call to get a question with the specified ID. Currently what I have there won't work, it's just there
|
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Mon Mar 05, 2012 8:20 am Post subject: Re: Quick question regarding reading a file and storing it.. |
|
|
| One Bored Motherfucka wrote: | Alright so I'm trying to make a simple quiz game in Java and I'm stuck at how I want to read the file with the questions and answers.
It will be in a format like this: question:answer1:answer2:answer3:answer4:id (then a new line and repeat)
and I'm trying to figure out how I can read this and store these each in to a string.
I started to use a Map<String, Integer> but I can't seem to retrieve from IDs, only the string part.
I don't really want to do a string array either. Is there any other way? I need to keep the id with it so when I check to see if the question has been asked it can do that fast since there might be a lot of questions. Thanks. Any pseudo-code will be great.
EDIT: Here is some of what I have:
| Code: | package com.fbla;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
/**
*
* @author Josh
*/
public class Questionaire {
private Scanner scnr;
private String strQ;
private int id;
private ArrayList<Integer> ids;
private HashMap<String, Integer> map;
public Questionaire(String strFilePath) {
scnr = new Scanner(strFilePath);
ids = new ArrayList<Integer>();
map = new HashMap<String, Integer>();
String str;
while (scnr.hasNext()) {
str = scnr.next();
strQ = str.split(":")[5];
id = Integer.valueOf(str.split(":")[6]).intValue();
map.put(strQ, id);
}
}
public String getQuestion(int qID) {
return null;
}
} |
The getQuestion method is what I want to call to get a question with the specified ID. Currently what I have there won't work, it's just there  |
Even though you don't want to use a string array I would. Just for reading the file it would be something like
| Code: |
Scanner scan = new Scanner(new File("filename"));
while(scan.hasNextLine())
{
String[] tokens = scan.nextLine().split(":");
//each token is a position in the array here, you could also use a string
//tokenizer but this will work fine for valid input
}
|
Personally I would not put these into a map because you don't record the structure. I would create a Question class which has the question and 4 answers and the id and then write out the question class like
| Code: |
//given an ArrayList<Question> containing all questions
PrintStream out = new PrintStream(new File(outFileName));
for(Question q: questions)
{
out.println(q.getQuestion() + ":" + q.getAnswerOne() + ":" + ... + q.getId());
}
out.close();
|
Java is an OO language, make use of it. Also maps aren't really that great'
Edit: I didn't see the name of your class oops
I see you have the right idea with the Questionare class but I still wouldn't store them in a map, I would store them in either an array or an ArrayList because maps are overkill for this problem.
_________________
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8585 Location: 127.0.0.1
|
Posted: Mon Mar 05, 2012 3:51 pm Post subject: |
|
|
Or perhaps look into serialization or other methods of storing the data rather then stressing yourself over your own method. Something such as:
- XML
- Json
- CSV
_________________
- Retired. |
|
| Back to top |
|
 |
NotReallySureWhatGoesHere Expert Cheater
Reputation: -1
Joined: 20 Feb 2012 Posts: 110
|
Posted: Mon Mar 05, 2012 6:19 pm Post subject: |
|
|
Thanks for the replies and tips guys! I'm going to try some things and post back And you've helped me understand OOP a bit more with that Question class. Wow a lot of things could be easier haha.
EDIT: Alright things seem to be going smooth Here is my updated code for anyone who is interested. Also if you guys have anymore suggestions on how to improve please let me know, thanks!
| Code: | package com.fbla;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author Josh
*/
public class Questionaire {
public ArrayList<Question> questions = new ArrayList<Question>();
public Question q = new Question();
public ArrayList<Integer> usedIDS = new ArrayList<Integer>();
public Questionaire(String strFilePath) throws FileNotFoundException {
Scanner scnr = new Scanner(new File(strFilePath));
String[] tokens;
while (scnr.hasNextLine()) {
tokens = scnr.nextLine().split(":");
q.setQuestion(tokens[0]);
q.setAnswerOne(tokens[1]);
q.setAnswerTwo(tokens[2]);
q.setAnswerThree(tokens[3]);
q.setAnswerFour(tokens[4]);
q.setQuestionID(Integer.valueOf(tokens[5]).intValue());
questions.add(q);
usedIDS.add(q.getQuestionID());
}
}
} |
| Code: | package com.fbla;
/**
*
* @author Josh
*/
public class Question {
private int id;
private String strQuestion, strAnswerOne, strAnswerTwo, strAnswerThree, strAnswerFour;
public Question() {
}
public String getQuestion() {
return strQuestion;
}
public void setQuestion(String str) {
strQuestion = str;
}
public String getAnswerOne() {
return strAnswerOne;
}
public void setAnswerOne(String str) {
strAnswerOne = str;
}
public String getAnswerTwo() {
return strAnswerTwo;
}
public void setAnswerTwo(String str) {
strAnswerTwo = str;
}
public String getAnswerThree() {
return strAnswerThree;
}
public void setAnswerThree(String str) {
strAnswerThree = str;
}
public String getAnswerFour() {
return strAnswerFour;
}
public void setAnswerFour(String str) {
strAnswerFour = str;
}
public int getQuestionID() {
return id;
}
public void setQuestionID(int i) {
id = i;
}
} |
|
|
| Back to top |
|
 |
NotReallySureWhatGoesHere Expert Cheater
Reputation: -1
Joined: 20 Feb 2012 Posts: 110
|
Posted: Tue Mar 06, 2012 9:47 pm Post subject: |
|
|
Here's a minor update:
| Code: | package com.fbla;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
/**
*
* @author Josh
*/
public class Questionaire {
private ArrayList<Question> questions = new ArrayList<Question>();
private Random rnd = new Random();
public Questionaire(String strFilePath) {
Scanner scnr = null;
try {
scnr = new Scanner(new File(strFilePath));
} catch (FileNotFoundException fnfe) {
}
String[] tokens;
while (scnr.hasNextLine()) {
tokens = scnr.nextLine().split(":");
questions.add(new Question(tokens[0], tokens[1], tokens[2], tokens[3], tokens[4], tokens[5].charAt(0)));
}
}
public Question getRandomQuestion() {
return questions.get(rnd.nextInt(questions.size()));
}
} |
| Code: | package com.fbla;
/**
*
* @author Josh
*/
public class Question {
private String strQuestion, strAnswerOne, strAnswerTwo, strAnswerThree, strAnswerFour;
private char chrAnswer;
public Question(String strQ, String strOne, String strTwo, String strThree, String strFour, char chrAns) {
strQuestion = strQ;
strAnswerOne = strOne;
strAnswerTwo = strTwo;
strAnswerThree = strThree;
strAnswerFour = strFour;
chrAnswer = chrAns;
}
public String getQuestion() {
return strQuestion;
}
public String getAnswerOne() {
return strAnswerOne;
}
public String getAnswerTwo() {
return strAnswerTwo;
}
public String getAnswerThree() {
return strAnswerThree;
}
public String getAnswerFour() {
return strAnswerFour;
}
public char getCharAnswer() {
return chrAnswer;
}
} |
|
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Wed Mar 07, 2012 8:01 am Post subject: |
|
|
| Code: | package com.fbla;
/**
*
* @author Josh
*/
public class Question {
private char chrAnswer;
private String[] answers;
private String question;
public Question(String strQ, String strOne, String strTwo, String strThree, String strFour, char chrAns) {
strQuestion = strQ;
answers[0] = strOne;
answers[1] = strTwo;
answers[2] = strThree;
answers[3] = strFour;
chrAnswer = chrAns;
}
public String getQuestion() {
return strQuestion;
}
public String getAnswer(int which)
{
return answers[which-1];
}
public void setAnswer(int which, String newAnswer)
{
answers[which-1] = newAnswer;
}
public char getCharAnswer() {
return chrAnswer;
}
} |
_________________
|
|
| Back to top |
|
 |
NotReallySureWhatGoesHere Expert Cheater
Reputation: -1
Joined: 20 Feb 2012 Posts: 110
|
Posted: Wed Mar 07, 2012 2:04 pm Post subject: |
|
|
| HomerSexual wrote: | | Code: | package com.fbla;
/**
*
* @author Josh
*/
public class Question {
private char chrAnswer;
private String[] answers;
private String question;
public Question(String strQ, String strOne, String strTwo, String strThree, String strFour, char chrAns) {
strQuestion = strQ;
answers[0] = strOne;
answers[1] = strTwo;
answers[2] = strThree;
answers[3] = strFour;
chrAnswer = chrAns;
}
public String getQuestion() {
return strQuestion;
}
public String getAnswer(int which)
{
return answers[which-1];
}
public void setAnswer(int which, String newAnswer)
{
answers[which-1] = newAnswer;
}
public char getCharAnswer() {
return chrAnswer;
}
} |
|
Ah very clever!
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|