Results 1 to 15 of 15
  1. #1
    zniightmare is offline Member
    Join Date
    Feb 2012
    Posts
    19
    Rep Power
    0

    Default 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

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,040
    Rep Power
    10

    Default 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!

  3. #3
    zniightmare is offline Member
    Join Date
    Feb 2012
    Posts
    19
    Rep Power
    0

    Default 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?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default 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.

  5. #5
    jokobe is offline Member
    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0

    Default Re: Question With a loop

    Hi Mike,

    String companyName = keyboard.next();
    instead of
    String companyName = keyboard.nextLine();

    Jokobe

  6. #6
    zniightmare is offline Member
    Join Date
    Feb 2012
    Posts
    19
    Rep Power
    0

    Default 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]

  7. #7
    zniightmare is offline Member
    Join Date
    Feb 2012
    Posts
    19
    Rep Power
    0

    Default 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.

  8. #8
    jokobe is offline Member
    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0

    Default 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

  9. #9
    zniightmare is offline Member
    Join Date
    Feb 2012
    Posts
    19
    Rep Power
    0

    Default 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.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default 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.

  11. #11
    zniightmare is offline Member
    Join Date
    Feb 2012
    Posts
    19
    Rep Power
    0

    Default 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();

    }

    }

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default 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?

  13. #13
    zniightmare is offline Member
    Join Date
    Feb 2012
    Posts
    19
    Rep Power
    0

    Default 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.

  14. #14
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,778
    Blog Entries
    7
    Rep Power
    21

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    zniightmare is offline Member
    Join Date
    Feb 2012
    Posts
    19
    Rep Power
    0

    Default Re: Question With a loop

    Woot!!!!!! Got it working I want to thanks you all individually Tolls, Jokobe, and JosAH. You guys were a tremendous help. Thanks you all!!

Similar Threads

  1. Do....while Loop question
    By Roter in forum New To Java
    Replies: 10
    Last Post: 01-25-2012, 05:49 AM
  2. Loop question
    By rich123 in forum New To Java
    Replies: 6
    Last Post: 02-08-2011, 03:26 AM
  3. loop question
    By ccie007 in forum New To Java
    Replies: 22
    Last Post: 08-15-2010, 09:29 PM
  4. for Loop with Yes/No Question! help..please!
    By mastercrimson in forum New To Java
    Replies: 8
    Last Post: 06-02-2010, 06:08 PM
  5. Question about for loop..
    By sivakumar_sakam in forum New To Java
    Replies: 4
    Last Post: 05-16-2009, 12:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •