My problem is that I have a code and I have a problem of fixing the "Cannot find symbol" error. Here's the code.
public static void writer() throws IOException {
FileReader in = null;
FileWriter out = null;
BufferedReader br = null;
BufferedWriter bw = null;
try {
in = new FileReader("Student.txt");
out = new FileWriter("StudentAvg.txt");
br = new BufferedReader(in);
bw = new BufferedWriter(out);
String[] line = new String[28];
line = in.split("\t");
The error is pointed on the splitting process. Is there a problem with my variables?
The problem is, that you haven't read any lines.
br = new BufferedReader(in);
bw = new BufferedWriter(out);
String[] line = new String[28];
line = in.split("\t"); // <-- this is your input file reader.
I think you wanted
br = new BufferedReader(in);
bw = new BufferedWriter(out);
String fromFile;
while ((fromFile = br.readLine()) != null) {
String[] line = fromFile.split("\t");
You might also use a try-with-resources and something like
try (BufferedReader br = new BufferedReader(//
new FileReader("Student.txt"));
BufferedWriter bw = new BufferedWriter(//
new FileWriter("StudentAvg.txt"))) {
String fromFile;
while ((fromFile = br.readLine()) != null) {
String[] line = fromFile.split("\t");
}
}
Related
I have this code but when i press the button its edit only the first line in txt file:
String editN = jTextField13.getText();
if (jTextField18.getText().equals("1")) {
try {
String verify, putData;
File file = new File("Name.txt");
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(editN);
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
while ((verify = br.readLine()) != null) {
if (verify != null) {
putData = verify.replaceAll("here", "there");
bw.write(putData);
}
}
br.close();
bw.close();
FileReader fr2 = new FileReader(file);
BufferedReader br2 = new BufferedReader(fr2);
String line1 = br2.readLine();
br2.close();
Worker1.setText("1. " + line1);
EditWorker.setVisible(false);
Workers.setVisible(true);
Workers.pack();
} catch(IOException e) {
e.printStackTrace();
}
}
What I need to add that it will edit second line?
I need to edit something that it will edit the second line in the txt file.
Learning Java by coding...
Read and write text file to insert tabs to further be imported into Mnemosyne
I got a text file written by hand by myself like this:
First line of text\tansewer
Second line of text\tanswer
Third line of text\tansewer
However when I try to print it or write it to a file I don't get the tab I need.
public static void main(String[] args) {
String fileName = "jeg.txt";
String line = null;
String[] mylines = new String[20];
int i = 0;
try {
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
mylines[i++] = line;
}
bufferedReader.close();
}
All you are doing is reading the information into a variable. Once you have the information, you have to output it somewhere for it to show up.
public static void main(String[] args) {
String fileName = "jeg.txt";
String line = null;
String[] mylines = new String[20];
int i = 0;
try {
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
mylines[i++] = line;
}
bufferedReader.close();
}
for(int j=0;j<i;j++){
System.out.println(mylines[j]);
}
This will output your lines to the system line. You can use the same concept in a tab or anywhere else.
I read a file by using bufferreader and also have another array(array_to_compare_with). So, I want to compare these two data.
How can I get over this ? Anybody has a clue ?
String[] array_to_compare_with = { "str1","str2","str3","str4" }
FileInputStream fstream = new FileInputStream("filename");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
// strLine and array_to_compare_with comparison needed to be done
}
Maybe you want something like this:
String[] array_to_compare_with = { "str1","str2","str3","str4" };
FileInputStream fstream = new FileInputStream("filename");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
boolean found = false;
for(String str : array_to_compare_with){
if(str.equalsIgnoreCase(strLine.trim())){
found = true;
break;
}
}
if(found){
//DO WHAT YOU WHANT
}
}
String[] array_to_compare_with = { "str1","str2","str3","str4" }
FileInputStream fstream = new FileInputStream("filename");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
for(String s: array_to_compare_with){
if(strLine.contains(s)){
System.out.println(strLine);
break;
}
}
}
Hi I want to replace the BufferedReader in the piece of code with scanner?? I wrote this code but then realized that we're not allowed use bufferedreader. But Havent a clue how to even go about,
public static void Option1Method() throws IOException
{
FileWriter aFileWriter = new FileWriter("wordlist.txt", true);
PrintWriter out = new PrintWriter(aFileWriter);
String word = JOptionPane.showInputDialog(null, "Enter a word");
out.println(word);
out.close();
aFileWriter.close();
String inputFile = "wordlist.txt";
String outputFile = "wordlist.txt";
FileReader fileReader = new FileReader(inputFile);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String inputLine;
List<String> lineList = new ArrayList<String>();
while ((inputLine = bufferedReader.readLine()) != null) {
lineList.add(inputLine);
}
fileReader.close();
Collections.sort(lineList);
FileWriter fileWriter = new FileWriter(outputFile);
PrintWriter out1 = new PrintWriter(fileWriter);
for (String outputLine : lineList) {
out1.println(outputLine);
}
out1.flush();
out1.close();
fileWriter.close();
}
Take a look at the definitions for hasNextLine and nextLine in the Scanner class.
Although new Scanner(fileReader) works, you could rather pass the file name directly into the Scanner's constructor.
This method is used to add words to a txt file and then sort them but I am not allowed use the Buffered Reader Function in my course. Is it possible to use Scanner to do this
public static void Option1Method() throws IOException
{
FileWriter aFileWriter = new FileWriter("wordlist.txt", true);
PrintWriter out = new PrintWriter(aFileWriter);
String word = JOptionPane.showInputDialog(null, "Enter a word");
out.println(word);
out.close();
aFileWriter.close();
String inputFile = "wordlist.txt";
String outputFile = "wordlist.txt";
FileReader fileReader = new FileReader(inputFile);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String inputLine;
List<String> lineList = new ArrayList<String>();
while ((inputLine = bufferedReader.readLine()) != null) {
lineList.add(inputLine);
}
fileReader.close();
Collections.sort(lineList);
FileWriter fileWriter = new FileWriter(outputFile);
PrintWriter out1 = new PrintWriter(fileWriter);
for (String outputLine : lineList) {
out1.println(outputLine);
}
out1.flush();
out1.close();
fileWriter.close();
}
Sure, you can. Here are equivalent codes using BufferedReader and Scanner
public void bufferedReaderVersion() throws Exception{
File f = new File("filePath");
Reader reader = new FileReader(f);
BufferedReader bufferedReader = new BufferedReader(reader);
String line = null;
while((line = bufferedReader.readLine()) != null){
System.out.println(line);
}
bufferedReader.close();
reader.close();
}
public void scannerVersion() throws Exception{
File f = new File("filePath");
Scanner sc = new Scanner(f);
while(sc.hasNext()){
System.out.println(sc.nextLine());
}
sc.close();
}