#### Program for Splitting Roommate Bills Problems with Loops and Broken Code

Total Post:159

Points:1117
1321  View(s)
Ratings:
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!

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
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
Rent
Cable
1. Post:397

Points:3117
##### Re: Program for Splitting Roommate Bills Problems with Loops and Broken Code

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.

#### CONTACT INFO

10-B Taskand Marg, Near Patrika Chauraha, Civil Lines, Prayagraj, UP, India-211001.
contact@mindstick.com
91-532-2400505

969-G Edgewater Blvd,Suite
793 Foster City-94404, CA (USA)
+1-650-242-0133