Practice assignment due soon! Please help with infinite loop error

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 2882 Views - Last Post: 27 April 2014 - 08:30 PM Rate Topic: -----

#16 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: Practice assignment due soon! Please help with infinite loop error

Posted 18 April 2014 - 09:59 PM

View Postdarumaqn, on 18 April 2014 - 11:45 PM, said:

The document says decimal formatting is usually not synchronized. So that means I have to format each time I want to print to console right?



Synchronization will not affect you in this case. It's only an issue if you're running multiple threads, which you're not doing.

Basically, just go ahead and format numbers when you print them - when you're on a deadline, don't worry about making it beautiful, just get it working. Follow the examples, and if it looks right, go with it.

(this is why you shouldn't let your deadlines run down to hours, because then you write bad code that you have to maintain later)

And at this point, I think you're starting to treat this as a collaborative exercise. You've asked a question, it's been well answered, and now you should probably start to rely on your own wit and skill to get the rest of the way to the finish line. Good luck!
Was This Post Helpful? 0
  • +
  • -

#17 darumaqn   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 18-April 14

Re: Practice assignment due soon! Please help with infinite loop error

Posted 18 April 2014 - 09:59 PM

That's what I get too. Now I am wondering how to apply both numFormat (for decimals) and moneyFormat (for $) for a single input double. My intuition is telling me it would look messy in code.
Was This Post Helpful? 0
  • +
  • -

#18 infernorthor   User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Practice assignment due soon! Please help with infinite loop error

Posted 18 April 2014 - 10:02 PM

synchronized has to do with multithreading, which you aren't doing.
But if you use 1 format object you have to change each time, or create a new format object for each kind of format.

Also you can do
System.out.printf("$%.2f",amount); Specify format at printing if you want.

Was This Post Helpful? 0
  • +
  • -

#19 darumaqn   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 18-April 14

Re: Practice assignment due soon! Please help with infinite loop error

Posted 18 April 2014 - 10:25 PM

View Postjon.kiparsky, on 18 April 2014 - 09:59 PM, said:

View Postdarumaqn, on 18 April 2014 - 11:45 PM, said:

The document says decimal formatting is usually not synchronized. So that means I have to format each time I want to print to console right?



Synchronization will not affect you in this case. It's only an issue if you're running multiple threads, which you're not doing.

Basically, just go ahead and format numbers when you print them - when you're on a deadline, don't worry about making it beautiful, just get it working. Follow the examples, and if it looks right, go with it.

(this is why you shouldn't let your deadlines run down to hours, because then you write bad code that you have to maintain later)

And at this point, I think you're starting to treat this as a collaborative exercise. You've asked a question, it's been well answered, and now you should probably start to rely on your own wit and skill to get the rest of the way to the finish line. Good luck!


You're right. I'm getting ahead of myself, and I'm glad that I have most parts working with a few exceptions thanks to everyone's help. I'll keep this in mind for next time! Thanks everyone! Keep on rockin'!!
Was This Post Helpful? 0
  • +
  • -

#20 darumaqn   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 18-April 14

Re: Practice assignment due soon! Please help with infinite loop error

Posted 18 April 2014 - 11:35 PM

Hmmm. I feel bad attempting to bring attention back to this topic, but the problem I'm experiencing now, more or less, has to deal with the original question I asked. Here's the work I've put in during the last 1.5 hours thanks to your help:

1. Using
int equipmentCode = Integer.parseInt("4");
, I was able to exit the loop and continue to the next prompt.

2. The next prompt works as described and data validation is in order (e.g. inputting H/h or F/f works and inputting anything else will leave an error).

3. All input doubles are formatted with the dollar sign and two decimal places.

4. Lastly, I was able to reach the end of the program and prompt the user to either end or do another session

My problem now is the data validation for inputting number 1-4 for the first prompt does not happen. I can input anything (number, letter, etc.) and I will always get a receipt for purchasing "Jack Hammer" with its pricing. I think this has to do with the int equipmentCode = Integer.parseInt("4"); code that I used earlier. "Jack Hammer" happens to be the fourth item in the list. In the end, the program looks great on the console, but it's not working as it should. Any tips?

Here's the full code once again:

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Scanner;
public class Rental {

	public static void main(String[] args) {
		
		//constants
		final String EDDIE = "Welcome to Eddie's Equipment Rental!";
		final String EQUIP = "Equipment";
		final String HD = "Half-Day";
		final String FD = "Full-Day";
		final String RC = "Rug Cleaner";
		final String LM = "Lawn Mower";
		final String PS = "Paint Sprayer";
		final String JH = "Jack Hammer";
		final double RC_RATE = 16.00;
		final double LM_RATE = 12.00;
		final double PS_RATE = 20.00;
		final double JH_RATE = 18.00;
		final double DEPOSIT_FEE = 30.00;
		//initialize the number format
		NumberFormat moneyFormat = NumberFormat.getCurrencyInstance();
		NumberFormat numFormat = new DecimalFormat ("#,##0.00");
		//work fields
		String equipment;//for user input
		int equipmentCode = Integer.parseInt("4");//...
		String duration, again;
		char durationCode = ' ';//initialized to blank or space
		String desc = "";//description of equipment selection
		String adj = "";//description of rental duration
		double fee;// special label
		/*
		 * display welcome greeting and show menu of choices using
		 * constants and concatenation. Menu formatted with \n (new line)
		 * and \t (hard tab) to be pleasing to eye. 
		 */
		System.out.println("\t" + EDDIE);
		System.out.println("\n" + EQUIP + "\t\t" + HD + "\t" + FD);
		//1.5*rate constant to correctly output cost of full-day rental
		System.out.println("\n1. " + RC + "\t\t" + moneyFormat.format(RC_RATE) + "\t\t" + moneyFormat.format(1.5*RC_RATE));
		System.out.println("2. " + LM + "\t\t" + moneyFormat.format(LM_RATE) + "\t\t" + moneyFormat.format(1.5*LM_RATE));
		System.out.println("3. " + PS + "\t" + moneyFormat.format(PS_RATE) + "\t\t" + moneyFormat.format(1.5*PS_RATE));
		System.out.println("4. " + JH + "\t\t" + moneyFormat.format(JH_RATE) + "\t\t" + moneyFormat.format(1.5*JH_RATE));
		//create Scanner object for keyboard input
		Scanner sc = new Scanner(System.in);  //default for input is keyboard
		/*
		 * main loop (one trip)
		 */
		do {
			System.out.print("\nSelect an item (1, 2, 3 or 4): ");
			equipment = sc.nextLine(); //grab input line
			/*
			 * while true keep repeating (false, stop)
			 * is entry less than 1 or greater than 4
			 */
			while (equipmentCode < 1 || equipmentCode > 4) {
				System.out.println("Invalid choice! Please input 1, 2, 3 or 4: ");
				equipment = sc.nextLine();//get next
			}//ends while	
			System.out.print("\nSelect duration of (H)alf-day or (F)ull-day rental: ");
			duration = sc.nextLine();
			durationCode = duration.toUpperCase().charAt(0);
			/*
			 *is entry not equal to H and not equal to F
			 */
			while (durationCode != 'H' && durationCode != 'F') {
				System.out.println("Invalid choice! Please input an H or F: ");
				duration = sc.nextLine();  //get next
				durationCode = duration.toUpperCase().charAt(0);
			} //ends while
			/* determine user selection
			 * using nested if written in else if style
			 */
			if (equipmentCode == 1) {
				desc = RC;
				fee = RC_RATE;
			} else if (equipmentCode == 2) {
				desc = LM;
				fee = LM_RATE;
			} else if (equipmentCode == 3) {
				desc = PS;
				fee = PS_RATE;
			} else {//must be 4
				desc = JH;
				fee = JH_RATE;
			} //ends if
			if (durationCode == 'H') {
				adj = "(Half-day rental)";
			} else {//must be F
				fee = 1.5*fee;
				adj = "(Full-day rental)";
			} //ends if
			/* display the info 
			 * escape sequences start with a backslash \n is newline \t hard tab
			 */
			System.out.println("\nHere is your receipt!");
			System.out.print("\n" + desc + "\t" + moneyFormat.format(fee) + " " + adj);
			System.out.print("\nDeposit" + "\t\t" + moneyFormat.format(DEPOSIT_FEE));
			double total = fee + DEPOSIT_FEE;
			System.out.print("\nTotal" + "\t\t" + moneyFormat.format(total));
			System.out.print("\n\nAgain (Y/N): ");
			again = sc.nextLine();
		} while (again.toUpperCase().charAt(0) != 'N');  //any other character keeps going
		System.out.println("\nSession over!");
	} //ends main
} //ends class


Was This Post Helpful? 0
  • +
  • -

#21 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: Practice assignment due soon! Please help with infinite loop error

Posted 18 April 2014 - 11:39 PM

That's because you assign equipmentCode a value here:

int equipmentCode = Integer.parseInt("4");



and you never touch it again. So it has the value 4, which corresponds to a jackhammer.

You need to parse the input you get from the user and assign that value to equipmentCode

This post has been edited by jon.kiparsky: 18 April 2014 - 11:39 PM

Was This Post Helpful? 0
  • +
  • -

#22 DimitriV   User is offline

  • vexing conundrum
  • member icon

Reputation: 587
  • View blog
  • Posts: 2,746
  • Joined: 24-July 11

Re: Practice assignment due soon! Please help with infinite loop error

Posted 18 April 2014 - 11:43 PM

Dude, what happened here?
int equipmentCode = Integer.parseInt("4");//...

It's just as easy to set it to 4 by saying = 4;
And here:
equipment = sc.nextLine(); //grab input line

Here is where you need to be parsing input into equipmentCode…
equipmentCode = Integer.parseInt(equipment);


Was This Post Helpful? 0
  • +
  • -

#23 darumaqn   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 18-April 14

Re: Practice assignment due soon! Please help with infinite loop error

Posted 19 April 2014 - 12:09 AM

View PostDimitriV, on 18 April 2014 - 11:43 PM, said:

Dude, what happened here?
int equipmentCode = Integer.parseInt("4");//...

It's just as easy to set it to 4 by saying = 4;
And here:
equipment = sc.nextLine(); //grab input line

Here is where you need to be parsing input into equipmentCode…
equipmentCode = Integer.parseInt(equipment);



I wish I knew what happened...I'm going to reread parsing to make sure I have a clear understanding of its uses before I muck up the code again. To clarify, I turned in the assignment "as is" but I'm still working on it to figure things out. The comments are leading me in the right direction so I appreciate it!
Was This Post Helpful? 0
  • +
  • -

#24 infernorthor   User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Practice assignment due soon! Please help with infinite loop error

Posted 19 April 2014 - 12:20 AM

wow,

I'm not sure the appropriate terms. But, there are two kinds of code, variable and non-variable.
when you initialize like
int equimentCode = 0; you know the value so it non-variable at this point.

if( equipmentCode < 1 || equipmentCode > 4)
it is assumes variable so it can change during running the program.
sc.nextLine() only gives you a string. Which is variable to the input.
Parsing is to convert strings into other variable types.
equipmentCode = Integer.parseInt(equipment); // Integer is the class and what is turns into
Was This Post Helpful? 0
  • +
  • -

#25 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: Practice assignment due soon! Please help with infinite loop error

Posted 19 April 2014 - 12:20 AM

integer.parseInt takes any String and attempts to convert it into an integer. If the String represents an integer, that integer is returned. If it does not, an exception is thrown.

By "represent an integer" I mean, basically, if it looks like a base-ten integer representation, you're good.

You should use this when you have a String that you want to treat as a number - for example, if you want to compare it to other numbers.

For example, when you get input from a user.
Was This Post Helpful? 0
  • +
  • -

#26 darumaqn   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 18-April 14

Re: Practice assignment due soon! Please help with infinite loop error

Posted 27 April 2014 - 06:47 PM

I just got back my assignment with feedback and everything was good except that I forgot to parse the input in the do statement and only parsed input in the while statement. I overlooked that it needed to be done in both places. Thank you all for your help and explanations!
Was This Post Helpful? 0
  • +
  • -

#27 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: Practice assignment due soon! Please help with infinite loop error

Posted 27 April 2014 - 07:32 PM

Well, good work anyway. Onward and upward!
Was This Post Helpful? 0
  • +
  • -

#28 darumaqn   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 18-April 14

Re: Practice assignment due soon! Please help with infinite loop error

Posted 27 April 2014 - 08:10 PM

View Postjon.kiparsky, on 27 April 2014 - 07:32 PM, said:

Well, good work anyway. Onward and upward!

Thanks Jon! Looking forward to what I can do with Java by the time I'm done with the course! I may have a question coming up regarding a different concept. Should I just make a new topic for that one?
Was This Post Helpful? 0
  • +
  • -

#29 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: Practice assignment due soon! Please help with infinite loop error

Posted 27 April 2014 - 08:20 PM

View Postdarumaqn, on 27 April 2014 - 10:10 PM, said:

Thanks Jon! Looking forward to what I can do with Java by the time I'm done with the course! I may have a question coming up regarding a different concept. Should I just make a new topic for that one?


Yep. One topic per question and one question per topic is a good rule of thumb - helps keep things orderly.
Was This Post Helpful? 0
  • +
  • -

#30 darumaqn   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 18-April 14

Re: Practice assignment due soon! Please help with infinite loop error

Posted 27 April 2014 - 08:30 PM

[/quote]

Yep. One topic per question and one question per topic is a good rule of thumb - helps keep things orderly.
[/quote]
Good to know. Thanks! (:
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2