Home > DeveloperSection > Forums > Program for Splitting Roommate Bills Problems with Loops and Broken Code
Samuel Fernandes
Samuel Fernandes

Total Post:154

Points:1082
Posted on    April-30-2015 12:26 AM

 Java Java  For Loop 
Ratings:


 1 Reply(s)
 472  View(s)
Rate this:
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 


Mayank Tripathi
Mayank Tripathi

Total Post:397

Points:3117
Supported
Posted on    April-30-2015 7:26 AM

Let's have a look at your code. The first thing I can see is that you have got far too much in the one method; that should be split into several methods, preferably in several classes. I think you should have a Bill class and a Roommate class.

I prefer to avoid the <= and >= operators. They are not so much wrong as prone to confusion. Remember that when you iterate an array with a for loop, you write it like this
for (int i = 0; i < myArray.length; i++) ...
though you may have to alter that because special circumstances may mean you iterate all elements but 1 (or something like that). Same when you don't have arrays. Start by writing your for loops like this, which is virtually the same as before, and then consider whether you need to change from the common form later.
for (int i = 0; i < n; i++) ...
Also instantiate the arrays of bills and arrays or people after you know how many there are. If there are 3 bills use a 3‑element array.

Don't want to miss updates? Please click the below button!

Follow MindStick