Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You've been hired by Alfs Aeronautics to write a Java console application that reads, sorts, and writes planet data. Create text file PlanetsIn.txt and paste

You've been hired by Alfs Aeronautics to write a Java console application that reads, sorts, and writes planet data. Create text file PlanetsIn.txt and paste this data into it:

Planet Diameter (miles) Length of Day (hours)

Mercury 3032 4222.6

Venus 7521 2802.0

Earth 7926 24.0

Moon 2159 708.7

Mars 4221 24.7

Jupiter 88846 9.9

Saturn 74897 10.7

Uranus 31763 17.2

Neptune 30775 16.1

Pluto 1485 153.3

Here is the file specification for PlanetsIn.txt:

Field Type Start-End

Planet string 1-14

Diameter (miles) integer 15-21

Length of Day (hours) real 22-40

This file contains a header row.

Read and parse the data into three parallel arrays: planets, diameters, and lengths. Create method printArrays to print the unsorted data including the header row. Sort the data by diameter using a bubble sort, and use method printArrays to print the sorted data including the header row. Note that when data is sorted by diameter and values are swapped in that array, corresponding values must also be swapped in the other two arrays so that the name and length stay with the correct diameter. Write the header row and sorted data per the file specification to text file PlanetsOut.txt file.

I am not able to read fromthe text file which is on the desktop.

import java.io.*;

import java.util.*;

private static final String input = ("C:/Users/Desktop/PlanetsIn.txt");

static String planets[];

static int diameters[];

static double days[];

static final int LEN = 10;

static void printArrays(){

System.out.println("Planet Diameter (miles) Length of Day (hours)");

for(int i=0;i

System.out.println(planets[i]+" "+diameters[i]+" "+days[i]);

}

}

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

BufferedReader br = new BufferedReader(new FileReader(new File(input)));

String str = br.readLine();

planets = new String[LEN];

diameters = new int[LEN];

days = new double[LEN];

int ind = 0;

while((str=br.readLine())!=null){

// System.out.println(str);

StringTokenizer stk = new StringTokenizer(str);

String name = stk.nextToken();

int diameter = Integer.parseInt(stk.nextToken());

double day = Double.parseDouble(stk.nextToken());

planets[ind] = name;

diameters[ind] = diameter;

days[ind] = day;

ind++;

}

printArrays();

//Performing bubble sort

for(int i=0;i

for(int j=1;j

if(diameters[j]

int t = diameters[j];

diameters[j] = diameters[j-1];

diameters[j-1] = t;

String n = planets[j];

planets[j] = planets[j-1];

planets[j-1] = n;

double d = days[j];

days[j] = days[j-1];

days[j-1] = d;

}

}

}

System.out.println(" After sorting");

printArrays();

BufferedWriter bw = new BufferedWriter(new FileWriter(new File("C:/Users/Desktop/PlanetsOut.txt")));

bw.write("Planet Diameter (miles) Length of Day (hours) ");

for(int i=0;i

bw.write(planets[i]+" "+diameters[i]+" "+days[i]+" ");

}

bw.flush();

bw.close();

}

}

I also tried C:Users/my user name/desktop/PlanetsIn.txt" and it is also not working. How can i fix this? both files are created on the desktop.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Lab Manual For Database Development

Authors: Rachelle Reese

1st Custom Edition

1256741736, 978-1256741732

More Books

Students also viewed these Databases questions

Question

5. How do instructional objectives help learning to occur?

Answered: 1 week ago