The example below is that I got a number 6/7 from the fractions.txt and I split
ID: 3801058 • Letter: T
Question
The example below is that I got a number 6/7 from the fractions.txt and I split the numerator(6) and denominator(7). Then I save it to a terminal.txt. Then I want to read 6 and 7 from terminal.txt. How to do that?
I got some problem with my coding. Can you fix it for me?
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.PrintWriter;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
public class test
{
public static void main(String[]args){
Scanner inputStream = null;
PrintWriter outputStream = null;
try{
inputStream = new Scanner( new FileInputStream ("fractions.txt"));
outputStream = new PrintWriter (new FileOutputStream("terminal.txt"));
}
catch (FileNotFoundException e){
System.out.println("File not found.");
System.exit(0);
}
String data;
int count=0;
while (inputStream.hasNextLine()){
data = inputStream.nextLine();
count++;
System.out.println(data);
System.out.println(count);
for(String w:data.split("/",0)){
outputStream.println(w);
}
}
Scanner inputStream1 = null;
try{
inputStream1 = new Scanner( new FileInputStream ("terminal.txt"));
}
catch (FileNotFoundException e){
System.out.println("File not found.");
System.exit(0);
}
int [] denominator = new int [count];
int [] numerator = new int [count];
int i=0;
int j=0;
do {
if (inputStream1.hasNextLine()){
numerator [i++] = inputStream1.nextInt();
denominator [j++] = inputStream1.nextInt();}
}while (i<count && j<count);
System.out.println(numerator[0]);
System.out.println(denominator[0]);
outputStream.close();
inputStream.close();
inputStream.close();
}
}
Explanation / Answer
public class PrimeSieve { public static void main(String[] args) { int n = Integer.parseInt(args[0]); // initially assume all integers are prime boolean[] isPrime = new boolean[n+1]; for (int i = 2; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.