I put my super statements in the correct format, yet my only error is that it states that the constructor object in class java.lang.object can't be applied to given types for this part of the code:
super (openFile (filename));
Here is my current code now:
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Vector;import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
public class Buffin
{
private static boolean temp;
/////////////////////////////////
private boolean isKeyboard;
/** Connect to the disk file with the given name. If this
* cannot be done, connect to the keyboard instead. */
public Buffin (String filename)
{ super (openFile (filename));
isKeyboard = temp;
} //======================
private static Reader openFile (String filename)
{ try
{ temp = false;
return new FileReader (filename); // IOException here
}catch (IOException e)
{ temp = true;
return new InputStreamReader (System.in);
}
} //======================
/** Read one line from the file and return it.
* Return null if at the end of the file. */
public String readLine()
{ if (isKeyboard)
{ System.out.print (" input> ");
System.out.flush(); // flush the output buffer
}
try
{ return super.readLine(); // in BufferedReader
}catch (IOException e)
{ System.out.println ("Cannot read from the file");
return null;
}
} //============
}[/code]
Your class extends only the Object class, and it doesn't have a method or constructor called openFile. You would be fine if you remove the super call altogether.
At the moment, your class Buffin does not specify a super class; that means it has one super class, and that is java.lang.Object. There is no constructor that takes a Reader in java.lang.Object.
You could store a Reader as a field in your Buffin class, or you might extend a super class that accepts one in the constructor.
Super involves the constructor of the superclass, I see that the Class Buffin inherits no class. As #Sotirios Delimanolis said, no constructor accepting Reader in Object class.
Related
I've finally gotten around to learning Java and I'm trying to write an interpreter for an esolang. I looked up a few tutorials and wrote this code.
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
public class NDBall {
public static Scanner scanner = new Scanner(System.in);
public static void main(String args[]) {
String input = scanner.nextLine();
Path path = Paths.get(input);
List<String> code = new ArrayList<String>();
code = Files.readAllLines(path);
}
}
However, the readAllLines function keeps giving me an error, and I don't know why. It won't tell me what the error is, and everything else seems fine. I'm doing it exactly as the tutorials I looked up told me.
Is there some mistake I made?
The below code will work for you. have hard coded the path so escape characters are taken care of:
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Solution {
public static Scanner scanner = new Scanner(System.in);
public static void main(String args[]) {
String input = scanner.nextLine();
Path path = Paths.get("C:\\Users\\xyz\\Desktop\\imp.txt");
List<String> code = new ArrayList<String>();
try {
code = Files.readAllLines(path, StandardCharsets.UTF_8);
code.stream().forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
}
}
This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed last year.
im trying to read a csv with dummy data into java arraylists. I don't know what happened, but I get the described error message above the next day I started the program again.
Here's my Code. I hope you don't get irritated by the german variables. I think the structure is important.
package Aufgabe2;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import Aufgabe2.Models.Einlagekonto;
import Aufgabe2.Models.Girokonto;
import Aufgabe2.Models.Kunde;
public class Kundenreport {
public static void main(String[] args) throws ParseException, FileNotFoundException {
Scanner scanner = new Scanner(System.in);
List<Kunde> kunde = new ArrayList<Kunde>();
String pathKunden = "/Users/testuser/OneDrive/03_Privat/05_Code/01_Java/university/project8/src/Aufgabe2/Data/Kunden.csv";
String line = null;
try {
BufferedReader kundenReader = new BufferedReader(new FileReader(pathKunden));
kundenReader.readLine();
while ((line = kundenReader.readLine()) != null) {
String[] valuesKunden = line.split(";");
kunde.add(new Kunde(valuesKunden[0], valuesKunden[1], valuesKunden[2], valuesKunden[3]));
}
kundenReader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Here is the "Kunde" (engl. Customer) Class which inherits from the abstract class "Konto"
package Aufgabe2.Models;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import Aufgabe2.Abstract.Konto;
public class Kunde extends Konto {
public Kunde() {
}
// Kunde ohne Konten
public Kunde(String kundenNr, String name, String vorname, String kundeSeit) {
super(kundenNr, name, vorname, kundeSeit);
}
And the "Konto" (engl. account) class possesses the attributes, abstract methods and getters/setter methods.
package Aufgabe2.Abstract;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Scanner;
import Aufgabe2.Models.Kunde;
public abstract class Konto {
public final static Date today = new Date();
public Scanner scanner = new Scanner(System.in);
public static SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.GERMAN);
private String kundenNr, name, vorname, kundeSeit;
private double kontoStdGiro, kontoStdEinlage;
private String anlagedatum, faelligkeitsdatum;
public Konto() {
}
// Nur Kunde ohne Konten
public Konto(String kundenNr, String name, String vorname, String kundeSeit) {
this.kundenNr = kundenNr;
this.name = name;
this.vorname = vorname;
this.kundeSeit = kundeSeit;
}
I was searching for a while and thankful for every advise from you.
Thanks!
By the way, here is the folder structure of the project:
Screenshot of repository
Most languages - such as Java or Python are zero-indexed, meaning they start from 0 instead of 1 like we normally do when counting. So when accessing a particular index we have to go one less than its number, for example, if we want the second element in array arr, we would do arr[1]. So in this case an array of length one only has one index, meaning arr[0]. For further reading on zero index here's a link: https://softwareengineering.stackexchange.com/questions/110804/why-are-zero-based-arrays-the-norm
Unable to access read function in FILEREADER class when using Generic class. Can anybody please let me know why I can't access read function here?
Main class:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
public class MainIO {
public static void main(String[] args) throws Exception {
System.out.println("Inside mainIO class");
FileReader fr = null;
GenericInput gio;
try {
fr = new FileReader("C:\\Users\\alex\\IdeaProjects\\javaIO\\src\\inputfile.txt");
gio = new GenericInput(fr);
gio.readByteData(fr);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
2nd class:
import java.io.FileReader;
import java.io.InputStream;
public class GenericInput < T > {
T input;
public GenericInput(T input) {
System.out.println("Inside: " + input.getClass().getName());
this.input = input;
}
public void readByteData(T input) throws Exception {
int c;
while ((c = input.read()) != -1)
System.out.print(c + "-");
}
}
Input text file:
pythön
Error
C:\Users\alex\IdeaProjects\javaIO\src\GenericInput.java:14:25
java: cannot find symbol
symbol: method read()
location: variable input of type T
Since <T> is not constrained, it can be anything - even an Object or an Integer, so there's no guarantee it will have a read method. If you want to use that method, you need to constrain T to something that has such a method - e.g., something that extends a Reader:
public class GenericInput <T extends Reader> {
// Here ---------------------^
This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 5 years ago.
I have recently started learning about file handling in java. However, in this code (down below), I am trying to close the file at the end of all the reading and writing but am facing an error in doing it this way.
package trycatch;
import java.util.Scanner;
import org.omg.CORBA.DataInputStream;
import java.*;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Writer;
public class Source {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
try {
File f = new File("record.txt");
FileOutputStream writing = new FileOutputStream(f);
DataOutputStream write = new DataOutputStream(writing);
write.writeUTF("What are the things that you want to do");
String str;
FileInputStream reading = new FileInputStream(f);
java.io.DataInputStream read = new java.io.DataInputStream(reading);
str = read.readUTF();
System.out.println(str);
}
catch(FileNotFoundException e) {
System.out.println("The system collapsed");
}
finally {
write.close(); // write cannot be resolved
read.close(); // read cannot be resolved
}
input.close();
}
}
I am trying out the finally keyword but can you tell me why my IDE cannot recognize read and write when I write it there?
write cannot be resolved
Your read and write fields are local to try block, finally can't access then.Initialize it outside of try.
Try it like that:
package trycatch;
import java.util.Scanner;
import org.omg.CORBA.DataInputStream;
import java.*;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Writer;
public class Source {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
DataOutputStream write = null;
java.io.DataInputStream read = null;
try {
File f = new File("record.txt");
FileOutputStream writing = new FileOutputStream(f);
write = new DataOutputStream(writing);
write.writeUTF("What are the things that you want to do");
String str;
FileInputStream reading = new FileInputStream(f);
read = new java.io.DataInputStream(reading);
str = read.readUTF();
System.out.println(str);
}
catch(FileNotFoundException e) {
System.out.println("The system collapsed");
}
finally {
if (write != null)
write.close(); // write cannot be resolved
if (read != null)
read.close(); // read cannot be resolved
}
input.close();
}
}
You are declaring write inside the try-block. It can't be resolved inside the finally block as this is a different scope.
You need to declare write before the try-block to make it accessible in finally:
DataOutputStream write = null;
try {
...
write = new DataOutputStream(writing);
...
} finally {
if (write != null) {
write.close();
}
}
With recent versions of Java you could/should use the try-with-resource construct to ensure proper resource handling. With this you can omit the finally-block and the JVM will take care of closing your resources when the try-block is left:
try (DataOutputStream write = new DataOutputStream(writing)) {
...
}
write and read are created in the try block and their scope is only in the block. Move the declaration where you are declaring input and it should work.
Hi, I am totally new in java.
This is my java code:
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.BufferedReader;
public class readw {
public static void main(String[] args) throws IOException {
List<String> lines = new ArrayList<String>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("C:\\run\\input.txt"));
String line = null;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
} finally {
reader.close();
}
String[] array = lines.toArray();
}
}
When I am trying to compile it I got this type of error:
line 8: can not find symbol List (L)and ArrayList(A)
I am trying to get content of my text file and want to set in to as a array.
Add
import java.util.ArrayList;
import java.util.List;
yes its work now i want to see the array result. how?
With
System.out.println(lines);
You need to import all the classes you use.
import java.util.ArrayList;
import java.util.List;