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

Thread: Reading from a txt file and loop trouble

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reading from a txt file and loop trouble

    The code:

    // This program will read in from a file a series of numbers in degrees
    // Celsius, convert them to degree Fahrenheit and print out a table of 
    // those values.  The file input is handled using the command line's 
    // redirection mechanism.
     
    import java.util.Scanner;
     
    public class Lab4b
    {
       public static void main(String[] args)
       {
          // Declare variables
          Scanner scan = new Scanner(System.in);
     
          double degreeCelsius;
          double degreeFahrenheit;
     
          // Print to the screen the header of the output table
          // as seen in the sample run below.
          // There are two lines in the header.
          // Make sure it has the right spacing to line up with the table.
          System.out.println("Celsius    | Fahrenheit");
          System.out.println("-----------+-----------");
     
          // Write your loop here
          // We want to loop until there are no more inputs to be read. 
     
          while( scan.hasNext())
          {
             // Check to see if the input is a double.
             // If a double read in and store in degreeCelsius.
             // If not a double print an error message and quit.
             if (scan.hasNextDouble())
    			degreeCelsius = scan.nextDouble();
     
    		 else
    		 {
    			System.out.println("Error in input");
    			System.out.println("Programming is terminating...");
    		        System.exit(0);
    		 }
     
     
             // Convert input degrees Celsius to degrees Fahrenheit
             // and store in degreeFahrenheit.
             // f = c * (9.0/5.0) + 32.0
     
             degreeFahrenheit = degreeCelsius * (9.0/5.0) + 32.0;
     
             // Display to the screen the output as shown in the 
             // sample run below.  Use System.out.printf.  Make sure
             // that everything lines up properly.
             System.out.printf("       degreeCelsius%d|       degreeFahrenheit%d%n");
     
          } // End of loop
       } // End of main method
    } // End class

    I cant figure out what I am doing wrong in the loop.

    When I try and compile I get an initialization error

    C:\Users\Mitchell\My Documents\cs1113\Labs>javac Lab4b.java
    Lab4b.java:52: error: variable degreeCelsius might not have been initialized
    degreeFahrenheit = degreeCelsius * (9.0/5.0) + 32.0;

    So.. I set degreeCelsius = 0;
    Then it compiles but when I run the command java Lab4b < lab4data.txt


    C:\Users\Mitchell\My Documents\cs1113\Labs>java Lab4b < lab4data.txt
    Celsius | Fahrenheit
    -----------+-----------
    degreeCelsiusException in thread "main" java.util.MissingFormatArgumentEx
    ception: Format specifier 'd'
    at java.util.Formatter.format(Formatter.java:2487)
    at java.io.PrintStream.format(PrintStream.java:970)
    at java.io.PrintStream.printf(PrintStream.java:871)
    at Lab4b.main(Lab4b.java:57)

    So either I am having trouble with the loop or the printf statement
    Any hints/tips on those would be great



    Here is the txt file it has to read in:
    5.5
    15.2
    25.4
    35.8
    45.1
    55.9
    65.3
    75.7
    85.6
    95.0

    And here is the print format is has to look like:

    command in cmd: java Lab4b < lab4data.txt

    Celsius | Fahrenheit
    -----------+-----------
    5.50| 41.90
    15.20| 59.36
    25.40| 77.72
    35.80| 96.44
    45.10| 113.18
    55.90| 132.62
    65.30| 149.54
    75.70| 168.26
    85.60| 186.08
    95.00| 203.00

    ^^^^The lines have to match up^^^^
    Last edited by LaMigra; September 18th, 2014 at 05:19 AM.


  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: Reading from a txt file and loop trouble

    Welcome to the forum. Good first post . . . except, please describe what you need help with. Ask a specific question or describe the problem. If you're getting compiler or runtime errors, please post the entire error message with the stack trace, as appropriate. If logic or output errors, show a sample run by copying your console and pasting it into a post.

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading from a txt file and loop trouble

    Quote Originally Posted by GregBrannon View Post
    Welcome to the forum. Good first post . . . except, please describe what you need help with. Ask a specific question or describe the problem. If you're getting compiler or runtime errors, please post the entire error message with the stack trace, as appropriate. If logic or output errors, show a sample run by copying your console and pasting it into a post.
    edited for clarification.

  4. #4
    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: Reading from a txt file and loop trouble

    Local variables must be initialized, as in:

    int localVariable = 0;

    printf() can be confusing. Your statement should look something like:

    System.out.printf(" %f| %f\n", degreeCelsius, degreeFahrenheit );

    but you'll need to adjust it to get the desired output format.

  5. #5
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading from a txt file and loop trouble

    Quote Originally Posted by GregBrannon View Post
    Local variables must be initialized, as in:

    int localVariable = 0;

    printf() can be confusing. Your statement should look something like:

    System.out.printf(" %f| %f\n", degreeCelsius, degreeFahrenheit );

    but you'll need to adjust it to get the desired output format.
    Thank you I kinda out the printf. Need the printf format to make the output a double.

    but I encountered this when I initialized degreeCelsius = 0;

    C:\Users\Mitchell\My Documents\cs1113\Labs>java Lab4b < lab4data.txt
    Celsius | Fahrenheit
    -----------+-----------
    0.000000| 32.000000
    0.000000| 32.000000
    0.000000| 32.000000
    0.000000| 32.000000
    0.000000| 32.000000
    0.000000| 32.000000
    0.000000| 32.000000
    0.000000| 32.000000
    0.000000| 32.000000
    0.000000| 32.000000

    What do I need to do in the loop so the txt table loops through the temperature formula?

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

    Default Re: Reading from a txt file and loop trouble

    Where does the posted code read from a file?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading from a txt file and loop trouble

    Quote Originally Posted by Norm View Post
    Where does the posted code read from a file?
    Ok I figured out my initialization problem. Everything works great just need help with the printf format how to lose all those zero's in my printout.

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

    Default Re: Reading from a txt file and loop trouble

    Look at the API doc for the printf() method. It has a link to the Format String syntax in the Formatter class.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Reading lines from a txt file
    By neliJav in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 6th, 2013, 01:54 PM
  2. [SOLVED] Help reading a txt file into a datatype similar to a db
    By jmc117 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 27th, 2013, 02:11 PM
  3. Reading a txt file and storing them in a Java Array
    By FrozenFox in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: July 27th, 2012, 07:19 AM
  4. reading and witing to txt file
    By nautilusz in forum File I/O & Other I/O Streams
    Replies: 25
    Last Post: January 22nd, 2012, 02:02 PM
  5. [SOLVED] reading only certain lines from a .txt file
    By straw in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: March 7th, 2010, 07:49 PM