Results 1 to 3 of 3
  1. #1
    Breowleh is offline Member
    Join Date
    May 2012
    Posts
    1
    Rep Power
    0

    Question What's wrong? New to Java

    I just started experimenting with java, and I want to know what's wrong with the code I have here. Any help would be much appreciated. it compiles fin in javac.exe, but when I run it, it doesn't give me the desired results.

    import java.util.Scanner;
    import java.text.DecimalFormat;


    public class Hotel
    {
    public static void main (String []args)
    {
    DecimalFormat formatter = new DecimalFormat("#0.00");
    Scanner keyboard = new Scanner (System.in);
    double floors;
    double rooms;
    double occupiedRooms;
    double totalRate;
    double percentage = 0;

    for(int counter = 1; counter <= percentage ;counter++)
    {
    System.out.print("Please enter the number of floors in the hotel: \n" + counter);
    floors = keyboard.nextDouble();

    System.out.println("Please enter the number of rooms on floor #" + counter);
    rooms = keyboard.nextDouble();

    System.out.println("Please enter the number of occupied rooms on floor #" + counter);
    occupiedRooms = keyboard.nextDouble();

    totalRate = occupiedRooms/rooms;
    percentage = totalRate * 100;


    if (floors < 1)
    {
    System.out.print("You have entered an invalid number of floors.\n");
    }


    else if(rooms< 2)
    {
    System.out.print("You have entered an invalid number of rooms.\n");
    }


    System.out.println("The hotel has a total of" + rooms + ".");
    System.out.println(rooms + "of the rooms are occupied.");
    System.out.println(formatter.format(percentage) + "of the rooms are occupied.");

    }
    }
    }




    /*Write a program that calculates the occupancy rate for a hotel.
    The program should start by asking the user how many floors the hotel has.
    A for loop should then iterate once for each floor. In each iteration of
    the for loop, the program should ask the user for the number of rooms of
    the floor and how many of them are occupied. After all of the iterations
    are complete the program should display how many rooms the hotel has, how
    many of them are occupied, and the percentage of rooms that are occupied.

    It is traditional that many hotels do not have a 13th floor. The for loop in
    this program should skip the entire thirteenth loop iteration.

    Input validation (remember to use a loop never ever an "if"): Do not accept a
    value of less than one for the number of floors. Do not accept a value of less
    than 10 for the number of rooms on a floor. */

  2. #2
    brynpttrsn is offline Member
    Join Date
    Sep 2011
    Posts
    59
    Rep Power
    0

    Default Re: What's wrong? New to Java

    You should start by asking yourself "Which questions need to be asked only once?". Once you figure that out, put those questions outside of the loop. After that you should think about how many times the loop should execute and what variable that is associated with.

    Also, use the code tags when posting source code.
    Last edited by brynpttrsn; 05-07-2012 at 10:39 AM.

  3. #3
    Bushman is offline Member
    Join Date
    Apr 2012
    Location
    Spainish TOwn
    Posts
    25
    Rep Power
    0

    Default Re: What's wrong? New to Java

    Quote Originally Posted by brynpttrsn View Post
    You should start by asking yourself "Which questions need to be asked only once?". Once you figure that out, put those questions outside of the loop. After that you should think about how many times the loop should execute and what variable that is associated with.

    Also, use the code tags when posting source code.
    here's a quick solyution:

    PHP Code:
    import java.util.Scanner;
    import java.text.DecimalFormat;
    
    
    public class Hotel
    {
        static DecimalFormat formatter = new DecimalFormat("#0.00");
        static Scanner keyboard = new Scanner (System.in);
        static double floors = 0;
        static double roomsPrompt = 0, rooms = 0;
        static double occupiedRooms = 0;
        static double totalRate = 0;
        static double percentage = 0;
        static int floorCounter = -1;
            
        public static void main (String []args)
        {
            //this need only be asked once per program cycle....so you don't need to loop it's occurence
            System.out.print("Please enter the number of floors in the hotel: " );
            floors = keyboard.nextDouble();
            
            System.out.println ( ); //print line of space to distinguish prompts
            boolean fuck = true;
            
            if ( !floorValidation ( ) )
                System.out.println ( "invalid floor #" );
            else
                occupancyProcess ( );
        }
        
        public static boolean floorValidation ( )
        {
            return ( floors >= 1 ) && ( floors < 13 );
        }
        
        public static void occupancyProcess ( )
        {
            do
            {
                //update floor counter
                floorCounter ++;
                 
                System.out.print(":---------------------------------------------------------:\n" );
                System.out.print ("Please enter the number of rooms on floor # " + floorCounter + " : "  );
                roomsPrompt = keyboard.nextDouble();
                if(roomsPrompt< 2)
                    System.out.print("You have entered an invalid number of rooms.\n");
                else
                    rooms = roomsPrompt;
                
                System.out.println ( ); //print line of space to distinguish prompts
                
                System.out.print ("Please enter the number of occupied rooms on floor # " + floorCounter + " : " );
                occupiedRooms = keyboard.nextDouble();
                
                System.out.println ( ); //print line of space to distinguish prompts
               
                System.out.print(":---------------------------------------------------------:\n\n" );
            }
            while ( floorCounter < floors ); //limit the user from entering below 1 and above 12
            
            //compute stuff
            totalRate = occupiedRooms/rooms;
            percentage = totalRate * 100;
            //display stuff
            System.out.println("The hotel has a total of " + floors + " floors.");
            System.out.println("The hotel has a total of " + rooms + " rooms.");
            System.out.println( occupiedRooms + " rooms are occupied.");
            System.out.println( "So, " + formatter.format(percentage) + " % of the rooms are occupied.");
        }
    }

Similar Threads

  1. What is wrong in this code of java
    By kank635 in forum New To Java
    Replies: 1
    Last Post: 01-16-2012, 10:41 PM
  2. Wrong Output (Java Program)
    By poupas in forum New To Java
    Replies: 12
    Last Post: 11-28-2010, 05:28 PM
  3. Please look at my java code and tell me what's wrong
    By xcointoss in forum Advanced Java
    Replies: 3
    Last Post: 10-22-2010, 05:15 AM
  4. Java-Classes, what am I doing wrong?
    By Mattedatten in forum New To Java
    Replies: 3
    Last Post: 03-15-2010, 05:54 PM
  5. what wrong with my java code
    By MAXZZXAM in forum New To Java
    Replies: 3
    Last Post: 11-19-2009, 10:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •