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
Anonymous User
30-Apr-2015I 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.