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 7 of 7

Thread: Two questions, 1st having to do with File I/O, second needing advice on how to process contents from an input file

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

    Default Two questions, 1st having to do with File I/O, second needing advice on how to process contents from an input file

    So my assignment is to read these values from an input file which I previously created. The 10 values represent miles traveled and I convert them using some formula my prof gave me to get the total cost, and output both values to an output file.

    My programming question is the first number on the input file isnt a mileage value its the number 10, which is the number of values to be processed, which we're supposed to use in order to control a for or while loop which we'll use to process the contents of the input file. How would I create a while or for loop and get it to skip the first value of the input file?

    We're not allowed to use arrays for this assignment.

    Also here's my code so far, and it says its not being able to find the file, I made sure the file name is exactly as saved, and its saved in the same directory as the java class that i created for this assignment so I don't see what the problem could be?


    Here's my the class with the main method

    import java.util.*;
    import java.io.*;
     
    public class QudratullahMommandi_3_07
    {
     
     
      public static void main (String[]args)
      {
      double rentalCost;
      double mileage;
      String inputFile = "QudratullahMommandi_S_07.txt";
     
       SummaryStatement();
     
       File inputSource = new File(inputFile);
       Scanner input = new Scanner(inputSource);
     
       TableHeading();
     
       while (input.hasNext());
        { mileage = input.nextDouble();
     
          if (mileage <=0)
          { System.out.println("*****");
          }
          else if (mileage < 400)
          { rentalCost = mileage*.18;
          }
          else if (mileage < 900)
          { rentalCost = 65.00 + (.15 * (mileage-400));
          }
          else if (mileage < 1300)
          { rentalCost = 115.00 + (.12 * (mileage-800));
          }
          else if (mileage < 1900)
          { rentalCost = 140.00 + (.10 * (mileage-1200));
          }
          else if (mileage < 2600)
          { rentalCost = 165.00 + (.08 * (mileage-1800));
          }
          else if (mileage >= 2600)
          { rentalCost = 195.00 + (.06 * (mileage-2500));
          }
     
         }
     
     
     
      }// 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


    This is the error message I get

    ----jGRASP exec: javac -g QudratullahMommandi_3_07.java

    QudratullahMommandi_3_07.java:17: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
    Scanner input = new Scanner(inputSource);
    ^
    1 error

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.
    Last edited by Helplessdrowningpuppy; March 22nd, 2014 at 08:15 PM. Reason: added info


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Two questions, 1st having to do with File I/O, second needing advice on how to process contents from an input file

    You don't 'skip' the first value in the file. If you don't want to use it, collect it and then throw it away. However, the purpose of the number 10 is to give you a value to use in a loop to gather the rest of the values from the file.

    You're misinterpreting your error message. It's not that the file cannot be found, it says:

    error: unreported exception FileNotFoundException; must be caught or declared to be thrown

    Which means there is a possible exception that must be handled. The existence of the exception is mentioned on the Scanner API page, and here's an example of how to handle it.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    Helplessdrowningpuppy (March 23rd, 2014)

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

    Default Re: Two questions, 1st having to do with File I/O, second needing advice on how to process contents from an input file

    Thank you for the tip on the try catch construct, I thought because the file obviously existed I didn't need to put anything in for exceptions but I guess not. When I compile the program now though it's not printing the stack trace so I assume it found the file, but my scanner object isnt being recognized, which means that it didn't find the file?

    Here's an updated version of my code.

    import java.util.*;
    import java.io.*;
     
    public class QudratullahMommandi_3_07
    {
     
     
      public static void main (String[]args)
      {
     
      String numberOfValues;
      double rentalCost;
      double mileage;
      int controlVar;
      String inputFile = "C:\\Users\\Qudratullah\\Documents\\CSprograms\\QudratullahMommandi_3_07_Output";
     
       SummaryStatement();
     
      try
       { File inputSource = new File(inputFile);
        Scanner inputRead = new Scanner(inputSource);
       }
      catch (FileNotFoundException e)
       { e.printStackTrace();
       }
     
       TableHeading();
     
       numberOfValues = inputRead.nextLine();
       controlVar = Integer.parseInt(numberOfValues);
     
       for (int index = 0; index < controlVar; index++);
        { mileage = inputRead.nextDouble();
     
          if (mileage <=0)
          { System.out.println("*****");
          }
          else if (mileage < 400)
          { rentalCost = mileage*.18;
          }
          else if (mileage < 900)
          { rentalCost = 65.00 + (.15 * (mileage-400));
          }
          else if (mileage < 1300)
          { rentalCost = 115.00 + (.12 * (mileage-800));
          }
          else if (mileage < 1900)
          { rentalCost = 140.00 + (.10 * (mileage-1200));
          }
          else if (mileage < 2600)
          { rentalCost = 165.00 + (.08 * (mileage-1800));
          }
          else if (mileage >= 2600)
          { rentalCost = 195.00 + (.06 * (mileage-2500));
          }
     
         }
     
     
     
      }// 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

    This is the error I get now

    QudratullahMommandi_3_07.java:29: error: cannot find symbol
    numberOfValues = inputRead.nextLine();
    ^
    symbol: variable inputRead
    location: class QudratullahMommandi_3_07
    QudratullahMommandi_3_07.java:33: error: cannot find symbol
    { mileage = inputRead.nextDouble();
    ^
    symbol: variable inputRead
    location: class QudratullahMommandi_3_07
    2 errors

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Two questions, 1st having to do with File I/O, second needing advice on how to process contents from an input file

    The variable in the error message is defined inside of a pair of {}s and is out of scope where the code is trying to use it.
    You can define a variable outside of the {}s and give it a value inside of the {}s so it is in scope where it is being used.

    Also the formatting of the code needs to be fixed so that the { and } line up one above the other. There should NOT be code on the same line following a { because that tends to hide the { making the logic harder to understand.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    Helplessdrowningpuppy (March 23rd, 2014)

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

    Default Re: Two questions, 1st having to do with File I/O, second needing advice on how to process contents from an input file

    Quote Originally Posted by Norm View Post
    The variable in the error message is defined inside of a pair of {}s and is out of scope where the code is trying to use it.
    You can define a variable outside of the {}s and give it a value inside of the {}s so it is in scope where it is being used.

    Also the formatting of the code needs to be fixed so that the { and } line up one above the other. There should NOT be code on the same line following a { because that tends to hide the { making the logic harder to understand.

    I fixed up the code for readability, my prof has weird formatting guidelines. The problem still remains that the file isnt being found though. I've tried deleting it and creating a new one with no luck. All my java files are in mydocs which is the default folder they were in when I installed Jgrasp, and my text file is in there as well so I don't know why its not being able to find the file.

    This is the error message
    ----jGRASP exec: java QudratullahMommandi_3_07

    This program reads a list of values representing number of miles driven by individuals.
    It will output the dollar amount their rental cars cost.
    Exception in thread "main" java.io.FileNotFoundException: C:\Users\Qudratullah\Documents\CSprograms\Qudratul lahMommandi_3_07_Output.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:146)
    at java.util.Scanner.<init>(Scanner.java:656)
    at QudratullahMommandi_3_07.main(QudratullahMommandi_ 3_07.java:19)

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.

    import java.util.*;
    import java.io.*;
     
    public class QudratullahMommandi_3_07
    {
     
     
      public static void main (String[]args)throws IOException
      {
     
      String numberOfValues;
      double rentalCost;
      double mileage;
      int controlVar;
      String inputFile = "C:\\Users\\Qudratullah\\Documents\\CSprograms\\QudratullahMommandi_3_07_Output.txt";
     
       SummaryStatement();
       File file = new File(inputFile);
       Scanner inputRead = new Scanner(file);
     
     
     
       TableHeading();
       numberOfValues = inputRead.nextLine();
       controlVar = Integer.parseInt(numberOfValues);
     
       for (int index = 0; index < controlVar; index++);
        { 
          mileage = inputRead.nextDouble();
     
          if (mileage <=0)
          { 
           System.out.println("*****");
          }
          else if (mileage < 400)
          { 
           rentalCost = mileage*.18;
          }
          else if (mileage < 900)
          { 
           rentalCost = 65.00 + (.15 * (mileage-400));
          }
          else if (mileage < 1300)
          { 
           rentalCost = 115.00 + (.12 * (mileage-800));
          }
          else if (mileage < 1900)
          {
            rentalCost = 140.00 + (.10 * (mileage-1200));
          }
          else if (mileage < 2600)
          {
            rentalCost = 165.00 + (.08 * (mileage-1800));
          }
          else if (mileage >= 2600)
          { 
           rentalCost = 195.00 + (.06 * (mileage-2500));
          }
     
         }// end for loop
     
     
     
      }// 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

  8. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Two questions, 1st having to do with File I/O, second needing advice on how to process contents from an input file

    Unfortunately, this is more of a jGrasp question than a Java question, and we would need to be using jGrasp to understand how to solve your problem. Since few here (none?) use jGrasp, we don't know what to advise. Others in your class or the instructor/TAs should be able to help. I couldn't find anything in the jGrasp documentation that helped, but it's difficult to be sure without using the tool.

    Good luck.

  9. The Following User Says Thank You to GregBrannon For This Useful Post:

    Helplessdrowningpuppy (March 24th, 2014)

  10. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Two questions, 1st having to do with File I/O, second needing advice on how to process contents from an input file

    why its not being able to find the file
    Try a test: Simplify the path to: C:\\Test.txt
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    Helplessdrowningpuppy (March 26th, 2014)

Similar Threads

  1. Replies: 19
    Last Post: March 15th, 2013, 03:11 PM
  2. how to get value path file from jsp form- input type file
    By meeGoreng in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: October 4th, 2011, 12:05 AM
  3. advice for an Odd, Even, Zero counter from input.txt file
    By sanchezjk in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 29th, 2011, 06:15 AM
  4. Read a text file and parse the contents of file
    By HelloAll in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 3rd, 2011, 05:47 AM
  5. Replies: 8
    Last Post: January 6th, 2010, 09:59 AM

Tags for this Thread