I made this class in a NetBeans project and cannot figure out why the createNewFile method will not recognize it's import. NetBeans is giving the "cannot find symbol" error for that line. "createNewFile" is the only part underlined in red on that line. It also gives warning on "import java.io.File" saying that it is never used.
I've added try and catch blocks around the method but they make no difference. Got rid of them in the example below for the sake of simplicity.
import java.util.Scanner;
import java.io.File;
public class Bleh {
Scanner in = new Scanner(System.in);
User u = new User();
public void setUserName() {
System.out.print("Name: ");
u.setName(in.nextLine());
}
public void checkForAccount() {
createNewFile(u.getName());
}
}
Your import of java.io.File imports exactly that, the class itself, into your namespace. It doesn't import all of File's methods, and even if it did, there is no such method File.createNewFile(String); you need to create a File object and call the method on it:
new File(u.getName()).createNewFile();
Related
So I needed to read a ByteArray from the InputStream in Android. Therefore I used this custom method in java in a kivy App using pyjnius for the same reason as stated in the link.
I placed the ReadInput.java file in this directory:
~/Build_Environ/.buildozer/android/platform/build/dists/JniusPrintBluetoothAppie/src/main/java/org/kivy/android
I initialised the java class with pyjnius:
Reading = autoclass('org.kivy.android.ReadInput')
The java code:
package org.kivy.android;
import java.io.InputStream;
import java.lang.Byte;
import java.lang.Integer;
import java.io.IOException;
public class ReadInput {
public byte[] inputread(InputStream stream, int count) throws IOException {
byte[] by = new byte[count];
stream.read(by);
return by;
}
}
I read from the buffer in python using the following code:
Reading.inputread(self.recv_stream, 4) #recv_stream is an Android BluetoothAdapter createInsecureRfcommSocketToServiceRecord getInputStream object
But for some reason this above code constantly gave me the following error:
JavaException: Unable to find a None Method
After many, many days of struggle I finally got the method to work by simply declaring the method as:
public static
The new java method looked as follows and I called it in the same way as above:
package org.kivy.android;
import java.io.InputStream;
import java.lang.Byte;
import java.lang.Integer;
import java.io.IOException;
public class ReadInput {
public static byte[] inputread(InputStream stream, int count) throws IOException {
byte[] by = new byte[count];
stream.read(by);
return by;
}
}
What I want to know is why would the word 'static' make the java method suddenly work?
The reason is that in Python code you access the method in a static way:
Reading.inputread(...)
This will work only if you define the method inputread in Java as static.
But it was not necessary. It could better to keep the method as non-static in Java and to use it in Python in a normal non-static way:
Reading = autoclass('org.kivy.android.ReadInput')
reading = Reading()
reading.inputread(...)
I have this code:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Date;
public class EmployeeProcessor {
public static void main(String[] args) {
Employee employee = new Employee();
employee.lastName = "Smith";
employee.firstName = "Adam";
employee.id = 123456789;
employee.salary = 50000;
try(FileOutputStream fileOutStr = new FileOutputStream("Employee.ser");
ObjectOutputStream objectOutStr = new ObjectOutputStream(fileOutStr)) {
objectOutStr.writeObject(employee);
System.out.println("An employee is externalized into the file Employee.ser");
} catch (IOException ioError){
ioError.printStackTrace();
}
}
}
But in Intellij IDEA ObjectOutputStream class is strikethrough Like this:
screenshot. When pointing mouse pointer over - this message appears: 'java.io.ObjectOutputStream' is deprecated. What does it mean?
When I run this code, IntelliJ opens "Edit Configurations" windows asking me to introduce VM options. But I leave it blank and run anyway.
IntelliJ IDEA has an intention action to annotate library classes as Deprecated using the External Annotations support. You've probably triggered this intention action by accident.
For the classes deprecated this way there supposed to be the reverse action: Deannotate, but it may not work (bug reported).
To fix it manually, find the annotations.xml file in a directory that is configured in the SDK Annotations tab and edit/remove it.
UPDATE: Deannoate action should work now, but only while inside the annotated class itself, not from its reference.
I have this code:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Date;
public class EmployeeProcessor {
public static void main(String[] args) {
Employee employee = new Employee();
employee.lastName = "Smith";
employee.firstName = "Adam";
employee.id = 123456789;
employee.salary = 50000;
try(FileOutputStream fileOutStr = new FileOutputStream("Employee.ser");
ObjectOutputStream objectOutStr = new ObjectOutputStream(fileOutStr)) {
objectOutStr.writeObject(employee);
System.out.println("An employee is externalized into the file Employee.ser");
} catch (IOException ioError){
ioError.printStackTrace();
}
}
}
But in Intellij IDEA ObjectOutputStream class is strikethrough Like this:
screenshot. When pointing mouse pointer over - this message appears: 'java.io.ObjectOutputStream' is deprecated. What does it mean?
When I run this code, IntelliJ opens "Edit Configurations" windows asking me to introduce VM options. But I leave it blank and run anyway.
IntelliJ IDEA has an intention action to annotate library classes as Deprecated using the External Annotations support. You've probably triggered this intention action by accident.
For the classes deprecated this way there supposed to be the reverse action: Deannotate, but it may not work (bug reported).
To fix it manually, find the annotations.xml file in a directory that is configured in the SDK Annotations tab and edit/remove it.
UPDATE: Deannoate action should work now, but only while inside the annotated class itself, not from its reference.
The following code is for reading or writing files with java, but:
Eclipse prints these errors:
buffer_1 cannot be resolved to a variable
file_reader cannot be resolved
also other attributes...
what is wrong in this code here:
//Class File_RW
package R_2;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.lang.NullPointerException;
public class File_RW {
public File_RW() throws FileNotFoundException, NullPointerException {
File file_to_read = new File("C:/myfiletoread.txt");
FileReader file_reader = new FileReader(file_to_read);
int nr_letters = (int)file_to_read.length()/Character.BYTES;
char buffer_1[] = new char[nr_letters];
}
public void read() {
file_reader.read(buffer_1, 0, nr_letters);
}
public void print() {
System.out.println(buffer_1);
}
public void close() {
file_reader.close();
}
public File get_file_to_read() {
return file_to_read;
}
public int get_nr_letters() {
return nr_letters;
}
public char[] get_buffer_1() {
return buffer_1;
}
//...
}
//main method # class Start:
package R_2;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.lang.NullPointerException;
public class Start {
public static void main(String[] args) {
File_RW file = null;
try {
file = new File_RW();
} catch (NullPointerException e_1) {
System.out.println("File not found.");
}
//...
}
}
I can't find any mistake. I have also tried to include a try catch statement into the constructor of the class "File_RW", but the error messages were the same.
Yes, there are errors in your code - which are of really basic nature: you are declaring variables instead of fields.
Meaning: you have them in the constructor, but they need to go one layer up! When you declare an entity within a constructor or method, then it is a variable that only exists within that constructor/method.
If you want that multiple methods can make use of that entity, it needs to be a field, declared in the scope of the enclosing class, like:
class FileRW {
private File fileToRead = new File...
...
and then you can use your fields within all your methods! Please note: you can do the actual setup within your constructor:
class FileRW {
private File fileToRead;
public FileRW() {
fileToRead = ..
but you don't have to.
Finally: please read about java language conventions. You avoid using "_" within names (just for SOME_CONSTANT)!
javacode already running...thx
same program edited with c++ in visual Studio express...
visit the stackoverflow entry link:
c++ file read write-error: Microsoft Visual C++ Runtime libr..debug Assertion failed, expr. stream.valid()
I have been working on an assignment for my first class in programming. I am working with NetBeans. I finished my project and it worked fine. I felt I could do better so I scraped it and started with a fresh slate. Now I am getting a message that says "No main class found" when i try to run it. But I have a main class. Here is some of the code:
package MyName;
import java.util.*;
import java.io.*;
public class MyName {
public static void main(String[] args)throws Exception {
//declare new file
java.io.File newFile = new java.io.File("LuisRamosp4.txt");
//check to see if file exist, if it does then delete it
if (newFile.exists()) {
newFile.delete();
}
//used to create a file
System.setOut(new PrintStream(newFile));
//instance 1 of Guitar object
Guitar myGuitar = new Guitar();
On the this line there is an error message saying "Can not find symbol" only I have a class built already. here is the line:
Guitar myGuitar = new Guitar();
Here is a piece of my class:
public class Guitar {
boolean isPlaying = false;
boolean isTuned = false;
I understand i should have kept the code I had. I wanted to try again and now I am stuck. Any advice will be greatly appreciated.
in java, you shouldn't use throws for the main method, it is already expected.