Question

In: Computer Science

Write a Java program that reads a list of 30 fruits from the file “fruits.txt”, inserts...

Write a Java program that reads a list of 30 fruits from the file “fruits.txt”, inserts them into a string array, and sorts the array in alphabetical order. String objects can be compared using relational operators such as <, >, or ==. For example, “abc” > “abd” is false, but “abc” < “abd” is true. Sample output: Before Sorting: Cherry, Honeydew, Cranberry, Lemon, Orange, Persimmon, Watermelon, Kiwifruit, Lime, Pomegranate, Jujube, Pineapple, Durian, Plum, Banana, Coconut, Apple, Tomato, Raisin, Mandarine, Blackberry, Raspberry, Peach, Mango, Melon, Grape, Strawberry, Blueberry, Pear, Avocado After Sorting: Apple, Avocado, Banana, Blackberry, Blueberry, Cherry, Coconut, Cranberry, Durian, Grape, Honeydew, Jujube, Kiwifruit, Lemon, Lime, Mandarine, Mango, Melon, Orange, Peach, Pear, Persimmon, Pineapple, Plum, Pomegranate, Raisin, Raspberry, Strawberry, Tomato, Watermelon

Fruit.txt:

Cherry
Honeydew
Cranberry
Lemon
Orange
Persimmon
Watermelon
Kiwifruit
Lime
Pomegranate
Jujube
Pineapple
Durian
Plum
Banana
Coconut
Apple
Tomato
Raisin
Mandarine
Blackberry
Raspberry
Peach
Mango
Melon
Grape
Strawberry
Blueberry
Pear
Avocado

Solutions

Expert Solution

Code:

=====

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class SortFruits {
   public static void main(String[] args) throws FileNotFoundException {
       Scanner sc = new Scanner(new File("fruits.txt"));
       String[] fruits = new String[30];
       int i=0;
       while(sc.hasNextLine()) {
           fruits[i] = sc.nextLine().trim();
           i++;
       }
       System.out.println("Fruits before soring: ");
       for(String fruit : fruits) {
           System.out.print(fruit+", ");
       }
       String temp="";
       for(i=0; i<30; i++) {
           for(int j=0; j<30; j++) {
               if(fruits[i].compareTo(fruits[j])<0) {
                   temp = fruits[j];
                   fruits[j] = fruits[i];
                   fruits[i] = temp;
               }
           }
       }
       System.out.println("\n\nFruits after soring: ");
       for(String fruit : fruits) {
           System.out.print(fruit+", ");
       }
       sc.close();
   }
}

output:

=====


Related Solutions

Write a Java program (single file/class) whose filename must be Numbers.java. The program reads a list...
Write a Java program (single file/class) whose filename must be Numbers.java. The program reads a list of integers from use input and has the following methods: 1. min - Finds the smallest integer in the list. 2. max- Finds the largest integer in the list. 3. average - Computes the average of all the numbers. 4. main - method prompts the user for the inputs and ends when the user enter Q or q and then neatly outputs the entire...
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
Write a program, which reads a list of student records from a file in batch mode....
Write a program, which reads a list of student records from a file in batch mode. Each student record comprises a roll number and student name, and the student records are separated by commas. An example data in the file is given below. · SP18-BCS-050 (Ali Hussan Butt), SP19-BCS-154 (Huma Khalid), FA19-BSE-111 (Muhammad Asim Ali), SP20-BSE-090 (Muhammad Wajid), SP17-BCS-014 (Adil Jameel) The program should store students roll numbers and names separately in 2 parallel arrays named names and rolls, i.e....
2. Write a Java program that reads a series of input lines from given file “name.txt”,...
2. Write a Java program that reads a series of input lines from given file “name.txt”, and sorts them into alphabetical order, ignoring the case of words. The program should use the merge sort algorithm so that it efficiently sorts a large file. Contents of names.text Slater, KendallLavery, RyanChandler, Arabella "Babe"Chandler, StuartKane, EricaChandler, Adam JrSlater, ZachMontgomery, JacksonChandler, KrystalMartin, JamesMontgomery, BiancaCortlandt, PalmerDevane, AidanMadden, JoshHayward, DavidLavery,k JonathanSmythe, GreenleeCortlandt, OpalMcDermott, AnnieHenry, DiGrey, MariaEnglish, BrookeKeefer, JuliaMartin, JosephMontgomery, LilyDillon, AmandaColby, LizaStone, Mary FrancesChandler, ColbyFrye, DerekMontgomery,...
Write a program in Java that reads a file containing data about the changing popularity of...
Write a program in Java that reads a file containing data about the changing popularity of various baby names over time and displays the data about a particular name. Each line of the file stores a name followed by integers representing the name’s popularity in each decade: 1900, 1910, 1920, and so on. The rankings range from 1 (most popular) to 1000 (least popular), or 0 for a name that was less popular than the 1000th name. A sample file...
In java Write a program called FileProcessor.java that reads the file numbers.txt, and: Calculate the total...
In java Write a program called FileProcessor.java that reads the file numbers.txt, and: Calculate the total number of numbers in the file, Calculate the sum of all the numbers in the file, Calculate the average of all the numbers in the file, Find the smallest value of all the numbers in the file, Find the largest value of all the numbers in the file, Calculate the standard deviation of all the numbers in the file
Module 1 Program Write a complete Java program in a file called Module1Program.java that reads all...
Module 1 Program Write a complete Java program in a file called Module1Program.java that reads all the lyrics from a file named lyrics.txt that is to be found in the same directory as the running program. The program should read the lyrics for each line and treat each word as a token. If the line contains a double (an integer is also treated as a double) it should use the first double it finds in line as the timestamp for...
Write a program that opens the file: "Lab6A_Data", reads all the values from the file, and...
Write a program that opens the file: "Lab6A_Data", reads all the values from the file, and calculates the following: A) The number of values in the file B) The sum of all the values in the file (a running total) C) The average of all the values in the file. D) The minimum value. E) The maximum value. F) The range of the data set of values. G) The number of times the value: '357' occurrs in the file. Display...
Write a java program that inserts 25 random integers from 0 to 100 in order into...
Write a java program that inserts 25 random integers from 0 to 100 in order into a LinkedList object. The program should sort the elements, then calculate the sum of the elements and the floating-point average of the elements.
IN JAVA Lab 10 This program reads times of runners in a race from a file...
IN JAVA Lab 10 This program reads times of runners in a race from a file and puts them into an array. It then displays how many people ran the race, it lists all of the times, and if finds the average time and the fastest time. In BlueJ create a project called Lab10 Create a class called Main Delete what is in the class you created and copy the Main class below into the file. There are 7 parts...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT