Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You\'ve been hired by Alf’s Aeronautics to write a Java console application that

ID: 3802101 • Letter: Y

Question

You've been hired by Alf’s 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<LEN;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<LEN;i++){

       for(int j=1;j<LEN-i;j++){

       if(diameters[j]<diameters[j-1]){

       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<LEN;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.

Explanation / Answer

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Surya
*/
import java.io.*;

import java.util.*;
public class planets {
  
    //you should try like this...
        //C:user/accountname/Desktop/PlanetOut.txt
        //that is
       //C:Users/my user name/Desktop/PlanetsIn.txt"
            //in Desktop, d should be Capital D
    private static final String input = ("C:/Users/Surya/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<LEN;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<LEN;i++){

        for(int j=1;j<LEN-i;j++){

        if(diameters[j]<diameters[j-1]){

        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();
      
        br.close();//modified
      
      
        //you should try like this...
        //C:user/accountname/Desktop/PlanetOut.txt
        //that is
       //C:Users/my user name/Desktop/PlanetsIn.txt"
        //in Desktop, d should be Capital D

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

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

        for(int i=0;i<LEN;i++){

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

        }

        bw.flush();

        bw.close();

        }

      

      
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote