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
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();
}
}
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.
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){