FileReader Issues
Hello. I've been working on this one code for a little, and I'm coming up with two errors that I just don't know how to fix. Here's the code.
// Java Document
import java.io.*;
publicclass fileio
{
publicstaticvoid main(String[] args)
{
int[] letter =newint[26];
int i;
int letternumber;
int index;
FileReader inputStream =new FileReader("/Users/alexdavis/Documents/gettysburgaddress.txt");
PrintWriter outfile =new PrintWriter("/Users/alexdavis/Documents/gettysburgaddress.txt");
String textfile = inputStream.toUpperCase();
textfile.replaceAll(" ","");
textfile.replaceAll(",","");
textfile.replaceAll(".","");
textfile.replaceAll("-","");
for (letternumber = 0; letternumber < 26; letternumber++)
{
for(index = 0; index <= textfile.length(); index++)
{
String letterindex = textfile.substring(index, index + 1);
int letterinteger = (int) letterindex - 65;
if (letterinteger == letternumber)
letter[letternumber]++;
}
}
for (i = 0; i < 26; i++)
outfile.println((char)(i+65) +"count = " + letter[i]);
outfile.close();
inputStream.close();
}
}
Here are the errors I am getting
alex-davis-computer:~ alexdavis$ javac fileio.java
fileio.java:16: cannot find symbol
symbol : method toUpperCase()
location: class java.io.FileReader
String textfile = inputStream.toUpperCase();
^
fileio.java:26: inconvertible types
found: java.lang.String
required: int
int letterinteger = (int) letterindex - 65;
Does anyone know how to fix these errors I'm getting? I think I've done most of the heavy lifting. Any help would be extremely extremely thanked and welcomed.
# 1
I don't know if this helps anything for you guys, but I added an IO exception.
// Java Document
import java.io.*;
public class fileio
{
public static void main(String[] args) throws IOException
{
int[] letter = new int[26];
int i;
int letternumber;
int index;
try{
FileReader inputStream = new FileReader("/Users/alexdavis/Documents/gettysburgaddress.txt");
PrintWriter outfile = new PrintWriter("/Users/alexdavis/Documents/gettysburgaddress.txt");
String textfile = inputStream.toUpperCase();
textfile.replaceAll(" ", "");
textfile.replaceAll(",", "");
textfile.replaceAll(".", "");
textfile.replaceAll("-", "");
for (letternumber = 0; letternumber < 26; letternumber++)
{
for(index = 0; index <= textfile.length(); index++)
{
String letterindex = textfile.substring(index, index + 1);
int letterinteger = (int) letterindex - 65;
if (letterinteger == letternumber)
letter[letternumber]++;
}
}
for (i = 0; i < 26; i++)
outfile.println((char)(i+65) + "count = " + letter[i]);
outfile.close();
inputStream.close();
}
catch(IOException e)
{
System.out.println("Cannot find file. Try again");
}
}
}
I am continually working on this, but if anyone can offer any suggestions on how to fix the errors, it would be extremely appreciated. Thanks.
-Alex Davis
# 2
First error is there is toUpperCase method in FileReader.Second problem is you are trying to case a String in to int (you cant do that)use Integer.parseInt method instead
# 3
Well, you're trying to call toUpperCase() on inputstream, which doesn't have that method and then you're using a String instead of a char for letterindex (why is it named letterindex anyway, it should be more like 'letter').
You need to read the text file into a String completely if you want to use toUpperCase and replaceAll. Also note that to use replaceAll, you need to write string = string.replaceAll(); since Strings are immutable.
And finally, instead of substring, use charAt.
# 4
You have to actually read the text file
and put it in a string with a custom made method:String textfile = readText(inputStream);
Where:String readText(FileReader inputStream) throws IOException {
BufferedReader br = new BufferedReader(inputStream);
SringWriter sw = new SringWriter();
PrintWriter pw = new PrintWriter(sw);
String line;
while ((line = br.readLine()) != null)
pw.println(line);
pw.flush();
return sw.toString().toUpperCase();
}
Also for the other error you should do:char letterindex = textfile.charAt(index);
Regards
# 5
LRMK is right.
The thing is that .toUpperCase can be only applied to the String.
I thin it is better first to read it to one string than use .toUpperCase method.
About next error .
Actually i did not get what are you trying to do. But it must be like this
int letterinteger = Integer.parseInt(letterindex) - 65;
# 6
I'm sorry, I should have specified. What I am trying to do is read the Gettysburg Address, which is in a txt file, eliminate all the spaces and punctuation, and see how many of each letter is present, then output the data one line at a time. It's suppose to be case insensitive, hence the touppercase.
# 7
OK, I have fixed a majority of this, and I have an output file there. For some reason, the input file is not reading how many different letters there are of each.
// Java Document
import java.io.*;
import java.util.*;
public class fileio
{
public static void main(String[] args) throws FileNotFoundException
{
int[] letter = new int[26];
int i;
int letternumber;
int index;
try{
Scanner inputStream = new Scanner(new FileReader("/Users/alexdavis/Documents/gettysburgaddress.txt"));
PrintWriter outfile = new PrintWriter("/Users/alexdavis/Documents/gettysburgoutput.txt");
String in;
in = inputStream.next();
String textfile = in.toUpperCase();
textfile = textfile.replaceAll(" ", "");
textfile = textfile.replaceAll(",", "");
textfile = textfile.replaceAll(".", "");
textfile = textfile.replaceAll("-", "");
for(index = 0; index < textfile.length(); index++)
{
letter[(int) textfile.charAt(index) - 65]++;
}
for (i = 0; i < 26; i++)
outfile.println((char)(i+65) + " count = " + letter[i]);
outfile.close();
inputStream.close();
}
catch(FileNotFoundException e)
{
System.out.println("Cannot find file. Try again");
}
}
}
I have a Mac, so that's why the FileReader is like that. If there are any Macs users around here, am I getting the path wrong?
