* _Example commands for running this file_
* Compilation: javac Assignment1.java
* Execution: java Assignment1 < input.txt
*
* Reads in a text file and for each line verifies
whether the word has
* unique characters.
*
* % cat input.txt
* Hello
* World
*
* % java Assignment1 < input.txt
* False
* True
*
******************************************************************************/
import java.util.*;
public class SortInput {
/* a function for checking uniqueness of characters in a word
*/
private static boolean isUniqueChar(String s){
// Fill this part out
System.out.println(true);
return true;
}
/* a function for sorting an input word */
private static String sortWord(String s){
char[] c = s.toCharArray();
insertionSort(c);
return "";
}
/* insertion sort algorithm. It should return a string type
*/
public static void insertionSort(char[] word) {
if (word == null || word.length == 0)
return; // empty array have nothing to sort
for (int i=0; i < word.length; i++) {
char temp = word[i];
int j = i;
while (j > 0 && word[j-1] > temp) {
word[j] = word[j-1];
j--;
}
word[j] = temp;
}
return;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// read in words and determine whether it is composed of unique
characters
while (scanner.hasNextLine()) {
String s = scanner.nextLine();
isUniqueChar(s);
sortWord(s);
}
scanner.close();
}
}
THE OUTPUT SHOULD LOOK LIKE THIS:
You can reference the Java Docs or anything on the Internet.
Instructions for usage by command-line can be found in the Example.java.
You may use input.txt as a test case. The contents of the input.txt file:
hello world Brooklyn Queens
The output for that input file should look like:
false ehllo true dlorw false bklnoory false ceegllo
Format
In each line you have the true/false value followed by a tab space, followed by the word in sorted order (lowercase).
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class SortInput {
/* a function for checking uniqueness of characters in a word
*/
private static boolean isUniqueChar(String s){
// Fill this part out
s = sortWord(s);
for(int i=0;i<s.length()-1;i++)
{
if(s.charAt(i)==s.charAt(i+1))
{
return
false;
}
}
return true;
}
/* a function for sorting an input word */
private static String sortWord(String s){
char[] c = s.toCharArray();
return insertionSort(c);
}
/* insertion sort algorithm. It should return a string type
*/
public static String insertionSort(char[] word) {
if (word == null || word.length == 0)
return "";// empty array have nothing to sort
for (int i=0; i < word.length; i++) {
char temp = word[i];
int j = i;
while (j > 0 && word[j-1] > temp) {
word[j] = word[j-1];
j--;
}
word[j] = temp;
}
String result = new String(word);
return result.toLowerCase();
}
public static void main(String[] args) {
File file = new File("input.txt");
Scanner scanner = null;
try {
scanner = new Scanner(file);
} catch (FileNotFoundException e) {
System.out.println("FileNotFound: Check with you input
file");
e.printStackTrace();
}
// read in words and determine whether it is composed of unique
characters
while (scanner.hasNextLine()) {
String s = scanner.nextLine();
System.out.print(isUniqueChar(s));
System.out.println("\t"+sortWord(s));
}
scanner.close();
}
}
Get Answers For Free
Most questions answered within 1 hours.