I'm writing a Java program to process each state of a search graph, but can't seem to create instances of State inside one of my loops.
The program takes numbers from a file, line by line, and converts each number into a State object, however I'm getting an error State cannot be resolved to a variable
public class ABSrch {
static HashMap<Integer, State> states = new HashMap<Integer, State>();
public static void main(String[] args) {
File file = new File("D:\\Stuff\\TextExample.txt");
int level = 0, depthBound = 0, stateNumber = 0;
try {
Scanner s = new Scanner(file);
depthBound = Integer.parseInt(s.nextLine().split(" ")[1]);
s.close(); s = new Scanner(file);
while(s.hasNextLine()) {
String line = s.nextLine();
String[] tokens = line.split(" ");
if(level >= 0 && level <= 7) {
if(level == 0) tokens = line.split(" |7");
}
for(int i = 0; i < tokens.length; i++)
State parent = new State(false,0,0,null); //error: State not resolved to variable
level++;
}
s.close();
} catch (FileNotFoundException e) {
System.err.println("File not found");
}
}
}
State class:
public class State {
private boolean max;
private int name, value;
private State parent;
private ArrayList<State> children;
public State(boolean max, int name, int value, State parent) {
this.max = max;
this.name = name;
this.parent = parent;
children = new ArrayList<State>();
}
public boolean isMax() { if(max == true) return true; else return false; }
public void setMax(boolean max) { this.max = max; }
public int getValue() { return value; }
public void setValue(int value) { this.value = value; }
public int getName() { return name; }
}
for(int i = 0; i < tokens.length; i++)
State parent = new State(false,0,0,null);
You cannot have initialization inside for loop without braces. It counts as local variable declaration and is not allowed in Java loops. This is to prevent you from using references which are valid only during object initialization. After iterating over the loop, references to the object created in previous iteration would be lost so garbage collector would soon destroy this objects.
This, hovewer, should work (but it is a bad practice and probably won't do what you expect from it):
for(int i = 0; i < tokens.length; i++) {
State parent = new State(false,0,0,null);
}
Joshua Bloch in his book Java Puzzlers provides an excellent explanation:
A local variable declaration looks like a statement but technically
speaking is not; it is a local variable declaration statement [JLS
14.4]. The syntax of the language does not allow a local variable declaration statement as the statement repeated by a for, while, or do
loop [JLS 14.12-14]. A local variable declaration can appear only as a
statement directly within a block. (A block is a pair of curly braces
and the statements and declarations contained within it.)
Related
So I'm currently working on a project that is recreating methods for Array String Lists and Linked String Lists. There is a StringList interface, that both ArrayStringList and LinkedStringList implement. We are not allowed to see the source code for the interface - only the API documentation. For each class, we have to create a default constructor and copy constructor for both classes. I've ran tests, and the default constructors both pass but the ArrayStringList copy constructor does not work and has been throwing the error message of "null" or "-1". I am pretty new to inheritance and interfaces, and I think the object parameters vs string array data types are throwing me off a bit.
Here is the code I have so far, and the methods used in the constructor:
My Copy Constructor:
private String[] stringArray;
private int size;
public ArrayStringList(StringList sl) {
size = sl.size();
ArrayStringList asl = new ArrayStringList();
for(int i = 0; i < size-1; i++) {
if(sl.get(i) != null) {
asl.set(i,sl.get(i).toString());
} //if
} // for
} // copy constructor
Size Method:
public int size() {
return stringArray.length;
} // size
Get Method:
public String get(int index) {
if(index < 0 || index >= size) {
throw new IndexOutOfBoundsException("out of bounds");
} else {
return stringArray[index];
}
} //get
Set Method:
public String set(int index, String s) {
String old = stringArray[index];
stringArray[index] = s;
return old;
} // set
In the project, the description of the copy constructor was as follows:
The implementing class must explicitly define a copy constructor. The copy constructor should take exactly one parameter of the interface type StringList. It should make the newly constructed list object a deep copy of the list referred to by the constructor's parameter. Therefore, the initial size and string elements of the new list object will be the same as the other list. To be clear, the other list can be an object of any implementation of the StringList interface. No other assumptions about the type of the object should be made.
public class ArrayStringList implements StringList {
private static final int INITIAL_CAPACITY = 10;
private String[] stringArray;
private int size;
public ArrayStringList(StringList sl) {
stringArray = sl.toArray();
size = stringArray.length;
}
public ArrayStringList() {
stringArray = new String[INITIAL_CAPACITY];
size = 0;
}
// TODO: Extract 'if-cascade' to an validate(..) method
#Override
public String set(int index, String s) {
if (index >= size) {
throw new IndexOutOfBoundsException("")
} else if (s == null) {
throw new NullPointerException("the specified string is null");
} else if (s.isEmpty()) {
throw new IllegalArgumentException("specified string is empty");
}
String old = stringArray[index];
stringArray[index] = s;
return old;
}
// TODO: Check if posible to extend the stringArray
#Override
public boolean add(String s) {
if (s == null) {
throw new NullPointerException("the specified string is null");
} else if (s.isEmpty()) {
throw new IllegalArgumentException("specified string is empty");
}
if (size == stringArray.length) {
int newListCapacity = stringArray.length * 2;
stringArray = Arrays.copyOf(stringArray, newListCapacity);
}
stringArray[++size] = s;
return true;
}
// TODO: implement other methods ...
}
Keep in mind that this implementation is still buggy, but you can use it as a starting point
public void ArrayStringList(StringList sl) {
size = sl.size();
ArrayStringList asl = new ArrayStringList();
for(int i = 0; i < size; i++) {
if(sl.get(i) != null) {
String s = asl.set(i,sl.get(i).toString());
System.out.println(s);
} //if
} // for
}
Change set method like below. And call it by the help of class object. it will set value in global static list.
//Change set method like this
public String set(int index, String s) {
stringArray[index] = s;
return stringArray[index];
}
I would initialise the internal array to the value of size and also make use of the fact that the String class also has a copy-constructor
public ArrayStringList(StringList sl) {
this.size = sl.size();
this.stringArray = new String[size];
for(int j = 0; j < size; j++) {
this.stringArray[j] = new String(sl.get(i));
}
}
I know that the variable maxreps isn't in the scope of my main method so I wanted it call it by creating an object, but it still isn't able to get maxreps.
How could I fix this?
public class LUIS{
public void james(){
int current=1;
int maxreps=1;
String adriana = "aabbddddnsspkrrgg";
for(int a=0; a<adriana.length(); a++){
if(adriana.charAt(a) == adriana.charAt(a+1)){
current++;
if(maxreps>=current){
maxreps=current;
}
}
}
}
public static void main(String[] args){
LUIS fritz = new LUIS();
final int drei = fritz.james;
System.out.println(maxreps);
}
}
As you noted, scoping prevents seeing a variable defined in a different scope. You can resolve your particular issue by returning the value
public int james(){ // <-- change from void to an int return
int current=1;
int maxreps=1;
String adriana = "aabbddddnsspkrrgg";
for(int a=0; a<adriana.length(); a++){
if(adriana.charAt(a) == adriana.charAt(a+1)){
current++;
if(maxreps>=current){
maxreps=current;
}
}
}
return maxreps; // <-- return the value
}
And then in the main method set a variable to the returned value.
Alternatively, you can define it as a class variable, but there are reasons to avoid doing so -- globals are generally bad.
1) final int drei = fritz.james; cannot compile. You cannot invoke a method in this way (that is without ()).
2) Besides, the james() method should have a more meaningful name.
This method computes the max series of a same character. So, you could call it computeMaxSeries()
3) And instead being a void method, you could return the max series number.
4) Besides this :
for (int a = 0; a < adriana.length(); a++) {
if (adriana.charAt(a) == adriana.charAt(a + 1)) {
will throw a StringIndexOutOfBoundsException as adriana.charAt(a + 1) refers to an index beyond the valid limit of the String length.
You should rather iterate until the last index -1 :
for (int a = 0; a < adriana.length()-1; a++) {
5) At last this is not consistent since you update maxreps by relying on maxreps instead of current :
if(maxreps>=current){
maxreps=current;
}
You should rather write :
if (current >= maxreps) {
maxreps = current;
}
So, finally the method would be :
public int computeMaxSeries(){
int current=1;
int maxreps=1;
String adriana = "aabbddddnsspkrrgg";
for(int a=0; a<adriana.length()-1; a++){
if(adriana.charAt(a) == adriana.charAt(a+1)){
current++;
if (current >= maxreps) {
maxreps = current;
}
}
}
return maxreps;
}
Now you can do :
final int maxreps = fritz.computeMaxSeries();
System.out.println(maxreps);
I got a null pointer exception when accessing a static array from a static member method.
The exception is thrown when i call setData(x, y, z) from a thread. When I debugged it I found out data[0] is null when i try to write to it. I just don't understand how it can be null
public class dataContainer
{
private static final short nrOfDataElements = ids.total_ids;
private static regularDataElement[] data = new regularDataElement[nrOfDataElements];
public static synchronized void getData(final short i, regularDataElement r)
{
if ( (i >= 0) && (i < nrOfDataElements) )
r.set(data[i].getTimestamp(), data[i].getValue());
}
public static synchronized void setData(short i, double ts, long val)
{
if ( (i >= 0) && (i < nrOfDataElements) )
data[i].set(ts, val); //<<-------null pointer exception, debugging showed data[i] == null, (with i = 0 and nrOfDataElements = 12)
}
}
and
public class regularDataElement
{
regularDataElement()
{
set(0, 0);
}
public void set(double _ts, long _val)
{
System.out.println(this.ts + " " + _ts + " " + this.val + " " + _val); System.out.flush();
this.ts = _ts;
this.val = _val;
}
public double getTimestamp()
{
return this.ts;
}
public long getValue()
{
return this.val;
}
private double ts;
private long val;
}
The statement private static regularDataElement[] data = new regularDataElement[nrOfDataElements]; initializes data with an array the size of nrOfDataElements. It does not initialize each element in this array. I.e., all the elements are null.
If you wish to initialize the elements, you should do so yourself. E.g., you could add a static block to handle this:
static regularDataElement[] data = new regularDataElement[nrOfDataElements];
static {
for (int i = 0; i < nrOfDataElements; ++i) {
data[i] = new regularDataElement();
}
}
Did you ever initialize the data array?
private static regularDataElement[] data =
new regularDataElement[nrOfDataElements];
will create an array full of null objects of size nrOfDataElements. It won't actually initialize any elements in the array.
You don't appear to be allocating memory for data[i], which is why you're getting the NPE.
Allocating memory for the array itself is not enough. You need to allocate memory for each element:
for (int i = 0; i < nrOfDataElements; ++i) {
data[i] = new regularDataElement(...);
}
(Replace ... with the actual arguments.)
You never actually create any objects. You must add somewhere:
for (int i = 0; i < data.length; i++) {
data[i] = new regularDataElement();
}
When you initialize an Object array in Java, like data in your code, all elements are by default set to null.
So, you need to populate the data array first, before being able to call any methods against its elements.
Assuming that the regularDataElement class has a no-args (i.e., no parameters) constructor, you could do
static regularDataElement[] data = new regularDataElement[nrOfDataElements];
static
{
for (int i=0; i<nrOfDataElements; i++)
{
data[i] = new regularDataElement();
}
}
Of course, you could have a separate method to initialize the array, e.g.
static regularDataElement[] initialize(int nrOfDataElements)
{
regularDataElement[] elements = new regularDataElement[nrOfDataElements];
for (int i=0; i<nrOfDataElements; i++)
{
elements[i] = new regularDataElement();
}
return elements;
}
and then call that method to create and initialize the data array, replacing the statement
static regularDataElement[] data = new regularDataElement[nrOfDataElements];
with
static regularDataElement[] data = initialize(nrOfDataElements);
Also, as a matter of following established coding conventions, you should name your classes starting with a capital letter, i.e. use RegularDataElement instead of regularDataElement.
It seems that when I create my scanner I get this error. I have tried to solve this by searching the error name, but have so far been unsuccessful in getting the message to stop appearing.
Code:
import java.util.Scanner;
public class PrintQueue {
//Instance variables
private Queue<Job> pq;
//Constructor
public PrintQueue() {
pq = new Queue<Job>();
}
//Adds a job object to the end of the queue
public void lpr(String owner, int jobId) {
Job j = new Job(owner, jobId);
pq.enqueue(j);
}
//Enumerates the queue
public void lpq() {
Job curr = pq.first();
for (int i = 0; i < pq.size(); i++) {
System.out.println(curr);
curr = pq.next();
}
}
//Removes the first entry in the queue if the input integer matches the integer contained within the job object
public void lprm(int jobId) {
if (pq.first().getJobId() == (jobId))
pq.dequeue();
else
System.out.println("Unable to find jobId.");
}
//Removes all objects that contain the input String
public void lprmAll(String owner) {
Job curr = pq.first();
for (int i = 0; i < pq.size(); i++) {
if (curr.getOwner().equals(owner))
pq.dequeue();
curr = pq.next();
}
}
//Demo
public static void main(String[] args) {
Scanner k = new Scanner(System.in);
PrintQueue myPQ = new PrintQueue();
String name;
int id;
for (int i = 1; i <= 5; i++) {
System.out.print("Enter owner and id: ");
name = k.next();
id = k.nextInt();
myPQ.lpr(name, id);
}
System.out.println("Print Queue");
myPQ.lpq();
myPQ.lprm(101);
myPQ.lprmAll("ronaldinho");
System.out.println("Print Queue");
System.out.println("\n\n");
myPQ.lpq();
}
}
Part where I get the error:
Scanner k = new Scanner(System.in);
That's because you're never closing the Scanner. Change your code to:
Scanner k = null;
try {
k = new Scanner(System.in);
//do stuff with k here...
} finally {
if( k != null )
k.close();
}
It seems that it is rather warning than error. However it is good practice to solve it.
Actually you just have to call k.close(); in the end of your method.
The best practice is to call close in finally block: this guarantees that the resource is closed whenever exception is thrown or not;
Scanner k = null;
try {
k = new Scanner(System.in);
........
} finally {
if (k != null) {
k.close();
}
}
Fortunately java 7 provides makes this syntax less verbose:
try (
Scanner k = new Scanner(System.in);
) {
.... // use k
}
When object of any class that implements Closable is created in special section of try block that marked with regular brackets () you do not have to write finally block: it is added by compiler.
I am new to using arrays of objects but can't figure out what I am doing wrong and why I keep getting a Null pointer exception. I am trying to create an Theatre class with an array of spotlight objects that are either set to on or off. But - whenever I call on this array I get a null pointer exception.
package theatreLights;
public class TheatreSpotlightApp {
public static void main(String[] args) {
Theatre theTheatre = new Theatre(8);
System.out.println("element 5 " + theTheatre.arrayOfSpotlights[5].toString());
}
}
package theatreLights;
public class Theatre {
spotlight[] arrayOfSpotlights;
public Theatre(int N){
arrayOfSpotlights = new spotlight[N];
for (int i = 0; i < arrayOfSpotlights.length; i++) {
arrayOfSpotlights[i].turnOn();
}
}
}
package theatreLights;
public class spotlight {
int state;
public spotlight(){
state = 0;
}
public void turnOn(){
state = 1;
}
void turnOff(){
state = 0;
}
public String toString(){
String stringState = "";
if(state == 0){
stringState = "is off";
}
else if(state==1){
stringState = "is on";
}
return stringState;
}
}
I must be doing something basic wrong in creating the array but can't figure it out.
replace
arrayOfSpotlights[i].turnOn();
with
arrayOfSpotLights[i] = new Spotlight();
arrayOfSpotlights[i].turnOn();
The line
arrayOfSpotlights = new spotlight[N];
will create an array of spotlights. It will however not populate this array with spotlights.
When you do "arrayOfSpotlights = new spotlight[N];" you init an array of length N, what you need to do is also init each object in it:
for i=0; i<N; i++
arrayOfSpotlights[i] = new spotlight();
arrayOfSpotlights[i].turnOn();
Hope I'm correct :)
You are not creating an spotlight objects.
arrayOfSpotlights = new spotlight[N];
This just creates an array of references to spotlights, not the objects which are referenced.
The simple solution is
for (int i = 0; i < arrayOfSpotlights.length; i++) {
arrayOfSpotlights[i] = new spotlight();
arrayOfSpotlights[i].turnOn();
}
BTW You should use TitleCase for class names.
You could write your class like this, without using cryptic code like 0 and 1
public class Spotlight {
private String state;
public Spotlight() {
turnOff();
}
public void turnOn() {
state = "on";
}
void turnOff() {
state = "off";
}
public String toString() {
return "is " + state;
}
}
You declared the array arrayOfSpotlights, but didn't initialize the members of the array (so they are null - and you get the exception).
Change it to:
public class Theatre {
spotlight[] arrayOfSpotlights;
public Theatre(int N){
arrayOfSpotlights = new spotlight[N];
for (int i = 0; i < arrayOfSpotlights.length; i++) {
arrayOfSpotlights[i]=new spotlight();
arrayOfSpotlights[i].turnOn();
}
}
}
and it should work.