Trouble Calling a Method

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 506 Views - Last Post: 26 September 2012 - 09:18 PM Rate Topic: -----

#1 BigD959919  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 91
  • Joined: 26-March 12

Trouble Calling a Method

Posted 26 September 2012 - 07:03 PM

Ill post my two classes that are conflicting with each other first then explain my issue.

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

public class WorkClass {
	public static WorkClass useTextFile(String file)throws IOException {
		String quiz_name, category, question, answer, next;
	    int num_questions, max_num_questions, num_chances, num_answers;
	    Holder HOLD;
	    WorkClass WC;
	    
	    FileReader in = new FileReader(file);
	    Scanner trivia_in = new Scanner(in);
	     
	    quiz_name = trivia_in.nextLine();
	    num_questions = trivia_in.nextInt();
	    num_chances = trivia_in.nextInt();
	    next = trivia_in.nextLine();

	    // Instantiate the Trivia Game.
	    HOLD = new Holder(quiz_name, max_num_questions, num_chances);

	    // Scan in and set up the questions and answers.
	    for (int i = 1; i <= num_questions; i++)
	    {
	      category = trivia_in.nextLine();
	      question = trivia_in.nextLine();
	      num_answers = trivia_in.nextInt();
	      next = trivia_in.nextLine();
	      HOLD = new Holder(category, question, num_answers);
	      for (int j = 1; j <= num_answers; j++)
	      {
	        answer = trivia_in.nextLine();
	        HOLD.save_answer(answer);
	      }
	      HOLD.insertQuestion(HOLD);
	    }
		
	    return WC;
	}
}



Holder Class
public class Holder {
	String quiz_name, category, question, answer;
	int max_num_questions, num_chances, num_answers, remaining_chances, num_correct = 0, num_incorrect = 0, curr_num_questions = 0;
	Holder[] questions;
	boolean[] correct;
	StringLogInterface SLI;
	
	public Holder(String category, String question, int max_numbers) {
		this.category = category;
		this.question = question;
		SLI = new StringLog("Trivia", max_numbers);
	}

	public Holder(String quiz_name, int max_num_questions, int num_chances) {
	    this.quiz_name = quiz_name;
	    this.max_num_questions = max_num_questions;
	    this.num_chances = num_chances;
	    remaining_chances = num_chances;
	    questions = new Holder[max_num_questions];
	    correct = new boolean[max_num_questions];
	}
	
	public String get_category() {
		return category;
	}
	
	public String get_question() {
		return question;
	}
	
	public boolean try_answer(String ANS) {
		return SLI.contains(ANS);
	}
	
	public void save_answer(String ANS) {
		SLI.insert(ANS);
	}
	
	public String get_quiz_name() {
		return quiz_name;
	}

	public int get_num_chances() {
	    return num_chances;
	}

	public int get_remaining_chances() {
	    return remaining_chances;
	}

	public int get_num_correct() {
	    return num_correct;
	}

	public int get_num_incorrect() {
	    return num_incorrect;
	}

	public int get_curr_num_questions() {
	    return curr_num_questions;
	}
	
	public Holder get_trivia_question(int QUESTNUMBER) {
	    return questions[QUESTNUMBER - 1];
	}

	public boolean is_answered(int QUESTNUMBER) {
	    return correct[QUESTNUMBER - 1];
	}
	
	public boolean is_over() {
	    return (num_correct == curr_num_questions)
	           ||
	           (remaining_chances <= 0);
	}
	
	public void insertQuestion(Holder question) {
	    questions[curr_num_questions] = question;
	    correct[curr_num_questions] = false;      
	    curr_num_questions = curr_num_questions + 1;
	}
	  
	public void correct_answer(int QUESTNUMBER) {
	    correct[QUESTNUMBER - 1] = true;
	    num_correct = num_correct + 1;
	    remaining_chances = remaining_chances - 1;
	}

	public void incorrect_answer() {
	    num_incorrect = num_incorrect + 1;
	    remaining_chances = remaining_chances - 1;
	} 
}



I am trying to add the question taken from the .txt file to the array of questions I have setup in the Holder class but I am getting an uninitalized error on

return WC


Which I can understand because I am never setting WC to anything but I can't think of another solution that doesn't involve adding in another class which I can't due do to project limitations. Any suggestions would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Trouble Calling a Method

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7749
  • View blog
  • Posts: 13,106
  • Joined: 19-March 11

Re: Trouble Calling a Method

Posted 26 September 2012 - 07:27 PM

What exactly is it you're trying to achieve here? What do you start with, and what do you want to end with?

It's obvious to me that you're not going at it the right way, but I'm not sure exactly what it is that would be the right thing to do, because I'm not sure what you want to do.

Quote

I am trying to add the question taken from the .txt file to the array of questions I have setup in the Holder class



This seems pretty simple: read the file into a a long String, with some sort of separator ("\n" if they're separated by newlines and there are no other newlines within the questions, or some unique character string), and then split on your separator. Now you've got an array of Strings, which are your questions.

However, maybe it's more complicated than that.
Was This Post Helpful? 0
  • +
  • -

#3 BigD959919  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 91
  • Joined: 26-March 12

Re: Trouble Calling a Method

Posted 26 September 2012 - 07:37 PM

The goal of the project is to read in a structured text file whos contents form a quiz. This is actually a rework of an example in the book but we were tasked to do so better and with as few classes as possible. So I am trying to do this in 3 working classes and 2 stringlog classes, the stringlog classes are just from the book and help illustrate the advantages of this particular format.

My WorkClass is meant to read in the text files contents and get them saved into the stringlog. My Holder class is meant to do everything else. Checking the number of questions, the number of acceptable answers, the number of chances left, etc. The line I am having trouble with is

HOLD.insertQuestion(HOLD);



With that line I am attempting to take the question that was read from the file and add it to the array of questions I am building in the holder class. The original method was to throw this into class 3 then to class 2 so I am attempting to go from class 1 to class 2 or cut out the middle step.

I didnt mention this but since I am not inatilaizing WC it is giving me compiler errors and wants me to inatilaze WC to null which wont work.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7749
  • View blog
  • Posts: 13,106
  • Joined: 19-March 11

Re: Trouble Calling a Method

Posted 26 September 2012 - 07:44 PM

Clearly if the object is null, there's nothing much you can do with it.

Why don't you want to initialize it now?



HOLD.insertQuestion(HOLD);



That is a weird line. You're calling a method of the object HOLD (please use standard object naming conventions!) and passing the same object as an argument. Why? How does it ever know what the question is that you're trying to insert? If the HOLD object already knows, then the job is done. If it doesn't, where does it find out?
Was This Post Helpful? 0
  • +
  • -

#5 BigD959919  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 91
  • Joined: 26-March 12

Re: Trouble Calling a Method

Posted 26 September 2012 - 07:56 PM

Originally the line above the for loop for the answers called a method for a different class that looked at one question. The way I have it now, I think (I am an EE, I prefer hardware), is my Holder class looks at the individual question as well as all the other misc events where as before there was a different class to handle all the misc events too.

So I guess that the question is already getting to the log but the .insertQuestion method is needed for the total nuber of questions.

Also I apologize about the programming convention, yet again I am an EE, this is my second and last programming course I have to take, it is just not my cup of tea.
I dont want WC to be null but I dont know how to inatalize it other than calling a method.

This post has been edited by BigD959919: 26 September 2012 - 07:58 PM

Was This Post Helpful? 0
  • +
  • -

#6 BigD959919  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 91
  • Joined: 26-March 12

Re: Trouble Calling a Method

Posted 26 September 2012 - 08:03 PM

I think I solved this issue but it brought up three more when I try to compile the program :(

Here is my new Holder class

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

public class WorkClass {
	public static WorkClass useTextFile(String file)throws IOException {
		String quiz_name, category, question, answer, next;
	    int num_questions, max_num_questions, num_chances, num_answers;
	    Holder HOLD;
	    WorkClass WC = new WorkClass();
	    
	    FileReader in = new FileReader(file);
	    Scanner trivia_in = new Scanner(in);
	     
	    quiz_name = trivia_in.nextLine();
	    num_questions = trivia_in.nextInt();
	    num_chances = trivia_in.nextInt();
	    next = trivia_in.nextLine();

	    // Instantiate the Trivia Game.
	    HOLD = new Holder(quiz_name, num_questions, num_chances);

	    // Scan in and set up the questions and answers.
	    for (int i = 1; i <= num_questions; i++)
	    {
	      category = trivia_in.nextLine();
	      question = trivia_in.nextLine();
	      num_answers = trivia_in.nextInt();
	      next = trivia_in.nextLine();
	      HOLD = new Holder(category, question, num_answers);
	      for (int j = 1; j <= num_answers; j++)
	      {
	        answer = trivia_in.nextLine();
	        HOLD.save_answer(answer);
	      }
	      HOLD.insertQuestion(HOLD);
	    }
		
	    return WC;
	}
}



and this is my RunGame class

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

public class RunGame {
	public static void main(String[] Args) throws IOException {
		String quiz_name = "", answer;
		int num_questions = 0, num_chances = 0;
		WorkClass WC;
		
		Holder HOLD = new Holder(quiz_name, num_questions, num_chances);
		WC = WorkClass.useTextFile("C:/Users/Dylan.Desktop/Desktop/game.txt");
		
		Scanner user_in = new Scanner(System.in);
		
		System.out.println("Welcome to the " + HOLD.get_quiz_name() + " trivia quiz.");
	    System.out.print("You will have " + HOLD.get_num_chances() + " chances ");
	    System.out.println("to answer " + HOLD.get_curr_num_questions() + " questions.\n");
	    
	    int questNum = 0;
	    while (!HOLD.is_over()) {
	    	do
	    		if (questNum == HOLD.get_curr_num_questions())
	    			questNum = 1;
	    		else
	    			questNum = questNum + 1;
	    	while (HOLD.is_answered(questNum));
	    	System.out.println(HOLD.get_category() + ": " + HOLD.get_question());
	    	answer = user_in.nextLine();
	    	if (HOLD.try_answer(answer)) {
	    		System.out.println("Correct!\n");
	    		HOLD.correct_answer(questNum);
	    	} else {
	    		System.out.println("Incorrect\n");
	    		HOLD.incorrect_answer();
	    	}
	    }
	    
	    System.out.println("\nGame Over");
	    System.out.println("\nResults:");
	    System.out.print("   Chances used: " + (HOLD.get_num_chances() - HOLD.get_remaining_chances()));
	    System.out.println("   Number Correct: " + HOLD.get_num_correct());
	    System.out.println("\nThank you.\n");
	}
}



I am getting the following errors:

Exception in thread "main" java.lang.NullPointerException
at Holder.insertQuestion(Holder.java:78)
at WorkClass.useTextFile(WorkClass.java:35)
at RunGame.main(RunGame.java:10)

So I think I actually didnt fix it but simple got it to the point where java would allow it to compile.

the RuneGame.java:10 error is the same issue, I tried to do a workaround because I needed to initalize HOLD but dont know how to do so.
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7749
  • View blog
  • Posts: 13,106
  • Joined: 19-March 11

Re: Trouble Calling a Method

Posted 26 September 2012 - 08:15 PM

At line 29, you have a problem - you nuke your Holder object and make a new one in its place. If the Holder is the notebook you're writing down your questions in, this is like writing down a question in a notebook, then tossing it in the fire and getting a new notebook to write down the next question, and so forth.

I think you can probably make this a lot easier by putting more of the code into the Holder class. The easiest thing would be to just pass it a file name and let it do all the work. Or you might prefer to read the File into a long String or into an array of lines and pass that reference to the Holder, that would be fine too - if other classes are reading files, you might want to centralize that functionality.
Was This Post Helpful? 0
  • +
  • -

#8 BigD959919  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 91
  • Joined: 26-March 12

Re: Trouble Calling a Method

Posted 26 September 2012 - 08:22 PM

They way it was explained to me, and this could be wrong, but that was okay because they were 2 different sets of arguments for the constructors. Line 29 is the string, string, int constructor and wouldn't interfere with the string int int constructor.

Or am I missing your point entirely?

Also I have put as much of the code into holder as I can. WorkClass is just for reading in the text file and moving the read in contents to the stringlog and such.

This post has been edited by BigD959919: 26 September 2012 - 08:25 PM

Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7749
  • View blog
  • Posts: 13,106
  • Joined: 19-March 11

Re: Trouble Calling a Method

Posted 26 September 2012 - 08:28 PM

They are different constructors, meaning they create an object using different raw materials, but they're still creating an object.

And what's more important, they're assigning that object to a place where you already had one - the old one is now gone, along with anything you wrote into it before you nuked it.

Basically, same as this:

int x = 5;
x = 234;

System.out.println(x);  // where'd my 5 go? it's gone.


or this:

String s = "Hello, world!";
s = "Goodbye, cruel world";

System.out.println(s); // great, now your computer's suicidal


In all of these cases, you're doing the same thing: reassigning a variable to point at something else, which loses the previous value stored there. In the case of an object, if this is the last remaining reference to that object then it's dead, and it'll lie around waiting to be reaped by the Garbage Collector.
Was This Post Helpful? 0
  • +
  • -

#10 BigD959919  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 91
  • Joined: 26-March 12

Re: Trouble Calling a Method

Posted 26 September 2012 - 08:35 PM

Oh okay I see what I am doing wrong there but how would I go about fixing my RuneGame.java:10 error? I tried to do

Holder HOLD = new Holder();


the way my book tells me to start this method identifier but eclipse is spitting a Constructor() is undefined error at me but the WorkClass is just fine.
Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7749
  • View blog
  • Posts: 13,106
  • Joined: 19-March 11

Re: Trouble Calling a Method

Posted 26 September 2012 - 08:49 PM

It's not a problem to construct an object there. The problem is you don't want to overwrite it each time you read a question. You want to store the questions in the existing object.
Was This Post Helpful? 0
  • +
  • -

#12 BigD959919  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 91
  • Joined: 26-March 12

Re: Trouble Calling a Method

Posted 26 September 2012 - 09:00 PM

No I am talking about instantiating HOLD in my main. I understand the issue pertaining to question.
Was This Post Helpful? 0
  • +
  • -

#13 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7749
  • View blog
  • Posts: 13,106
  • Joined: 19-March 11

Re: Trouble Calling a Method

Posted 26 September 2012 - 09:03 PM

Then I don't understand the question. What is your problem with line 10? It looks to me like a legitimate constructor call. Is it not working correctly?
Was This Post Helpful? 0
  • +
  • -

#14 BigD959919  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 91
  • Joined: 26-March 12

Re: Trouble Calling a Method

Posted 26 September 2012 - 09:04 PM

Nope, it is spitting "The constructor Holder() is undefined" at me. Like I said my book tells me to do it this way but eclipse isn't liking it.
Was This Post Helpful? 0
  • +
  • -

#15 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7749
  • View blog
  • Posts: 13,106
  • Joined: 19-March 11

Re: Trouble Calling a Method

Posted 26 September 2012 - 09:08 PM

Wait - are you calling
Holder HOLD = new Holder(quiz_name, num_questions, num_chances);


as you did before, or
Holder HOLD = new Holder();


The latter won't work because you haven't got a no-argument constructor defined. The former should be fine, assuming the class Holder is visible to the class that's calling it.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2