I am trying to take in a text file (UTF-8) and store/print it with the following constraints (see example below):
I first tried using scanner to read until the end of the file. I converted the string read to a char array but could not make it lowercase correctly (see commented out code). I have tried using BufferedReader, but can't get the correct changes. I tried reading in by character and using the character class to set a lowercase restriction (see code below).
public static void OpenPlainText(File plainTextFile)
{
try
{
Scanner scanner = new Scanner(plainTextFile);
BufferedReader br = new BufferedReader(new InputStreamReader
(new FileInputStream (plainTextFile), Charset.forName("UTF-8")));
StorePlainText(scanner, br);
scanner.close();
br.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void StorePlainText(Scanner scanner, BufferedReader br) throws IOException
{
/*String str = "";
while (scanner.hasNext())
{
str += scanner.next();
}
str.toCharArray();
str.toLowerCase();
System.out.println(str);*/
int c;
while ((c = br.read()) != -1)
{
char character = (char) c;
if (Character.isLetter(character))
{
if (Character.isUpperCase(character))
{
Character.toLowerCase(character);
System.out.print(character);
}
}
}
}
Ex. Text File
Art of Computer Programming, Volume 1, Fascicle 1, The: MMIX -- A RISC Computer for the New Millennium
This multivolume work on the analysis of algorithms has long been recognized as the definitive description of classical computer science.
Ex. Store/Print
artofcomputerprogrammingvolumefasciclethemmixarisccomputerforthenewmillenniumthismultivolumeworkontheanalysisofalgorithmshaslongbeenrecognizedasthedefinitivedescriptionofclassicalcomputerscience
Character.toLowerCase
returns the lower case character it does not convert the one you pass in.
while ((c = br.read()) != -1)
{
char character = (char) c;
if (Character.isLetter(character))
{
if (Character.isUpperCase(character))
{
character = Character.toLowerCase(character);
System.out.print(character);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.