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

Thread: Parsing input from a file using Scanner

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

    Default Parsing input from a file using Scanner

    I'm sorry if the thread title is misleading I solved my initial problem but ran into another one. I'm having some kind of weird problem reading input from a file. It says that my scanner object I'm using to hold an item of information isn't initialized, when I do try to initialize it, it says error variable already initialized. I'm using the scanner to read input from a file whose contents are this

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

    This is my program

    import java.util.*;
    import java.io.*;
     
    public class QudratullahMommandi_3_07
    {
     
      Toolkit_General toolKit = new Toolkit_General();
     
      public static void main (String[]args)throws IOException
      {
     
     
      double rentalCost = 0;
      double mileage;
      int controlVar;
      String inputFile = "QudratullahMommandi_3_07_Output.txt";
     
     
       SummaryStatement();
       File file = new File(inputFile);
       Scanner inputRead = new Scanner(file);
     
     
     
       TableHeading();
     
       controlVar = inputRead.nextInt();
     
     
       for (int index = 0; index < controlVar; index++);
        { 
     
          String holder = inputRead.nextLine();
          String holder2 = holder2.trim();
          mileage = Double.parseDouble(holder2);
     
          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));
          }
     
          System.out.println(rentalCost);
     
         }// 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

    This is the error message


    QudratullahMommandi_3_07.java:34: error: variable holder2 might not have been initialized
    String holder2 = holder2.trim();
    ^
    1 error

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.
    Last edited by Helplessdrowningpuppy; March 26th, 2014 at 02:56 AM.


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Parsing input from a file using Scanner

    it is either you make you controlVar a double premitive type. or
    cast what you have parse to int premitive type : controlVar = (int) Double.parseDouble(numberOfValues);

    because your controlVar is integer and your trying to put a value on it which has a premitive type of double.

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

    Default Re: Parsing input from a file using Scanner

    Yes, it was inappropriate to delete your original question and change it to a new question. dicdic's answer is completely out of context and makes no sense.

    Next time, mark your first thread solved and start a new thread for your new question.

    As for your current question, you're confused. holder2 is not a Scanner object; it's a String object, and I think you meant this statement:

    String holder2 = holder2.trim();

    to be:

    String holder2 = holder.trim();

    Tired?

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

    Helplessdrowningpuppy (March 26th, 2014)

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

    Default Re: Parsing input from a file using Scanner

    I was tired to the point of mild retardation making this thread, dicdic helped me out with something obvious, so ty, I have another question related to this same program but I'll start a new thread for it.

Similar Threads

  1. complex input parsing
    By Wolfone in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: September 3rd, 2013, 12:56 AM
  2. File input using Scanner help.
    By AaronHarris in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 4th, 2013, 07:15 AM
  3. Replies: 5
    Last Post: October 19th, 2011, 08:21 PM
  4. Parsing input based on grammar rules
    By Winterfresh in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 11th, 2010, 07:54 PM
  5. Input file parsing to insert into DB
    By IDForums in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: September 30th, 2009, 02:29 AM