forum

Home / DeveloperSection / Forums / Program for Splitting Roommate Bills Problems with Loops and Broken Code

Program for Splitting Roommate Bills Problems with Loops and Broken Code

Samuel Fernandes 1908 30-Apr-2015
I have a couple of questions about my current program. The first one is that when I run my for loops that ask questions and store the user input in arrays, it is always printing the question out one last unneeded time. The next problem is that when I get to the point where I am storing the amount of each bill, it doesn't do anything and the program gets hung up. Any help will be appreciated. Code is posted below. Also, any other pointers to make my code better would be appreciated as well. 

Source Code: 

package RoommateBillSplitter;
 
import java.util.*;
 
/**
 *
 * @author Tanner
 */
public class Main {
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //Variable Declarations
        String place;
        String[] roommates = new String[20];
        String[] bills = new String[20];
        int amount[] = new int[20];
        int numRoom = 0;
        int numBills = 0;
        int i = 0;
         
        Scanner input = new Scanner(System.in);
         
        System.out.println("Welcome to Roommate Bill Splitter!\n");
         
        //Get Residence Name from User
        System.out.print("Please Enter Name of Place or Address:  ");
        place = input.nextLine();
         
        //Get # of Roommates at Residence
        System.out.print("How Many Total People Reside at " + place + ":  ");
        numRoom = input.nextInt();
         
        //Get Names of Roommates
        for(i = 0; i <= numRoom; i++){
            roommates[i] = input.nextLine();
            System.out.print("What is Person Number " + (i + 1) + "'s Name:  ");
        } 
         
        for(i = 0; i <= numRoom; i++){
            System.out.println(roommates[i]);
        }
         
        //Get # of Bills Split Between Roommates
        System.out.print("What is the Total Number of Bills to be Split at " + place + ":  ");
        numBills = input.nextInt();
         
        //Get Names of Roommates
        for(i = 0; i <= numBills; i++){
            bills[i] = input.nextLine();
            System.out.print("Please List Bill Number " + (i + 1) + ":  ");
        }
         
        for(i = 0; i <= numBills; i++){
            System.out.println(bills[i]);
        }
          
        //Get Amount of Each Bill
        for(i = 0; i <= numBills; i++){
            amount[i] = input.nextInt();
            System.out.print("What is the Total Amount of the " + (bills[i]) + "Bill:  $");
        }
         
         for(i = 0; i <= numBills; i++){
            System.out.print(bills[i]);
            System.out.print("\t");
            System.out.println(amount[i]);
        }         
    }
     
}


Output: 

run: 
Welcome to Roommate Bill Splitter! 

Please Enter Name of Place or Address: 1212 Main 
How Many Total People Reside at 1212 Main: 3 
What is Person Number 1's Name: John 
What is Person Number 2's Name: Steve 
What is Person Number 3's Name: Chad 
What is Person Number 4's Name: 
John 
Steve 
Chad 
What is the Total Number of Bills to be Split at 1212 Main: 2 
Please List Bill Number 1: Rent 
Please List Bill Number 2: Cable 
Please List Bill Number 3: 
Rent 
Cable 

Updated on 30-Apr-2015

Can you answer this question?


Answer

1 Answers

Liked By