---
title: "Program for Splitting Roommate Bills Problems with Loops and Broken Code"  
description: "Program for Splitting Roommate Bills Problems with Loops and Broken Code"  
author: "Samuel Fernandes"  
published: 2015-04-30  
updated: 2015-04-30  
canonical: https://www.mindstick.com/forum/23162/program-for-splitting-roommate-bills-problems-with-loops-and-broken-code  
category: "java"  
tags: ["java", "for loop"]  
reading_time: 5 minutes  

---

# Program for Splitting Roommate Bills Problems with Loops and Broken Code

I have a couple of [questions](https://www.mindstick.com/blog/124895/hp-hpe0-s54-cheat-sheet-exam-questions-bank-for-guaranteed-success) about my [current program](https://answers.mindstick.com/qa/94783/how-can-i-let-the-current-program-finish-in-android-studio). The first one is that when I run my for loops that ask questions and store the [user input](https://www.mindstick.com/forum/159624/setting-an-enum-from-user-input) in [arrays](https://www.mindstick.com/articles/11955/arrays-and-its-limitations), it is always [printing](https://www.mindstick.com/blog/63804/5-ways-you-can-use-printing-in-your-marketing-strategy) the [question](https://www.mindstick.com/blog/23175/how-to-solve-neet-question-paper-in-less-time) out one last unneeded time. The next [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) 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](https://www.mindstick.com/articles/1811/objective-c-pointers) to make my code better would be appreciated as well. \
**[Source Code](https://www.mindstick.com/forum/33476/pls-send-me-source-code-for-changing-passward-in-asp-dot-net-i-tried-so-much-from-google-but-its-not-workining):** \

```
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

## Replies

### Reply by Anonymous User

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.


---

Original Source: https://www.mindstick.com/forum/23162/program-for-splitting-roommate-bills-problems-with-loops-and-broken-code

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
