Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Problem with I/O

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    19
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Problem with I/O

    So I'm trying to read 11 values from a text file, each value on a separate line. The first value I use as loop control to run through calculations on the other ten and finally output both the numbers and the calculations to the console and an output file. I'm not getting a compiler error or a runtime error but my loop seems to stop after reading the first line. It also doesnt seem to be writing to my output file but does create it when I run the program. This is what my text file looks like

    10
    150.4
    88.4
    -3.14
    499.4
    799.4
    1299.8
    0
    1900.2
    901.7
    4444.4


    This is what my program looks like

    import java.util.*;
    import java.io.*;
     
    public class assignment7scratch
    {
     
      Toolkit_General toolKit = new Toolkit_General();
     
      public static void main (String[]args)throws IOException
      {
     
      double rentalCost = 0; // holds cost calculated from mileage
      double mileage = 0;    // miles driven
      int controlVar = 0;   // loop control for going through input file
      String inputFile = "QudratullahMommandi_3_07_Input.txt";
     
       // Summary Statement
       SummaryStatement();
     
       //Output file being created
       PrintWriter outPut = new PrintWriter("QudratullahMommandi_3_07_Output.txt");
     
     
     
       // input file being accessed
       File file = new File(inputFile);
       Scanner inputRead = new Scanner(file);
     
     
       // outputting a table heading
       TableHeading();
     
       // using the first line of input for loop control
       controlVar = inputRead.nextInt();
     
       // running through the contents of the input file
       for (int index = 0; index < controlVar; index++);
        { 
         // getting value from input
         mileage = inputRead.nextDouble();
     
          // construct to get the rentalCost calculation from mileage
          if (mileage <=0)
          { 
           System.out.println(mileage + "\t\t\t\t\t" + "*****");
          }
          else if (mileage < 400)
          { 
           rentalCost = mileage*.18;
           System.out.println(mileage + "\t\t\t\t\t" + rentalCost);
          }
          else if (mileage < 900)
          { 
           rentalCost = 65.00 + (.15 * (mileage-400));
           System.out.println(mileage + "\t\t\t\t\t" + rentalCost);
          }
          else if (mileage < 1300)
          { 
           rentalCost = 115.00 + (.12 * (mileage-800));
           System.out.println(mileage + "\t\t\t\t\t" + rentalCost);
          }
          else if (mileage < 1900)
          {
            rentalCost = 140.00 + (.10 * (mileage-1200));
            System.out.println(mileage + "\t\t\t\t\t" + rentalCost);
          }
          else if (mileage < 2600)
          {
            rentalCost = 165.00 + (.08 * (mileage-1800));
            System.out.println(mileage + "\t\t\t\t\t" + rentalCost);
          }
          else if (mileage >= 2600)
          { 
           rentalCost = 195.00 + (.06 * (mileage-2500));
           System.out.println(mileage + "\t\t\t\t\t" + rentalCost);
          }
     
          // outputting to output folder
          outPut.println(mileage + "\t\t\t\t\t" + rentalCost);
     
     
         }// end for loop
     
         // closing input and output files
         inputRead.close();
         outPut.close();
     
     
      }// end main
     
     
     
     
      public static void SummaryStatement()
      { 
          System.out.println("This program reads a list of values representing number of miles"
          + " driven by individuals.\n" + "It will output the dollar amount their rental cars cost.");   
      }// end SummaryStatement
     
     
      public static void TableHeading()
      { 
          System.out.println();
          System.out.println("Number of miles traveled on the left as well as amount reimbursed on the right");
          System.out.println("-----------------------------------------------");
          System.out.println("Miles Driven" + "                  " + "Amount reimbursed");
      }
     
     }// end class

    so I dont get an error but this is what my output looks like


    ----jGRASP exec: java assignment7scratch

    This program reads a list of values representing number of miles driven by individuals.
    It will output the dollar amount their rental cars cost.

    Number of miles traveled on the left as well as amount reimbursed on the right
    -----------------------------------------------
    Miles Driven Amount reimbursed
    150.4 27.072

    ----jGRASP: operation complete.

    it also doesn't write anything to my output file, though it does create one.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Problem with I/O

    Is the statement that is supposed to write to the file executed? Add a println next to it to see if the code is executed.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    19
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Problem with I/O

    Quote Originally Posted by Norm View Post
    Is the statement that is supposed to write to the file executed? Add a println next to it to see if the code is executed.
    I changed the code a bit, it is writing to the output file now but its still only processing the first line of information. I tried printing controlvariable and it prints as 10 so the loop understands the control variable as 10 but doesnt run through ten times, or does but doesnt print 10 times ...idk which or maybe its something else.

    This is my updated code I tried to change things for readability.
    import java.util.*;
    import java.io.*;
     
    public class assignment7scratch
    {
     
      Toolkit_General toolKit = new Toolkit_General();
     
      public static void main (String[]args)throws IOException
      {
     
      final String OUTPUT = "QudratullahMommandi_3_07_Output.txt";
      final String INPUT = "QudratullahMommandi_3_07_Input.txt";
      double rentalCost = 0; // holds cost calculated from mileage
      double mileage = 0;    // miles driven
      int controlVar = 0;   // loop control for going through input file
      String table;         // holds the table
     
       // Summary Statement
       SummaryStatement();
     
       //Output file being created
       PrintWriter outPut = new PrintWriter(OUTPUT);
     
     
     
       // input file being accessed
       File file = new File(INPUT);
       Scanner inputRead = new Scanner(file);
     
     
       // outputting a table heading to console and folder
       table = TableHeading();
       System.out.println(table);
       outPut.println(table);
     
       // using the first line of input for loop control
       controlVar = inputRead.nextInt();
     
       // running through the contents of the input file
       for (int index = 0; index < controlVar; index++);
        { 
         // getting value from input
         mileage = inputRead.nextDouble();
     
          // construct to get the rentalCost calculation from mileage
          if (mileage <=0)
          { 
           System.out.println(mileage + "\t\t\t\t\t" + "*****");
           outPut.println(mileage + "\t\t\t\t\t" + "*****");
          }
          else if (mileage < 400)
          { 
           rentalCost = mileage*.18;
           System.out.println(mileage + "\t\t\t\t\t" + rentalCost);
           outPut.println(mileage + "\t\t\t\t\t" + rentalCost);
          }
          else if (mileage < 900)
          { 
           rentalCost = 65.00 + (.15 * (mileage-400));
           System.out.println(mileage + "\t\t\t\t\t" + rentalCost);
           outPut.println(mileage + "\t\t\t\t\t" + rentalCost);
          }
          else if (mileage < 1300)
          { 
           rentalCost = 115.00 + (.12 * (mileage-800));
           System.out.println(mileage + "\t\t\t\t\t" + rentalCost);
           outPut.println(mileage + "\t\t\t\t\t" + rentalCost);
          }
          else if (mileage < 1900)
          {
            rentalCost = 140.00 + (.10 * (mileage-1200));
            System.out.println(mileage + "\t\t\t\t\t" + rentalCost);
            outPut.println(mileage + "\t\t\t\t\t" + rentalCost);
          }
          else if (mileage < 2600)
          {
            rentalCost = 165.00 + (.08 * (mileage-1800));
            System.out.println(mileage + "\t\t\t\t\t" + rentalCost);
            outPut.println(mileage + "\t\t\t\t\t" + rentalCost);
          }
          else if (mileage >= 2600)
          { 
           rentalCost = 195.00 + (.06 * (mileage-2500));
           System.out.println(mileage + "\t\t\t\t\t" + rentalCost);
           outPut.println(mileage + "\t\t\t\t\t" + rentalCost);
          }
     
     
     
         }// end for loop
     
         // closing input and output files
         inputRead.close();
         outPut.close();
     
     
      }// end main
     
     
     
     
      public static void SummaryStatement()
      { 
          System.out.println("This program reads a list of values representing number of miles"
          + " driven by individuals.\n" + "It will output the dollar amount their rental cars cost.");   
      }// end SummaryStatement
     
     
      public static String TableHeading()
      {  
          String holder =
          ("Number of miles traveled on the left as well as amount reimbursed on the right\n" 
           + "-----------------------------------------------\n"
           +"Miles Driven" + "                  " + "Amount reimbursed");
           return holder;
      }// end TableHeading
     
     }// end class

    this is the output file after running

    Number of miles traveled on the left as well as amount reimbursed on the right
    -----------------------------------------------
    Miles Driven Amount reimbursed
    150.4 27.072


    I wrote a similar program for another assignment and it uses .hasnext to check if the input has another file and it ran through the entire input file fine, so I have a feeling it has something to do with an error in my for loop or the following if/elseif logic, but I've tried changing it around and going through it slowly and it should work as far as I can see but it doesnt.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Problem with I/O

    an error in my for loop
    What error? Copy and post the full text of any error messages.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    19
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Problem with I/O

    Quote Originally Posted by Norm View Post
    What error? Copy and post the full text of any error messages.
    Not an error in that sense, I'm not getting any error messages, I meant something wrong in my loops logic.

    --- Update ---

    My prof emailed me back, I had a semicolon at the end of my for loop. My penchant for overlooking details is becoming apparent, and my future as a computer programmer is becoming ever murkier in my minds eye, this string of a possible future is naught but a muddy illusion.

Similar Threads

  1. Catch block problem. Please fix my problem.
    By damnitsme in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 3rd, 2014, 01:49 AM
  2. Problem with Project Euler problem 18
    By sara_magdy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 19th, 2013, 12:46 PM
  3. Replies: 3
    Last Post: January 5th, 2012, 01:44 AM
  4. [SOLVED] [Problem] imports javax.swing problem
    By Brollie in forum AWT / Java Swing
    Replies: 8
    Last Post: July 5th, 2009, 07:59 AM
  5. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM

Tags for this Thread