Writing a Program to calculate Circumference of planets reading from a file and writing to new file.

Writing a Program to calculate Circumference of planets reading from a file and writing to new file.

Hello, I would like to know how to write a program to read data from a file. Then use the numerical data within to then calculate the circumference of the planets. I then have to output the data into a new file.

The text file is Sun,860000 Mercury,4000 Venus,8000 Earth,8400 Mars,2500 Jupiter,135000 Saturn,100000 Uranus,75000 Neptune,70000 Pluto,2750

the integers represent the diameter. I have most of the code, but I am stumped when it comes to reading the file and writing to a new file. My code is as follows

package SBLab08;

import java.io.*;
import java.util.*;
import java.math.*;

/**
 *
 * @author ConfibulatoryMadness
 */
public class Main {

    public static void main(String[] args) throws IOException {

         Scanner input = new Scanner(System.in);
        int loop = 0;
        while(loop==0){
        System.out.print("\nPlease enter the name of the input file - ");
        String Input_File = input.nextLine();
         System.out.print("\nPlease specify the name of the output file - ");
        String Output_File = input.nextLine();

        if(Input_File.equalsIgnoreCase(Output_File)){
            System.out.print("\nThe output file must be different than the input one.");
        }else{ loop=1;}
        Scanner IF = new Scanner(new File(Input_File));
        while( IF.hasNext() ){
            String Seperator = (",");
            String lineFromFile = IF.nextLine();
            String[] partsOfLine = lineFromFile.split( "," ); // "," should be defined as a constant

        // Use partsOfLine[ 0 ] for the name.
        // Calculated the circumference as Math.PI * Integer.parseInt( lineFromFile[ 1 ] );
        // Both 0 and 1 can be defined as constants
        // Be sure NOT to calculate circumference twice because you need it for both the 
        // console and file I/O.

        // Perform file I/O and console I/O here.
    }          
        java.io.File New_Planet_Info = new java.io.File(Output_File);
        PrintWriter output = new PrintWriter(Output_File);
        System.out.print(IF);
        }

    }
}

Where I have the comments in my code is an area where I could use the most help. Please Help. Thank You Confibulatory Madness

View Answers

April 15, 2011 at 5:22 PM

import java.io.*;
import java.util.*;
import java.math.*;

public class Main {
    public static void main(String[] args) throws IOException {
        Scanner input = new Scanner(System.in);

        System.out.print("\nPlease enter the name of the input file - ");
        String Input_File = input.nextLine();
         System.out.print("\nPlease specify the name of the output file - ");
        String Output_File = input.nextLine();
        BufferedWriter bw=new BufferedWriter(new FileWriter(new File(Output_File),true));
BufferedReader br = new BufferedReader(new FileReader(new File(Input_File)));
    String strLine;
    ArrayList list=new ArrayList();
    while ((strLine = br.readLine()) != null){
    list.add(strLine);
    }
  Iterator itr;
    for (itr=list.iterator(); itr.hasNext(); ){
  String str=itr.next().toString();  
    String [] splitSt =str.split(",");
    String planet="",radius="";
      for (int i = 0 ; i < splitSt.length ; i++) {
         planet=splitSt[0];
         radius=splitSt[1];
    }
    double r=Double.parseDouble(radius);
    double circumference=2*3.14*r;
    bw.write(Double.toString(circumference));
    bw.newLine();
}
bw.close();
        }

}

April 15, 2011 at 6:08 PM

That is most of it, but I need to also output to the user the new data included with the original data. and for 3.14 can i just use pi since I imported the math package?
My output needs to look like this:

   NAME         DIAMETER           CIRCUMFERENCE
    Sun           860000             2701770
Mercury             4000               12566
  Venus             8000               25133
  Earth             8400               26389
   Mars             2500                7854
Jupiter           135000              424115
 Saturn           100000              314159
 Uranus            75000              235619
Neptune            70000              219911
  Pluto             2750                8639


I wish this thing would display my text just like it is, it almost looks like a table. My teacher is really anal on detail.

April 15, 2011 at 6:33 PM

I also got this error when running the file

Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Scanner.java:1516) at SBLab08.main(SBLab08.java:35) Java Result: 1

which refers to

while ((strLine = sc.nextLine()) != null){

Ads









Related Tutorials/Questions & Answers:
Writing a Program to calculate Circumference of planets reading from a file and writing to new file.
Writing a Program to calculate Circumference of planets reading from a file... to then calculate the circumference of the planets. I then have to output the data into a new... stumped when it comes to reading the file and writing to a new file. My code
Writing a Program to calculate Circumference of planets reading from a file and writing to new file.
Writing a Program to calculate Circumference of planets reading from a file... to then calculate the circumference of the planets. I then have to output the data into a new... it comes to reading the file and writing to a new file. My code is as follows
Advertisements
Writing to and reading from a binary file in java.
Writing to and reading from a binary file in java.  I have written... the binary file from another program as follows: m_dis = new DataInputStream... work, but the records obtained after reading the binary file are much less than
Writing and Reading A File
Writing and Reading A File  Hello, I've been trying to learn writing and reading data from file for our assignment, but just stuck on how to proceed...(ActionEvent ae){ File file=new File("Employee.txt"); FileInputStream fis = null
reading multiple files from a directory and writing them into a single file
reading multiple files from a directory and writing them into a single file... this: /*this program reads multiple files * from a single directory. */ package elite.tech.com... static void main(String[]args) throws IOException{ File Folder = new
File saving and Writing
File saving and Writing   Hello Rose india..... I have a doubt... a "file create option" it will be ".csv" file and i want to write the data from arraylist into .csv file and i want to save that file so it will ask for "save
without writing web.xml file we can write servlet program
without writing web.xml file we can write servlet program  Sir Morning... Sir I have one Question "without writing web.xml file we can write servlet program". if yes which way? if no why? but without use Annotation........ Plz
writing and appending to a file
writing and appending to a file  My input file includes data from... and values and writes into an existing file for 'male'. How can I do the same for females and write into a different 'female string' in the same program? Sorry If I
accessing data from a json file writing javascript or using jQuery
accessing data from a json file writing javascript or using jQuery  I... page is built from this json file, but I can't figure out how to code... correctly accessing the attributes in the json file. Nothing is working. I'm new
Reading And Writing Excel File
reading and writing excel file   ... words, we can say that it is used to read the excel file. After reading the excel... file and write it into other excel file by this way. You can add new cell
counting the values in input file and and writing the output to a file
counting the values in input file and and writing the output to a file  Can any one please correct this code. This is to read a file and later...-code/16483-counting-values-input-file-writing-output-file.html this code
Writing and reading from/to a serialized file through Hash Table in Java
Writing and reading from/to a serialized file through Hash Table in Java... illustrates you how to read and write from/to a serialized file through the hash table.... Following program has the facility if the specified serialized file does not exist
Writing Log Records to a Log File
Writing Log Records to a Log File       This section demonstrates for writing log records to a log file. Logger provides different types of level like: warning, info
writing a text into text file at particular line number
writing a text into text file at particular line number   Hi, thanks for quick response, I want to insert text at some particular line number.. after line number four my text will display in text file using java program
How to access the image file from project folder itself while writing to pdf using itext?
How to access the image file from project folder itself while writing to pdf using itext?  I am writing a pdf using itext where i add a image... it to pick it up from the project folder itself.Because later i deploy the project
writing a text into text file at particular line number
writing a text into text file at particular line number   Hi, thanks..... after line number four my text will display in text file using java program  ... = ""; int lineNo; try { File f=new File("c
writing a text into text file at particular line number
writing a text into text file at particular line number   Hi, thanks..... after line number four my text will display in text file using java program  ... = ""; int lineNo; try { File f=new File("c
Reading string from file timed
Reading string from file timed  So I want to make a file reader/ buffered reader that reads a new line of the textfile, lets say every 30 second. Like it reads the first line, waiting 30 seconds, read the next line and so one
writing document (.doc) file from java with format such as font type, font style
writing document (.doc) file from java with format such as font type, font... = new File("C:/sample.doc"); POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file)); HWPFDocument doc = new HWPFDocument(fs); Range range
Reading a file from Jar JAVA
Reading a file from Jar JAVA  I have added one excel sheet into a jar file. can anybody tell me how i can read that file. actually when i am running code from eclipse i able to read it but when i am adding that jar file
Reading a file from Jar JAVA
Reading a file from Jar JAVA  I have added one excel sheet into a jar file. can anybody tell me how i can read that file. actually when i am running code from eclipse i able to read it but when i am adding that jar file
problem of writing to a local file ( JApplet ) - Applet
problem of writing to a local file ( JApplet )  Dear All, I want to program a guestbook using java applets but now I have problem of writing to a file from JApplet. it is working without any problem if i run the program using
Writing for the Purpose of Reading
Writing for the Purpose of Reading   ... of such an operation can vary from browser to browser. - The texts should be organized... disoriented. - The advantage of web writing is that a greater number of lists
reading dropdown values from properties file in jsp
reading dropdown values from properties file in jsp  reading dropdown values from properties file in jsp
reading a csv file from a particular row
reading a csv file from a particular row  how to read a csv file from a particular row and storing data to sql server by using jsp servlet
Reading a text file in java
in java.io.* package for reading and writing to a file in Java. To learn more about reading text file in Java see the tutorial Read File in Java. Thanks...Reading a text file in java  What is the code for Reading a text file
reading data from excel file and plotting graph
reading data from excel file and plotting graph  I am doing a project...); try { ChartUtilities.saveChartAsJPEG(new File("C... at the same time from one excel file using different columns..how can i do that? waiting
jar file not reading input from serial port
jar file not reading input from serial port  i used a coding for getting data from serial port. JOptionbox with "port not found" message is shown when i execute thru jar file. but when i execute thru netbeans, data is received
jar file not reading input from serial port
jar file not reading input from serial port  i used a coding for getting data from serial port. JOptionbox with "port not found" message is shown when i execute thru jar file. but when i execute thru netbeans, data is received
Writing to File in Java
Writing to File in Java Writing any character or text in Java file is possible... Class FileWriter accedes from the OutputStreamWriter class, used for writing...) This is the simplest example of the class used for writing character file. BufferedWriter
writing angularjs hello world program
tutorials. I want to make simple program and thinking to start from the Hello World program in AngularJS. Searching for simple example for writing angularjs...writing angularjs hello world program  Hi, I just started to learn

Ads