6 Replies - 1252 Views - Last Post: 17 November 2010 - 11:45 AM Rate Topic: -----

#1 usaims  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 02-November 10

'void' type not allowed here

Posted 17 November 2010 - 08:21 AM

Hi,

In my driver tester program I'm trying to get the toInfo method to print, but I'm getting
'void' type not allowed here when trying to compile the HotelTester program, please offer help, TIA.
public class HotelTester
{
   public static void main(String[] args)
   {
	
	Room room = new Room("Smith", 2, 2, 29.99,10);
	
	System.out.println(room.getNumBeds());
	System.out.println(room.toInfo());
	
	}
	
}

####

This is my class:

/**
   Attributes of a hotel room 
*/
public class Room
{  
   private String guest;
   private int number;
   private int numBeds;
   private double rate;
   private int numOfDays;

   /**
      Constructs a guest room..
      @param guest Last Name of guest.
      @param number
   */
   public Room(String guestName, int roomNumber, int howManyBeds, double dailyRate, int howManyNights )
   {   
      guest = guestName;
      number = roomNumber;
      numBeds = howManyBeds;
      rate   = dailyRate;
      numOfDays = howManyNights; 
   }

   /**
      Gets the last name of the guest..
      @return the last name
   */
   public String getGuest()
   {   
      return guest;
   }

public int getNumber()
   {  
      return number;
   }

public int getNumBeds()
   {
      return numBeds;
   }

public int getNumOfDays()
   {
      return numOfDays;
   }

   /**
      Input the guest's name.
      @param guest is the name of the guest.
   */
   public void setGuest(String guestName)
   {  
   guest = guestName;
   }


   /**
      Inputs the room number.
      @param number is the room number..
   */
   public void setNumber(int roomNumber)
   { 
   number = roomNumber;
   }
     
   /**
      Inputs the number of bed.
      @param numBeds is the number of beds..
   */
   
   public void NumBeds(int howManyBeds)
   {
   numBeds = howManyBeds;
   }

   /**
      Inputs the rate.
      @param rate is the daily rate.
   */
   
   public void setRate(double dailyRate)
   {
   rate = dailyRate;
   }
  
/**
      Inputs the rate.
      @param rate is the daily rate.
   */
  
   public void setnumOfDays(int howManyNights )
   {
    numOfDays = howManyNights;
   }
	
	public void toInfo ()
{

System.out.println(getGuest());

		
 }
}


This post has been edited by macosxnerd101: 17 November 2010 - 08:27 AM
Reason for edit:: Please use code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: 'void' type not allowed here

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10693
  • View blog
  • Posts: 39,778
  • Joined: 27-December 08

Re: 'void' type not allowed here

Posted 17 November 2010 - 08:29 AM

Moved to Java help. Please reserve the Java Programmers Forum for topical, non-help related questions.

Can you post the exact error message, including the line number.
Was This Post Helpful? 0
  • +
  • -

#3 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 120
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: 'void' type not allowed here

Posted 17 November 2010 - 08:45 AM

View Postusaims, on 17 November 2010 - 02:21 PM, said:

public class HotelTester
{
   public static void main(String[] args)
   {
	
	Room room = new Room("Smith", 2, 2, 29.99,10);
	
	System.out.println(room.getNumBeds());
	System.out.println(room.toInfo());
	
	}
	
}



	public void toInfo ()
{

System.out.println(getGuest());

		
 }
}




You define toInfo as a method returning void, but you pass it as a parameter of System.out.println in your main as if it was returning a value.

Either have toInfo return an object (String?) and print, or just call room.toInfo(); as it already does the printing.
Was This Post Helpful? 0
  • +
  • -

#4 usaims  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 02-November 10

Re: 'void' type not allowed here

Posted 17 November 2010 - 10:39 AM

Thanks.

The whole idea of the toInfo method is to return the guest's name, number of beds, number of days staying at the hotel, etc in a pretty and neat format like:

Guest's name = Smith
Number of beds = 2
Number of Days = 10
Daily Rate = 29.99

So from the main, I want to go System.out.println(room.toInfo());

####

This worked so far, but I can only have one return statement in a method:
public static void main(String[] args)
   {
	
	Room room = new Room("Smith", 2, 2, 29.99,10);
	
	
	System.out.println("Guest's name:" + " " + room.toInfo());
	
	}
	
}

public String toInfo ()
{

return guest;

		
 }

Thanks in advance.



Edited by macosxnerd101: Please use code tags. Like so: :code:.
Was This Post Helpful? 0
  • +
  • -

#5 Sh4d0ws  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 40
  • Joined: 23-July 10

Re: 'void' type not allowed here

Posted 17 November 2010 - 10:50 AM

You could change it to return an object, and then just print the different parts of the object. Or just make it return an array of strings if you felt like being lazy.

It might be a consideration for you to put it into separate accessor methods? for example...

public String returnName(){
   return name; //assuming name is the string variable that holds name in the Room class.
}


Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2791
  • View blog
  • Posts: 11,821
  • Joined: 20-September 08

Re: 'void' type not allowed here

Posted 17 November 2010 - 11:03 AM

To make Room comply with standard Java practice, give it a toString() method

For some reason your policy is to allow one guest, but potentially several beds - something wrong there ;)

This post has been edited by g00se: 17 November 2010 - 11:04 AM

Was This Post Helpful? 0
  • +
  • -

#7 usaims  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 02-November 10

Re: 'void' type not allowed here

Posted 17 November 2010 - 11:45 AM

Thanks, the toString method did the trick.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1