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

Thread: Problem with looping

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with looping

    I'm a beginner in Java and I wanted to create a program where I can convert from celsius to fahrenheit but I need to be able to read and process data from a text file also. The text file has 5 lines of text each with different numeric values and when I run my program I'm only able to do a conversion for the first line of text. I know that I should be able to get my program to read all of the lines and do the calculations with a loop but I'm not sure how to do it. Any suggestions would be helpful. Here is my code so far:

    import java.util.Scanner;
    import java.io.*;
     
    public class CToFConversion {
     
    		char symbol = '\u00B0'; // this is the unicode "degree" symbol
    		Scanner myScanner= new Scanner("tempdata.txt");
     
    		public void readFile(String file_name) {
    		try{
    			 File file = new File(file_name);
    			 Scanner myScanner= new Scanner(file);
    			 double celsius = myScanner.nextDouble();
    			 double Fahrenheit = 9.0 / 5 * (celsius + 32);
    			 System.out.print(celsius + " Celsius is" + " Fahrenheit ");
    			 System.out.printf("%5.1f", Fahrenheit);	
     
    		while(myScanner.hasNextLine()){
    			 System.out.println(myScanner.nextLine());	
    			}
     
    		}catch(Exception ex){
    			 System.out.println(ex.getMessage());
    			 System.exit(0);
    		}
     
    	}
    }

    I also used another class to run this program, this is the code for that:

    import java.io.*;
     
    public class CToFConversionLoop {
     
    	public static void main(String[] args) throws IOException {
     
    		CToFConversion conversion = new CToFConversion();
    		conversion.readFile("tempdata.txt");String file_name = "tempdata.txt";	
     
    	}
    }

    The text file has 5 lines:
    0.0
    33.3
    100.0
    -10.0
    -273.0


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

    Default Re: Problem with looping

    What does your program print out when it executes?

    The program doesn't save the String that is returned by the nextLine() method. It should save the String that was read by assigning it to a String variable that can then be processed by the program.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with looping

    This is the result when it executes:

    0.0 Celsius is Fahrenheit 57.6
    33.3
    100.0
    -10.0
    -273.0

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

    Default Re: Problem with looping

    There is code that reads the values from the file and computes a value.
    Can you put that code inside the loop?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with looping

    I've put the values used to do the calculations in the loop but for the first line of the text file it skips it. Is there a different method I should use instead of nextLine? This is the code:

    import java.util.Scanner;
    import java.io.*;
     
    public class CToFConversion {
     
    		char symbol = '\u00B0'; // this is the unicode "degree" symbol
    		Scanner myScanner= new Scanner("tempdata.txt");
     
    		public void readFile(String file_name) {
    		try{
    			 File file = new File(file_name);
    			 Scanner myScanner= new Scanner(file);	
     
    		while(myScanner.hasNextLine()){
    			 System.out.println(myScanner.nextLine());
    			 double celsius = myScanner.nextDouble();
    			 double Fahrenheit = 9.0 / 5 * (celsius + 32);
    			 System.out.print(celsius + " Celsius is" + " Fahrenheit ");
    			 System.out.printf("%5.1f", Fahrenheit);	
    			}
     
    		}catch(Exception ex){
    			 System.out.println(ex.getMessage());
    			 System.exit(0);
    		}
     
    	}
    }

    This is my output:
    0.0
    33.3 Celsius is Fahrenheit 117.5
    100.0 Celsius is Fahrenheit 237.6
    -10.0 Celsius is Fahrenheit 39.6
    -273.0 Celsius is Fahrenheit -433.8

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

    Default Re: Problem with looping

    First line of the text file it skips it.
    The String read by the nextLine() method is printed and then is thrown away and not saved. What is the purpose of the statement:
     System.out.println(myScanner.nextLine());
    The data on every line read by nextLIne() in this statement is lost to the program.


    This statement reads a value from the file, converts it to double and saves its value in a variable that is used in later computations:
    double celsius = myScanner.nextDouble();
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Looping Over and Over Again?
    By avalanche72 in forum Loops & Control Statements
    Replies: 1
    Last Post: February 1st, 2012, 05:11 AM
  2. Looping method problem.
    By Swen in forum What's Wrong With My Code?
    Replies: 24
    Last Post: December 29th, 2011, 09:49 AM
  3. Need help with looping!
    By crsoccerplayer6 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 29th, 2011, 04:09 PM
  4. For-looping, if-else statements, charAt(), etc. Beginner programming problem
    By ayelleeeecks in forum Loops & Control Statements
    Replies: 11
    Last Post: October 3rd, 2011, 12:54 PM
  5. Looping / OutOfBounds code problem
    By thisbeme in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 3rd, 2011, 07:10 AM