• Post Reply Bookmark Topic Watch Topic
  • New Topic

Program for Splitting Roommate Bills Problems with Loops and Broken Code  RSS feed

 
Tanner Giles
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am a Java beginner, but have previous college coding experience in other languages. I have been struggling with this program that I am trying to do in my spare time in order to get better with my Java skills. 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:




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
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I shall add code tags to your post (always use the tags), and I bet it will look a lot better
 
Tanner Giles
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank for the tip in postings, I'm a newbie to forums...
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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++) ...
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am seeing things. If you go to the contents page for this forum, it says, “Program for Splitting Roommate”
 
Tanner Giles
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am kind of confused on the .length feature. I set those arrays to [20] when I declared them. That was to allow the user to designate who long that array would be. My other problem is that at the end of my output section, my code is stuck and not progressing through the program anymore. I'm just at a stand still at that point. When I change the (i <= n) to (i < n) in my for loops, it will ask for the last time, but will not accept any user out put for the last time. It will just proceed to print the answers to the questions. Thanks again for your help.
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you haven't got the user deciding the lengths of the arrays. You have fixed them at 20.Then you have to populate the array. It is not as serious with int[] arrays because they are implicitly filled with 0s, but arrays of reference types are implicitly filled with nulls and those can be dangerous, so you need to replace them all with real values before you try to use the array.That form of the for loop always visits every element of the array once and never cause any problems or Exceptions or anything …




… except possibly if you pass a negative value and try to create an array with −3 elements!
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you think you are asking for 3 inputs from the keyboard and you are actually running the loop 4 times, the Scanner will block execution of everything in that particular thread on the 4th attempt, until you give it some input.
 
Tanner Giles
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am still getting errors and my for loops are all running either too many times or not accepting user input for the last inquiry before printing out the array.
This is what I have changed in my code...



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:
John
Steve
What is the Total Number of Bills to be Split at 1212 Main: 3
Please List Bill Number 1: Rent
Please List Bill Number 2: Cable
Please List Bill Number 3:
Rent
Cable


It's not accepting user input for the 3rd person or the 3rd bill. Also, after it displays the Bills to be Split, my program gets hung up and I don't know why that is happening. Sorry this is such a pain and I really appreciate your help and input.

 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the things they don't tell you, and I bet you won't find it in your book.

What does your book say Scanner#nextLine does? I bet it will be wrong. You cannot get a sensible result out of nextLine after nextAnythingElse. Have a look at one of my old posts: this one, which gives you an explanation and a simple cure. Also write the prompt "Please enter…" before the myScanner.nextSomething call.
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few minutes ago, I wrote: . . . You cannot get a sensible result out of nextLine after nextAnythingElse. . . .
Actually that is a bit of an exaggeration. It is possible but only if you know what to expect next. It does not usually work if you have prompts.
 
Tanner Giles
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code is getting hung up at a certain point and my bills[i] array is not printing at all. I'm out of ideas...
Also, I know that my code should be in multiple methods or classes. I am just trying to get a working program before I create multiple methods and classes.



Output:

Welcome to Roommate Bill Splitter!

Please Enter Name of Place or Address: 12 Main
How Many Total People Reside at 12 Main: 2
What is Person Number 1's Name: John
What is Person Number 2's Name: Chad


What is the Total Number of Bills to be Split at 12 Main: 3
Please List Bill Number 1: rent //these bills should print out after all are input, but they are not printing. they should also show up before "Bill" on the next question asked for the total amount.
Please List Bill Number 2: elect
Please List Bill Number 3: cable



What is the Total Amount of the Bill: $500 //only asks for the rent amount and program gets hung up...should ask for elect amount and cable amount

 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tanner Giles wrote: . . . I am just trying to get a working program before I create multiple methods and classes. . . .
No, you should be creating classes in order to create a working program.
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you didn't have everything in the same method, you would be able to divide it into small pieces and tes‍t each of them individually.

input.next()? That is no way to sort out the problem with nextLine. You need a nextLine call after the nextInt call and before you actually want to use the next line. You want a prompt after the blank nextLine call and before the real nextLine call. That way you will push the enter key after how many people and before the first name. Then you will actually get the first name in the first place in the array. And calling input.next() will lose you the next token, so your names will be all out of phase.
 
Tanner Giles
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I put my code into different methods and now it's getting hung up sooner than it was when I just had it in one method. I have been trying to figure this out for the last week to no avail. I do not understand your last post about input.next();. I am not sure how to handle this situation and keep getting errors that I don't understand either.



Output:

Welcome to Roommate Bill Splitter!

Please Enter Name of Place or Address: 123 Main
How Many Total People Reside at 123 Main: 2
What is Person Number 1's Name: John
What is Person Number 2's Name: Chad
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at roomBillSplit.Main.bills(Main.java:62)
at roomBillSplit.Main.main(Main.java:19)




I am just a beginner at Java and am having a hell of a time trying to understand what I am doing wrong.
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never never never close a Scanner pointing to System.in. Nor a Formatter pointing to System.out or System.err.
You want a Scanner pointing to System.in? Yes, of course you want one. And when I say one I mean one. Create one such Scanner object and have it as a final static field. That is one of the few instances when you want a static field: there will be one of it, never two or three. And if you initialise that field as soon as you declare it, there will never be no instances.

I am afraid that won't cure your program, but it is a start.
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reason for not closing such a Scanner: you also close System.in and you can never reopen it.
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scanner is a really useful class, but it has some foibles which are not at all well publicised. That is why I asked what it says in your book about what nextLine does.
Apart from what I have already said about Scanners:

I have some suggestions
  • 1: Scanner defaults to using whitespace/multiple whitespace as a delimiter. I suggest at this stage, don't attempt to change it.
  • 2: Whitespace includes spaces tabs and line end characters. (There are others too.)
  • 3: The easiest method to understand is next() which returns a String starting at the first non‑whitespace character after the current position and ends just before the first whitespace following. That is equivalent to one “token” and a word is a kind of token. If I use next() and write “Campbell Ritchie”, it will return the String "Campbell".
  • 4: The nextXXX methods are quite easy to inderstand. If I use nextInt() and the first entry after the current whitespace is “123” then you get 123 returned. If you write something which isn't a number you get an Exception. There are ways of avoiding such Exceptions, but I think trying to explain them now would simply baffle you with science.
  • Look in the Scanner documentation and read about those methods.

    Now, if you want an address, you ask for nextLine and the user enters the address and pushes enter. All well and good. Then you want a number of people, so you use nextInt and the user enters 1487618243 and you spend the rest of your life filling in names. So far, so good.

    But the next time you try nextLine, everything goes wrong. What goes wrong is explained here, which I told you about a week ago. What happens is that nextLine actually reads from the current position to the end of the line, and will return an empty line and thereafter all the lines you enter are one position later than you think they are and it is only a matter of time before you try to read “Gas bill” with the nextInt() method and you can't understand why it has gone wrong. What you need is something like this:-Since this appears to be assigned work, I don't think I have given you as many hints as I am allowed.

    Go back to your code and put lots of print statements in it. Or better still, calls to a method which tells you what you entered.That method will give you immediate feedback about what the Scanner thinks it has read, so you can see whether it is different from what you thought the Scanner has read. That will allow you to test the code as it goes. When you have finished testing, delete the test calls or better prepend // to them, which will turn them into comments and cause them to vanish from the real code. Then you will have evidence of the work you put into testing the program. The two Unicode escapes 201c and 201d look like this: “ ”: posh quotes.

    [edit]Strike out “don't”. I ought to go to sleep now.[/edit]
     
    Campbell Ritchie
    Marshal
    Posts: 56581
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Earlier, I wrote: . . . thereafter all the lines you enter are one position later than you think they are . . .
    And input.next() calls like that in line 71 are guaranteed to put things even worse out of phase. Remove all such calls to input.next() which are not to the right of a = sign.
     
    Campbell Ritchie
    Marshal
    Posts: 56581
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I wrote: . . . . . .
    And this is what I should have written:-I forgot to include a prompt to the user.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!