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: Writing data get from methods to txt file

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

    Default Writing data get from methods to txt file

    Hi,
    I have written a simple converter from Celsius to Fahrenheit and vise versa. I want write to file what conversion was chosen by user, what value was entered and result.
    There is my code:
    User input class
    package Converter.learning;
     
    //This class handles user input
     
     
    import java.util.Scanner;
     
    public class UserInput {
     
    	public UserInput(){
    		//dataInput();
    	}
     
    	//declaring variables
    	float temp;
    	//Creating scanner for reading user input
    	Scanner inputTemp= new Scanner(System.in);
     
     
    	//Method that does handles input
    	public float dataInput(){
     
    		System.out.println("Enter a numer");
     
    		//Checking if an input is a float
    		while (!inputTemp.hasNextFloat()){
    			System.out.println("You have entered wrong type. Please enter number!");
    			inputTemp.next();
    		}
    		temp = inputTemp.nextFloat();
    		//Closing Scanner
    		inputTemp.close();
    		return temp ;
    	}
     
     
     
    }

    Conversion Class

    package Converter.learning;
     
    //This class converts C to F and vice versa
     
    public class Conversion {
    	//declaring variables
    	float result; 
    	float C;
    	float F;
    	//Constructor
    	public Conversion(){
    		//convertToC(F);
    		//convertToF(C);
    		//print();
     
    	}
    	//Method that does conversion from C to F 
    	public float convertToF (float C){
    		return result = C * 9/5 + 32;
    	}
     
    	//Method that does conversion from F to C 
    		public float convertToC (float F){
    			return result = (F - 32) * 5/9;
    		}
     
    		public void print(){
    			System.out.println("Result is: " + result);
    		}
    }

    Main Class
    package Converter.learning;
     
     
    import java.util.Scanner;
     
    /*
     * Program is a simple converter that converts temperature. Celsius to
     * Fahrenheit and oposite. 
     * Version 3.4 
     */ 
     
     
    public class Main {
     
    	public static void main(String[] args) {
    		String s ;
    		UserInput input = new UserInput();
    		Conversion conversion = new Conversion();
    		System.out.println("To convert Celsius to Fahrenheit press C, To convert Fahrenheit to Celsius press F ");
    		//Creating a scanner that enables conversion mode
    		Scanner scan = new Scanner(System.in);
    		while(!scan.hasNext("[CF]")){
    			System.out.println("wrong input, please enter C or F");
    			scan.next();
    			//mySet.add();
    		}
    		//mySet.add(s = scan.next());
    		s = scan.next();
    		switch(s){
    		case "C":
    			System.out.println("Conversion To Fahrenheit");
     
    			conversion.convertToF(input.dataInput());
    			conversion.print();
    			scan.close();
    			break;
    		case "F":
    			System.out.println("Conversion To Celsius");
    			conversion.convertToC(input.dataInput());
    			conversion.print();
    			scan.close();
     
    			break;
    		default:
    			break;
    		}
     
     
     
    		//input.dataInput();
    		//System.out.println(input.temp);
     
     
     
    	}
     
    }

    Do you have any ideas?


  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: Writing data get from methods to txt file

    Welcome to the Forum! Please read this topic to learn useful tips for newcomers.

    It's not clear what help you need. Can you focus on what you need help with and ask specific questions? How do I . . . ? Here's what I tried: I'm getting the following results, errors, etc. but I want this to happen.

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

    Default Re: Writing data get from methods to txt file

    Quote Originally Posted by GregBrannon View Post
    Welcome to the Forum! Please read this topic to learn useful tips for newcomers.

    It's not clear what help you need. Can you focus on what you need help with and ask specific questions? How do I . . . ? Here's what I tried: I'm getting the following results, errors, etc. but I want this to happen.
    I am trying to write data to file. For example: want temperature was entered and what results I get. I have method void print() in Class Conversion and I want to get information from method void print() and put in a file. I have already written Class that writes to files but I don't know how to integrate into my existing code.
    Here Class that should write data to file
    package Converter.learning;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.io.IOException;
     
     
    public class ManipulatingFiles {
     
    	String myFile ="file.txt";
    	String text = "Bandymas1_";
    	File f = new File("/home/dainius/", myFile);	
    	public void createFile() {
     
    		//
     
    		if(!f.exists()){			
    			try {
    				f.mkdir();
    				f.createNewFile();
    				System.out.print("Success");
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}else{
    		System.out.println("File already exits, overding it");
     
    	}
    }
    	public boolean writeToFile() throws IOException{
    		FileWriter writer = new FileWriter(f);
    		writer.write(text);
    		writer.flush();
    		writer.close();
    		return true;
    	}

  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: Writing data get from methods to txt file

    Using a class:

    Create an instance of it, as in

    ManipulatingFiles manFiles = new ManipulatingFiles();

    Then use the instance as needed to access the methods:

    manFiles.writeToFile( textToWriteToFile );

    Your method, writeToFile(), accepts no parameters, so is not very useful. Try rewriting the method to accept the data you want to write to the file.

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

    SkylineDiT (February 12th, 2014)

  6. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Writing data get from methods to txt file

    Quote Originally Posted by GregBrannon View Post
    Using a class:

    Create an instance of it, as in

    ManipulatingFiles manFiles = new ManipulatingFiles();

    Then use the instance as needed to access the methods:

    manFiles.writeToFile( textToWriteToFile );

    Your method, writeToFile(), accepts no parameters, so is not very useful. Try rewriting the method to accept the data you want to write to the file.
    Thank you. I modified method writeToFile. And now it is working.

  7. #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: Writing data get from methods to txt file

    Glad to help.

Similar Threads

  1. Writing to a txt file from the top.
    By sakonpure6 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: December 21st, 2013, 08:39 PM
  2. Replies: 1
    Last Post: October 30th, 2013, 09:55 PM
  3. Multiple Static Methods and printing to different .txt file?
    By Clockwork in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 17th, 2013, 09:11 PM
  4. Advice on removing duplicates from a .txt file while writing to an array
    By shakesgar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 19th, 2013, 03:09 PM
  5. Output some data from a txt file?
    By j4ze in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: April 24th, 2012, 07:20 AM