Question

Java console-based question. Do not use BufferedReader please. A file named customers.txt contains the names of...

Java console-based question.

Do not use BufferedReader please.

A file named customers.txt contains the names of customers for a local hairdressing salon. Unfortunately, the data in the file is in no particular order yet the owner of the salon would like the data in alphabetical order. Write a Java program that reads the contents of the file into an array of Strings, sorts the array of Strings into alphabetical order, and then writes the content of the array to a file named sortedCustomers.txt.

customers.txt contains one name per line and may contain data similar to:

      Smith, Alexandra
      Downes, Trish
      Akbal, Maria

Note the following:

  • You cannot assume how many lines of text are in the file.

  • You cannot assume that the file exists.

  • When storing the names in the array you may assume an array size of 500 elements.

  • When writing the names to the sortedCustomer.txt file there should be one name per line.

    For example:

          Akbal, Maria
          Downes, Trish
          Smith, Alexandra
    
  • Your code must use appropriate methods with parameter passing to solve the question.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code


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


public class Main
{
public static void main(String[] args)
{
String []names=new String[500];
int countNames=0;
try
{          
Scanner scanner = new Scanner(new File("customers.txt"));
while (scanner.hasNextLine())
{
       names[countNames]=scanner.nextLine();
countNames++;
}
scanner.close();
   }
catch (FileNotFoundException e)
{
System.out.println("File not found. Pleae check.");
   }
String temp;
for (int i = 0; i <= countNames - 1; i++)
{
for (int j = 1; j < countNames ; j++)
{ //Apply the bubble Sort
if (CompareString(names[j - 1], names[j]) == 1)
{ //Pass the two adjacent string for comparing
temp = names[j - 1];
names[j - 1] = names[j];
names[j] = temp;
}
}
}

File file = new File("sortedCustomer.txt");
FileWriter fr = null;
try
{
fr = new FileWriter(file);
for(int i=0;i<countNames;i++)
{
fr.write(names[i]+"\r\n");
}
fr.close();
} catch (IOException e)
{
e.printStackTrace();
}
System.out.println("Done!!!");
}
private static int CompareString(String first, String second) {
int len;

if (first.length() >= second.length()) //we need to take the smallest string length
len = second.length();
else
len = first.length();

for (int i = 0; i <= len; i++) {
if (first.charAt(i) > second.charAt(i)) //Suppose the first string letters is greater then return 1;
return 1;
else if (first.charAt(i) < second.charAt(i)) //if second string letter is greater then return -1;
return -1;
}
return 0; //if both the equal then return 0
}
}

output

text files

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
Java console-based question. Do not use BufferedReader please. A file named customers.txt contains the names of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as...

    Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as a String and returns each line in the file in an array. Have each element of the array be a String. Do not include the newline at the end of each line. Use a BufferedReader object to read the file contents. Note, the contents of input.txt will be different (including the number of lines) for each test case. Example: getLines( "input.txt" ) returns (an...

  • Design a Java class named StringQueue for storing strings of first names into a queue. The...

    Design a Java class named StringQueue for storing strings of first names into a queue. The StringQueue.java class contains: * A String[] data field named names that stores the String values in the queue. * A private data field named size that stores the number of names in the queue. * A public static final data field named DEFAULT_CAPACITY = 10 for the array. * A private static data field named firstInitCtr that counts the number of names with the...

  • Please run the program and show the result with screenshots after. Thank you (Java Eclipse) Customer...

    Please run the program and show the result with screenshots after. Thank you (Java Eclipse) Customer Data: Area Codes Assume you work for a company that tracks customer information, including name, gender and phone numbers Your company has a file called customers.txt which contains the following information: Jiming Wu F 4082123458 James Brown M 8315678432 Leanna Perez F 4087654433 Xing Li M 8313214555 Stacey Cahill O 8312123333 Mohammed Abbas M 4083134444 Kumari Chakrabarti F 4086667777 Shakil Smith M 4082123333 Jung...

  • Homework 1- Merge Files: 1. Design a class named MergeFiles 1 Three file names are passed...

    Homework 1- Merge Files: 1. Design a class named MergeFiles 1 Three file names are passed as command-line arguments to MergeFiles as follows: java MergeFiles filel file2 mergedFile II. MergeFiles reads names stored in files file and file2 III. Merges the two list of names into a single list IV. Sorts that single list V. Ignores repetitions VI. Writes the sorted, free-of-repetitions list to a new file named mergedFile.txt VII. The names in all three files are stored as one...

  • Java programming question: Here is the feedback I received "sort fails. Use Arrays class method to...

    Java programming question: Here is the feedback I received "sort fails. Use Arrays class method to sort." The actual question: Write a program as follows: Declare a five element array of Strings. Use a for loop and keyboard input to populate the array with the names of five friends. Sort the array in alphabetical order. Use a foreach loop to print the sorted array of friends, all on one line. Sample Output (inputs in italics) Enter the names of five...

  • Step 1: Getting Started Create a new .java file named Lab12.java. At the beginning of this...

    Step 1: Getting Started Create a new .java file named Lab12.java. At the beginning of this file, include your assignment documentation code block. After the documentation block, you will need several import statements. import java.util.Scanner; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; Next, declare the Lab12 class and define the main function. public class Lab12 { public static void main (String [] args) { Step 2: Declaring Variables For this section of the lab, you will need to declare...

  • Write a java project that reads a sequence of up to 25 pairs of names and...

    Write a java project that reads a sequence of up to 25 pairs of names and postal (ZIP) codes for individuals (sample input data is attached). Store the data in an object designed to store a first name (string), last name (string), and postal code (integer). Assume each line of input will contain two strings followed by an integer value, each separated by a tab character. Then, after the input has been read in, print the list in an appropriate...

  • Write you code in a class named HighScores, in file named HighScores.java. Write a program that...

    Write you code in a class named HighScores, in file named HighScores.java. Write a program that records high-score data for a fictitious game. The program will ask the user to enter five names, and five scores. It will store the data in memory, and print it back out sorted by score. The output from your program should look approximately like this: Enter the name for score #1: Suzy Enter the score for score #1: 600 Enter the name for score...

  • Write a program in Java according to the following specifications: The program reads a text file...

    Write a program in Java according to the following specifications: The program reads a text file with student records (first name, last name and grade on each line). Then it prompts the user to enter a command, executes the command and loops. The commands are the following: "print" - prints the student records (first name, last name, grade). "sortfirst" - sorts the student records by first name. "sortlast" - sorts the student records by last name. "sortgrade" - sorts the...

  • Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class...

    Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class should contain the following data fields and methods (note that all data and methods are for objects unless specified as being for the entire class) Data fields: A String object named firstName A String object named middleName A String object name lastName Methods: A Module2 constructor method that accepts no parameters and initializes the data fields from 1) to empty Strings (e.g., firstName =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT