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 (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 Java program that reads words from a text file and displays all the non-duplicate...
Write a Java program that reads words from a text file and displays all the non-duplicate words in ascending order. The text file is passed as a command-line argument. Command line argument: test2001.txt Correct output: Words in ascending order... 1mango Salami apple banana boat zebra
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
Using Java Write a program that reads a file of numbers of type int and outputs...
Using Java Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the output file should...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT