Results 1 to 15 of 15
Thread: Question With a loop
- 02-08-2012, 04:47 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 19
- Rep Power
- 0
Question With a loop
Im currently working on my homework and for some reason the second time the loop goes around it skips my first question "Enter the name of the company"? Can anyone tell my why, It works fine the first time around.
for(int i = 1; i <= 2; i++){
System.out.print("Enter the name of company " + "number " + i + ": ");
String companyName = keyboard.nextLine();
outputFile.println(companyName);
System.out.print("Enter the rate for Bodily Injury Liability of company " + "number " + i + ": ");
double rateBodily = keyboard.nextDouble();
outputFile.println(rateBodily);
System.out.print("Enter the rate for Property Damage Liability of company " + "number " + i + ": ");
double rateProperty = keyboard.nextDouble();
outputFile.println(rateProperty);
System.out.print("Enter the rate for Uninsured Drivers Coverage of company " + "number " + i + ": ");
double rateUninsured = keyboard.nextDouble();
outputFile.println(rateUninsured);
}
Mike
- 02-08-2012, 05:00 PM #2
Re: Question With a loop
If you want help, you'll have to provide an SSCCE that demonstrates exactly what's going on. Don't forget the code tags. Have you stepped through this with a debugger or simply added some print statements to get a better idea of what's going on?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 02-08-2012, 05:22 PM #3
Member
- Join Date
- Feb 2012
- Posts
- 19
- Rep Power
- 0
Re: Question With a loop
Trying to figure out the best way to post my code is there a tool i can use or do I just copy and paste?
- 02-08-2012, 05:31 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Re: Question With a loop
[code] code goes here [/code].
What does the above display when you run it?
What class is "keyboard"?
If it is a Scanner then I suspect it'll be to do with how it reads stuff in.
- 02-08-2012, 05:33 PM #5
Member
- Join Date
- Feb 2012
- Posts
- 6
- Rep Power
- 0
Re: Question With a loop
Hi Mike,
String companyName = keyboard.next();
instead of
String companyName = keyboard.nextLine();
Jokobe
- 02-08-2012, 05:34 PM #6
Member
- Join Date
- Feb 2012
- Posts
- 19
- Rep Power
- 0
Re: Question With a loop
Anyways not sure if this is the best way to format this but here is my program. Pretty much what im trying to do is have the user enter 4 different company car insurances. They need Name, and 3 different rates for that insurance. Later I will need to add each rate up for each insurance and find the lowest deal. Now I can easily do this the long way by making variable names for each company name, and their rates but I was trying to use less code and make a for loop. Now when I run the program it will enter data for the first time around, but once i get to the second it will skip the entering the company name and go on to the first rate. Let me know if im not making sense lol, been trying to figure this out for awhile.
[code]
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class AutoInsurence {
/**
* @param args
*/
public static void main(String[] args) throws IOException {
// String autoCompanyDay;
// String autoCompanyCourse;
// String autoCompanyEngle;
// String autoCompanyHuber;
// double rateBodilyDay;
// double rateBodilyCourse;
// double rateBodilyEngle;
// double rateBodilyHuber;
// double ratePropertyDay;
// double ratePropertyCourse;
// double ratePropertyEngle;
// double ratePropertyHuber;
// double rateUninsuredDay;
// double rateUninsuredCourse;
// double rateUninsuredEngle;
// double rateUninsuredHuber;
// double rateProperty;
// double rateUnisured;
int numCompanies;
Scanner keyboard = new Scanner(System.in);
// System.out.print("How many Auto Companies do you want to add: ");
// numCompanies = keyboard.nextInt();
// keyboard.nextLine();
System.out.print("Enter the filename: ");
String filename = keyboard.nextLine();
PrintWriter outputFile = new PrintWriter(filename);
for (int i = 1; i <= 2; i++) {
System.out.print("Enter the name of company " + "number " + i
+ ": ");
String companyName = keyboard.nextLine();
outputFile.println(companyName);
System.out
.print("Enter the rate for Bodily Injury Liability of company "
+ "number " + i + ": ");
double rateBodily = keyboard.nextDouble();
outputFile.println(rateBodily);
System.out
.print("Enter the rate for Property Damage Liability of company "
+ "number " + i + ": ");
double rateProperty = keyboard.nextDouble();
outputFile.println(rateProperty);
System.out
.print("Enter the rate for Uninsured Drivers Coverage of company "
+ "number " + i + ": ");
double rateUninsured = keyboard.nextDouble();
outputFile.println(rateUninsured);
}
outputFile.close();
File myFile = new File(filename);
Scanner inputFiles = new Scanner(myFile);
String first;
first = inputFiles.nextLine();
System.out.println(first);
while (inputFiles.hasNext()) {
double balls = inputFiles.nextDouble();
System.out.println(balls);
}
inputFiles.close();
}
}
[\code]
- 02-08-2012, 05:40 PM #7
Member
- Join Date
- Feb 2012
- Posts
- 19
- Rep Power
- 0
Re: Question With a loop
Jokobe your a life saver. Okay now that ive got that settled I have sucessfully been able to enter the data correctly into the text file. I think. Now Im not the experienced with I/O files Im able to read the fist line by using
String first;
first = inputFiles.nextLine();
System.out.println(first);
Then the doubles for the first company. But how exactly can you read specific lines. Like After reading the first set of doubles, how would i be able to read Line 5 the second company name? Im sorry that im terrible with this stuff the I/O is killing me.
- 02-08-2012, 05:45 PM #8
Member
- Join Date
- Feb 2012
- Posts
- 6
- Rep Power
- 0
Re: Question With a loop
Hi Mike,
you could use this instead:
String input;
input = javax.swing.JOptionPane.showInputDialog("Please Companyname");
You get a popup window for every call.
jokobe
- 02-08-2012, 05:56 PM #9
Member
- Join Date
- Feb 2012
- Posts
- 19
- Rep Power
- 0
Re: Question With a loop
Thank you so much for the help. Going to tinker around again might have another question lol. That .next helped me out so much.
- 02-08-2012, 05:58 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Re: Question With a loop
If they're learning IO then introducing Swing to the equation isn't going to help.
To the OP:
So you have a file now and you want to find line 'x' in it?
Loop 'x' times.
- 02-08-2012, 06:13 PM #11
Member
- Join Date
- Feb 2012
- Posts
- 19
- Rep Power
- 0
Re: Question With a loop
Alright, I just have one last question. Ive cleaned up my code and I have a program now that the user enters 4 Different company names, and their insurance coverage. This includes the total Coverage for each Company as well. Now all I need to do now is figure out the lowest Cost for the total Coverage. Now since this is using IO where exactly should I find the lowest number after I print out the numbers from the file or while im uploading them. Here is my new code.
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class AutoInsurence {
/**
* @param args
*/
public static void main(String[] args) throws IOException {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the filename: ");
String filename = keyboard.nextLine();
PrintWriter outputFile = new PrintWriter(filename);
for (int i = 1; i <= 4; i++) {
System.out.print("Enter the name of company " + "number " + i
+ ": ");
String companyName = keyboard.next();
outputFile.println(companyName);
System.out
.print("Enter the rate for Bodily Injury Liability of company "
+ "number " + i + ": ");
double rateBodily = keyboard.nextDouble();
outputFile.println(rateBodily);
System.out
.print("Enter the rate for Property Damage Liability of company "
+ "number " + i + ": ");
double rateProperty = keyboard.nextDouble();
outputFile.println(rateProperty);
System.out
.print("Enter the rate for Uninsured Drivers Coverage of company "
+ "number " + i + ": ");
double rateUninsured = keyboard.nextDouble();
outputFile.println(rateUninsured);
double totalCost = rateBodily + rateProperty + rateUninsured;
outputFile.println(totalCost);
}
outputFile.close();
File myFile = new File(filename);
Scanner inputFiles = new Scanner(myFile);
for (int i = 1; i <= 4; i++) {
String name = inputFiles.next();
System.out.println("Company " + "i: " + name);
Double bodily = inputFiles.nextDouble();
System.out.println("Rate for Bodily Injury Liablility " + bodily);
Double property = inputFiles.nextDouble();
System.out.println("Rate for Property Damage Liablility "
+ property);
Double uninsured = inputFiles.nextDouble();
System.out.println("Rate for Uninsured Drivers Coverage "
+ uninsured);
Double total = inputFiles.nextDouble();
System.out.println("Total Annual Cost of Coverage " + total);
}
inputFiles.close();
}
}
- 02-08-2012, 06:20 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Re: Question With a loop
You need to use code tags.
Your previous attempt had the slash the wrong way round.
As for the question (I can't read the code as it stands), you could store the entered values and then compare them?
- 02-08-2012, 06:37 PM #13
Member
- Join Date
- Feb 2012
- Posts
- 19
- Rep Power
- 0
Re: Question With a loop
Okay I think I might have gotten it figured out how do you make one string equal to another. Example for integers. highest = number.
Hopefully this makes sense. How do you assign a string to another string if thats possible.
- 02-08-2012, 06:42 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
Re: Question With a loop
A simple keyboard.nextLine() call as the last statement of the body of that for-loop would've cured it all; no need for Swing components here.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-08-2012, 06:46 PM #15
Member
- Join Date
- Feb 2012
- Posts
- 19
- Rep Power
- 0
Similar Threads
-
Do....while Loop question
By Roter in forum New To JavaReplies: 10Last Post: 01-25-2012, 04:49 AM -
Loop question
By rich123 in forum New To JavaReplies: 6Last Post: 02-08-2011, 02:26 AM -
loop question
By ccie007 in forum New To JavaReplies: 22Last Post: 08-15-2010, 08:29 PM -
for Loop with Yes/No Question! help..please!
By mastercrimson in forum New To JavaReplies: 8Last Post: 06-02-2010, 05:08 PM -
Question about for loop..
By sivakumar_sakam in forum New To JavaReplies: 4Last Post: 05-15-2009, 11:23 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks