JAVA String !!Help!!
Could someone assist me in solving this problem?
Requirements:
In this project, you will work with Java String. You are to write a program that does the following tasks:
1) Ask the user for the name of the input file and the name of the output file
2) Read a string from the input file (assume that the string is there)
3) Determine its reverse string and write both the original string and its reverse to the output file
4) Determine the number of non-duplicate characters from the original string, then write this number and those characters into the output file
Note that white spaces such as blanks do not count as a character, and lower-case character is the same as its upper-case character.
For example:
Input String Reverse String Number of non-duplicate characters
ABCD DCBA 4 (A, B, C, D)
Abcd dcbA 4 (A, B, C, D)
Aa Bb bB aA 2 (A, B)
Please use the following data to test your program:
Aa Bb
Code:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Files
{
public static void main(String args[])
{
try
{
// open a input file to read the contents
BufferedReader in = new BufferedReader (new FileReader("in.txt"));
// open a output file to read the contents
BufferedWriter out = new BufferedWriter (new FileWriter("Tes1.txt", false));
// declare an array to keep track of non-duplicate characters
boolean alpha[] = new boolean[26];
String str = "";
do
{
// read a line from input file
str = in.readLine();
// break upon no data
if (str == null)
break;
else
{
// initialize the non-duplicate array
for (int i = 0; i < 26; i++ )
alpha[ i ] = false;
int count = 0; // initialize the count to zero
// find the length of the string
int l = str.length();
// write the string to the output file
out.write (str + "\t\t");
// declare a character array and allocate memory to it
char rev[] = new char[ 1+1 ];
// get the reverse of the string
for (int i = 0; i < 1; i++)
{
// get characters of the string from the right to left
rev[ i ] = str.charAt( 1-i-1 );
// write the characters to the output file
out.write(rev[ i ]);
// ignore the white space characters
if (Character.toUpperCase ( rev[ i ]) < 65)
// check for the duplication and if not increase count
if (alpha[Character.toUpperCase (rev[ i ]) - 65 ] == false )
{
alpha [Character.toUpperCase (rev[ i ]) - 65 ] = true;
count++;
} // end if
} // end for
// write the count to the output file
out.write ("\t\t" + count + " ( " );
// write all the non-duplicate characters to the output file
for (int i = 0; i < 26;i++)
if (alpha[ i ] == true)
out.write( (char) (i+ 65) + ", ");
out.write (")\n");
} // end else-if
} while (true); // continue to read one more string from input file
// close the input and output file
in.close();
out.close();
} // end try
catch (IOException e)
{
}
} // end method main
} // end class Files

