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...
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 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...
Using Java Project 2: Deduplication Write a program that reads a file of numbers of type...
Using Java Project 2: Deduplication 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...
Write a JAVA program that reads a text file into RAM efficiently, takes a regular expression...
Write a JAVA program that reads a text file into RAM efficiently, takes a regular expression from the user, and then prints every line that matches the RE.
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. In paragraph 1 Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. In pragraph 2 Replace "We" with v"i" This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would...
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. Replace "sh" with ph This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would not do that Paragraph 2 We...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT