Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 04-23-2006, 02:11 AM   #1 (permalink)
 
Junior Techie

Join Date: Oct 2005

Posts: 72

chooka

Exclamation Java Problem Please Help

Hi there all, im having problems trying to get a loop to jump out when it has to. Heres part of the program

if (age < 12)
outFile.println("No one " + age + " years of age can hire a DVD");
dvdType = inFile.next().charAt(0);
if (age > 12 && age <15)
outFile.println("Borrowers " + age + " years of age can hire only \"G\" DVDs ");
dvdType = inFile.next().charAt(0);

This is in a while loop. I want it to exit if the age is less than 12 and go onto the next line of the infile.
If someone can help that would be good.
Cheers
__________________
Pentium Core 2 Quad Q6600 2.4Ghz
4Ghz 1066 RAM
2x 500G SATA II HDD
GIGABYTE GA-P35-DS4
GeForce 8800GTS 320MG
Themaltake Armor

_______________________

Diploma in IT Computer Networking
Bachelor of Computer Science
chooka is offline  
Old 04-23-2006, 02:59 AM   #2 (permalink)
 
Monster Techie

Join Date: Jul 2003

Posts: 1,179

Emily is on a distinguished road

Send a message via AIM to Emily
Default

What's the condition in the while loop? You could either set the condition to false, or you could use a break statement in the if statement.
__________________
<a href=\"http://www.upstark.com\">www.upstark.com</a>
Emily is offline  
Old 04-23-2006, 03:02 AM   #3 (permalink)
 
Junior Techie

Join Date: Oct 2005

Posts: 72

chooka

Default

hi, i want the program to break if the customers age is less than 12. But i want it to display what was read from the infile, and not calculate it.
Could u please give me some examples??
__________________
Pentium Core 2 Quad Q6600 2.4Ghz
4Ghz 1066 RAM
2x 500G SATA II HDD
GIGABYTE GA-P35-DS4
GeForce 8800GTS 320MG
Themaltake Armor

_______________________

Diploma in IT Computer Networking
Bachelor of Computer Science
chooka is offline  
Old 04-23-2006, 03:07 AM   #4 (permalink)
 
Monster Techie

Join Date: Jul 2003

Posts: 1,179

Emily is on a distinguished road

Send a message via AIM to Emily
Default

I'm not really sure what you mean... could you post a larger excerpt of the code, like the whole loop?
__________________
<a href=\"http://www.upstark.com\">www.upstark.com</a>
Emily is offline  
Old 04-23-2006, 03:11 AM   #5 (permalink)
 
Junior Techie

Join Date: Oct 2005

Posts: 72

chooka

Default

This is the whole loop. I want it to jump back up to the start of the loop. Thats where i put the "dvdType = inFile.next().charAt(0);" statement in. This reads the first part of the infile. Let me know if its all wrong..


while (dvdType != '*' )
{
duration = inFile.nextInt();
age = inFile.nextInt();
weeksHired = inFile.nextInt();
audience = inFile.next();

//Calculate weekly hire costs
if (duration == 1)
weeklyHireCost = 5;
if (duration == 2)
weeklyHireCost = 8;
if (duration !=1 && duration !=2)
weeklyHireCost = 10;

//Output values entered
outFile.println("------------------------------------------------------------------------------");
outFile.println("Enter classification [G, M or R, * ends]:> " + dvdType );
outFile.println("Enter hours of duration:>" + duration);
outFile.println("Enter age:>" + age);
outFile.println("Enter no. of weeks hired:>" + weeksHired);
outFile.println("Enter intended audience [

rivate or <M>***:>" + audience);

//Start of switch statement
switch (dvdType)
{
case 'R' : outFile.println("*** $100 surcharge for \"R\" rated DVDs ***");
numOfR++;
mainTotal = (weeklyHireCost * weeksHired) + ratingSurcharge;
hoursViewed = (hoursViewed + duration);
break;
case 'M' : mainTotal = (weeklyHireCost * weeksHired);
numOfM++;
hoursViewed = (hoursViewed + duration);
break;
case 'G' : mainTotal = (weeklyHireCost * weeksHired);
numOfG++;
hoursViewed = (hoursViewed + duration);
break;
}
//Distinguish between Mass and private audiences`
if (audience.equals("M"))
{
outFile.println("*** Mass audience viewing incurs double the total cost ***");
mainTotal = mainTotal * 2;
}
// Determine age barrier
if (age < 12)
{
outFile.println("*** No one " + age + " years of age can hire a DVD ***");
dvdType = inFile.next().charAt(0);
}
if (age > 12 && age <15)
{
outFile.println("*** Borrowers " + age + " years of age can hire only \"G\" DVDs ***");
dvdType = inFile.next().charAt(0);
}

//Calculate discount for Pensioners
if ((age >64) && (dvdType!=('R')))
{
mainTotal = (mainTotal * .95);
outFile.println("*** 5% senior citizens discount applied ***");
}
//Output Costs
outFile.println("Cost for a " + age + " yo hiring an " + dvdType + " rated, " + duration + " hr DVD for " + weeksHired + " wks,"
+ " shown to a " + audience + " is $" + mainTotal);
outFile.println();
//Calculate Total Amount
totalAmount = (totalAmount + mainTotal);
dvdType = inFile.next().charAt(0);
}
__________________
Pentium Core 2 Quad Q6600 2.4Ghz
4Ghz 1066 RAM
2x 500G SATA II HDD
GIGABYTE GA-P35-DS4
GeForce 8800GTS 320MG
Themaltake Armor

_______________________

Diploma in IT Computer Networking
Bachelor of Computer Science
chooka is offline  
Old 04-23-2006, 03:17 AM   #6 (permalink)
 
Monster Techie

Join Date: Jul 2003

Posts: 1,179

Emily is on a distinguished road

Send a message via AIM to Emily
Default

Ok, I think I see what you're trying to do, and as I see it there's a couple ways you could do it. I think the easiest would be to enclose everything after the if (age < 12) statement with an else statement, like so:
Quote:
if (age < 12)
{
outFile.println("*** No one " + age + " years of age can hire a DVD ***");
dvdType = inFile.next().charAt(0);
} else {
And then add a closing bracket right before the bracket that closes the while loop, at the very end.

I think that should work... let me know if it doesn't.
__________________
<a href=\"http://www.upstark.com\">www.upstark.com</a>
Emily is offline  
Old 04-23-2006, 03:36 AM   #7 (permalink)
 
Junior Techie

Join Date: Oct 2005

Posts: 72

chooka

Default

Nah that didnt work, all i want the program to do is jump back to the top of the loop if the customer is less that 12 years old. When i start the loop back up i use "dvdType = inFile.next().charAt(0);". That starts the loop again. When i put more than 1 of these in the loop it generates this error.

> Executing: C:\Program Files\ConTEXT\ConExec.exe "C:\Program Files\Java\jdk1.5.0_03\bin\java.exe" number3

Exception in thread "main" java.lang.IllegalStateException: Scanner closed
at java.util.Scanner.ensureOpen(Scanner.java:1025)
at java.util.Scanner.next(Scanner.java:1411)
at java.util.Scanner.nextInt(Scanner.java:2040)
at java.util.Scanner.nextInt(Scanner.java:2000)
at number3.main(number3.java:39)
> Execution finished.
__________________
Pentium Core 2 Quad Q6600 2.4Ghz
4Ghz 1066 RAM
2x 500G SATA II HDD
GIGABYTE GA-P35-DS4
GeForce 8800GTS 320MG
Themaltake Armor

_______________________

Diploma in IT Computer Networking
Bachelor of Computer Science
chooka is offline  
Old 04-23-2006, 03:46 AM   #8 (permalink)
 
Monster Techie

Join Date: Jul 2003

Posts: 1,179

Emily is on a distinguished road

Send a message via AIM to Emily
Default

So if age < 12, it should basically read a value for dvdType and then skip the Output Costs code? If that's what you want it to do, what I gave you should work... are you sure you've got all the brackets in the right place and everything?
__________________
<a href=\"http://www.upstark.com\">www.upstark.com</a>
Emily is offline  
Old 04-23-2006, 03:58 AM   #9 (permalink)
 
Junior Techie

Join Date: Oct 2005

Posts: 72

chooka

Default

ok that part worked.. Is it possible to have the other statement in also
if (age > 12 && age <15)
{
outFile.println("*** Borrowers " + age + " years of age can hire only \"G\" DVDs ***");
dvdType = inFile.next().charAt(0);
}
and skip the output costs
??
__________________
Pentium Core 2 Quad Q6600 2.4Ghz
4Ghz 1066 RAM
2x 500G SATA II HDD
GIGABYTE GA-P35-DS4
GeForce 8800GTS 320MG
Themaltake Armor

_______________________

Diploma in IT Computer Networking
Bachelor of Computer Science
chooka is offline  
Old 04-23-2006, 12:06 PM   #10 (permalink)
 
Monster Techie

Join Date: Jul 2003

Posts: 1,179

Emily is on a distinguished road

Send a message via AIM to Emily
Default

Yeah, do it like this:
Quote:
// Determine age barrier
if (age < 12)
{
outFile.println("*** No one " + age + " years of age can hire a DVD ***");
dvdType = inFile.next().charAt(0);
} else if (age > 12 && age <15)
{
outFile.println("*** Borrowers " + age + " years of age can hire only \"G\" DVDs ***");
dvdType = inFile.next().charAt(0);
} else
{
//Calculate discount for Pensioners
if ((age >64) && (dvdType!=('R')))
{
mainTotal = (mainTotal * .95);
outFile.println("*** 5% senior citizens discount applied ***");
}
//Output Costs
outFile.println("Cost for a " + age + " yo hiring an " + dvdType + " rated, " + duration + " hr DVD for " + weeksHired + " wks,"
+ " shown to a " + audience + " is $" + mainTotal);
outFile.println();
//Calculate Total Amount
totalAmount = (totalAmount + mainTotal);
dvdType = inFile.next().charAt(0);
}
}

__________________
<a href=\"http://www.upstark.com\">www.upstark.com</a>
Emily is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On