9 Replies - 655 Views - Last Post: 29 April 2008 - 08:40 AM Rate Topic: -----

#1 mogstronaut_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 18-April 08

Getting a 'void' error

Posted 28 April 2008 - 02:10 AM

Hey guys,
I've had a maaaassive long day practicing the ol' Java. I've been fine up until now (which is unusual for me XD).
I've ran into this problem while trying to compile this part of my code; 'void' type not allowed here.
Here's the code:
public void beginGame()
	{
		System.out.println("===================================================================\n\n"
		+ "Welcome Mr Connz.\nIt is Day #" + politician.getCurrentDay() + "\nSo far this week:\n" + politician.getRumour());
		System.out.println("Your shredder is currently " + politician.getShredderVolume() + "% full");
		System.out.println(shredderCheck());
	}
	   
	public void shredderCheck()
	{				 
		 if (politician.hasDocument())
		 {
		 	System.out.println("The original document is still in your possession.");
		 }
		 else
		 {
		 	System.out.println("Your political instinct tells you that you might be in trouble...");
		 }
	}


I figure you probably don't need the rest of the code [plus there's lots of it] but if you do, I'll happily provide.
It's probably something really simple that I just can't see..
Anyway, off to sleep..
Thanks!

err.. i forgot to put where i was getting my error. that's how tired i am. it's "System.out.println(shredderCheck());" that's causing the trouble.

This post has been edited by mogstronaut_: 28 April 2008 - 02:11 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Getting a 'void' error

#2 Ellie  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 8
  • View blog
  • Posts: 533
  • Joined: 17-January 07

Re: Getting a 'void' error

Posted 28 April 2008 - 02:17 AM

Hi,

Your shredderCheck() method doesn't return anything, so you can't use it like that. Since you already wrote the printing bit inside the method, you just need to call it, and it'll print.

System.out.println("Your shredder is currently " + politician.getShredderVolume() + "% full");
shredderCheck();


Was This Post Helpful? 0
  • +
  • -

#3 mogstronaut_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 18-April 08

Re: Getting a 'void' error

Posted 28 April 2008 - 03:24 AM

Thanks for that, I can't believe I missed that. I'm getting pretty close to finishing now, but I have just come across another error.
My code is:
	public void startLoop()
	{
		String loop;		// Holds the user's input to test if beginGame needs to be called
		
		System.out.print("Would you like to play (y)es or (n)o? ");
		loop = sc.nextLine();
		
		if(loop.equalsIgnoreCase("y"))
		{
			beginGame();		// If the user has input the character 'y', the beginGame method is called. If not, the program ends.
		}
	}


   	public void getChoices()
   	{	
   		int userChoice;
   		System.out.println("\nWhat would you like to do:\n\t1. make no comment\n\t2. deny in Parliament that this memorandum ever existed" 
		+ "\n\t3. shred the document or\n\t4. resign");
		userChoice = sc.nextInt();
		
		if(userChoice == 1)
		{
			System.out.println("You attempt to tough it out.");
		}
		if(userChoice == 2)
		{
			System.out.println("Well, you've bluffed them so far. Or have you?");
		}
		if(userChoice == 3)
		{	
			if (politician.hasDocument())
			{
			System.out.println("Document successfully shredded.");
			}
			else
			{
				System.out.println("Sorry but you can't destroy what you do not have!");
			}
		}
		if(userChoice == 4)
		{
			System.out.println("===================================================================\n\n\t\tGAME OVER\n"
			+ "Oh well, I suppose you only let yourself down really...");
			startLoop();
		}
   	}


It compiles fine, but I get a run error. All the options work fine except for the fourth. When the user inputs '4' instead of fully inserting the startLoop() method, it only prints 'Would you like to play (y)es or (n)o?' and does not allow the user to input anything.

What causing this?
Thanks again.
Was This Post Helpful? 0
  • +
  • -

#4 Ellie  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 8
  • View blog
  • Posts: 533
  • Joined: 17-January 07

Re: Getting a 'void' error

Posted 28 April 2008 - 03:42 AM

Hi,

I think the problem must be inside the beginGame() method, which I don't see in your code, because I made a programme out of your sections here, and it reaches that part ok. Examine your beginGame() method, or post it here.
Was This Post Helpful? 0
  • +
  • -

#5 mogstronaut_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 18-April 08

Re: Getting a 'void' error

Posted 28 April 2008 - 03:45 AM

Oh really?
The code is
	public void beginGame()
	{
		System.out.println("===================================================================\n\n"
		+ "Welcome Mr Connz.\nIt is Day #" + politician.getCurrentDay() + "\nSo far this week:\n" + politician.getRumour());
		System.out.println(politician.getEvents());
		System.out.println("Your shredder is currently " + politician.getShredderVolume() + "% full");
		shredderCheck();
		getChoices();
	}
and I don't see anything there that could be affecting getChoices

=/
Was This Post Helpful? 0
  • +
  • -

#6 Ellie  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 8
  • View blog
  • Posts: 533
  • Joined: 17-January 07

Re: Getting a 'void' error

Posted 28 April 2008 - 03:55 AM

Hmm, I am able to keep reisgning (option 4) and looping.

Just incase I have done something without realising what it is, here's my little programme (note that I've just removed a lot of stuff about the politicians).

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.*;

public class Choices {

public Choices() {

BufferedReader in = 
			new BufferedReader(new InputStreamReader(System.in));
getChoices(in);	
}
public void getChoices(BufferedReader sc)
	   {	
		   int userChoice;
		   String s = "-1";
		   System.out.println("\nWhat would you like to do:\n\t1. make no comment\n\t2. deny in Parliament that this memorandum ever existed"
		+ "\n\t3. shred the document or\n\t4. resign");
		try {
		s = sc.readLine();
		} catch (IOException ie) {System.out.println(ie.getMessage());};
		//userChoice = sc.nextInt();
		userChoice = Integer.parseInt(s);
		
		if(userChoice == 1)
		{
			System.out.println("You attempt to tough it out.");
		}
		if(userChoice == 2)
		{
			System.out.println("Well, you've bluffed them so far. Or have you?");
		}
		if(userChoice == 3)
		{	
			int i = 0;
			//if (politician.hasDocument())
			if (i == 0)
			{
			System.out.println("Document successfully shredded.");
			}
			else
			{
				System.out.println("Sorry but you can't destroy what you do not have!");
			}
		}
		if(userChoice == 4)
		{
			System.out.println("===================================================================\n\n\t\tGAME OVER\n"
			+ "Oh well, I suppose you only let yourself down really...");
			startLoop(sc);
		}
	   }


public void startLoop(BufferedReader sc)
	{
		String loop = "";		// Holds the user's input to test if beginGame needs to be called
		
		System.out.print("Would you like to play (y)es or (n)o? ");
		try {
		loop = sc.readLine();
		} catch (IOException ie) {System.out.println(ie.getMessage());}
		
		if(loop.equalsIgnoreCase("y"))
		{
			beginGame(sc);		// If the user has input the character 'y', the beginGame method is called. If not, the program ends.
			/*System.out.println("Game begun");
			String input = "";
			try {
			input = sc.readLine();	
			} catch (IOException ie) {
			System.out.println(ie.getMessage());}	
			System.out.println("Input: " + input);*/
			
		}
		
	}
		public void beginGame(BufferedReader sc)
	{
		System.out.println("===================================================================\n\n Welcome Mr Connz.\nIt is Day #\nSo far this week:\n");
		//System.out.println(politician.getEvents());
		System.out.println("Your shredder is currently " + "% full");
		//shredderCheck();
		getChoices(sc);
	}
	
	public static void main (String args[])
	{
		Choices app = new Choices();	
	}
	
}

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#7 mogstronaut_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 18-April 08

Re: Getting a 'void' error

Posted 28 April 2008 - 04:11 AM

Posted Image
Hmm! This is really quite strange.
Thankyou for all your help. Hopefully someone could point out the problem. Although I don't even see how this is possible.
Was This Post Helpful? 0
  • +
  • -

#8 Ellie  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 8
  • View blog
  • Posts: 533
  • Joined: 17-January 07

Re: Getting a 'void' error

Posted 28 April 2008 - 04:21 AM

Wow, the answer must be somewhere in the differences between yours and my code. If you run my programme, does it work ok with the option 4? At least that gives you somewhere to work from.

If you want to email me your project code to ellie[at]bartender.net I'll take a look, but it might not be right away, I've got an exam tomorrow... :crazy:

This post has been edited by Ellie: 28 April 2008 - 04:22 AM

Was This Post Helpful? 0
  • +
  • -

#9 mogstronaut_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 18-April 08

Re: Getting a 'void' error

Posted 28 April 2008 - 03:17 PM

Okay, I just woke up and tried to compile your code and it works fine. I haven't started to read about BufferedReader andInputStreamReader yet, so I don't really know what they do.
It's very strange we're getting different output, though.
Was This Post Helpful? 0
  • +
  • -

#10 Ellie  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 8
  • View blog
  • Posts: 533
  • Joined: 17-January 07

Re: Getting a 'void' error

Posted 29 April 2008 - 08:40 AM

Haha yeah, the InputStream business was because I was testing it on my work computer which has a very old version of Java on it and so doesn't include the Scanner package.

I've emailed you a working version of your code now.

Hope it works for you. :^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1