Our professor assigns us exercises through the jarpeb sytsem (Java Randomised and Personalised Exercise Builder). So the variables names are random.
public class Eczema extends Thread {
private int aurite;
private int[] serlvulate;
public Eczema(int[] serlvulate) {
this.serlvulate = serlvulate;
}
public int getAurite () {
return aurite;
}
#Override
public void run () {
try {
aurite = Integer.MAX_VALUE;
for (int i = 0; i < serlvulate.length; i++) {
if (i < this.aurite) {
this.aurite = i;
sleep (1000);
}
}
} catch (InterruptedException e) {
System.out.println("Found an Exception!");
return;
}
}
}
public class Stub {
public static int polytoky (int[]a, int[]b) throws InterruptedException {
Eczema Eczema1 = new Eczema (a);
Eczema Eczema2 = new Eczema (b);
Eczema1.start();
Eczema1.join();
Eczema2.start();
Eczema2.join();
return Math.min (Eczema1.getAurite(), Eczema2.getAurite());
}
}
I followed the instructions of the exercise but when I chech it on the cmd the following error occurs:
Field servulate not found: java.lang.NoSuchFieldException: servulate.
Any ideas how I fix it?
You appear to be accessing a field named servulate while the proper field name is serlvulate. Find the line where this happens and fix the spelling.
Related
Using static analysis is it possible to alert on the following code?
public class IntegerConversion {
static int value; // Notice this is an int and not Integer
public static Integer strToInt(String s) {
Integer k;
try {
k= Integer.parseInt(s);
} catch (NumberFormatException e) {
k = null;
}
return k;
}
public static void main(String[] args) {
// possible null pointer here as strToInt can return null
value = strToInt("abc");
}
}
I'm using spotbugs (findbugs successor), and it correctly alert on the code, if the assignment is done within the same method like below.
public class IntegerConversion {
static int value;
public static void strToInt(String s) {
Integer k;
try {
k= Integer.parseInt(s);
} catch (NumberFormatException e) {
k = null;
}
value = k; // spot bugs alert on this line
}
public static void main(String[] args) {
strToInt("abc");
}
}
Is the missing alert on the first code snippet a design choice, or a techincal limitation? are there other static analysis tools for java that can catch this error?
The code below compiled successfully here https://www.compilejava.net/ but execution fails
Error: Could not find or load main class ClassDemo
whereas it does have a main entry point. Why ?
package com.tutorialspoint;
import java.lang.reflect.*;
public class ClassDemo {
public static void main(String[] args) {
try {
ClassDemo c = new ClassDemo();
Class cls = c.getClass();
// returns the array of Field objects
Field[] fields = cls.getDeclaredFields();
for(int i = 0; i < fields.length; i++) {
System.out.println("Field = " + fields[i].toString());
}
}
catch(Exception e) {
System.out.println(e.toString());
}
}
public ClassDemo() {
// no argument constructor
}
public ClassDemo(long l, int i) {
this.l = l;
this.i = i;
}
long l = 77688;
int i = 3;
}
You need to remove the package identifier from your code (first line) since you are using an online compiler/executor.
Hope this helps.
It's because you have a package statement.
Remove that and it will work just fine.
I am making an application to control a serial attached printer and I want to provide a static factory method PrinterLocator.FindPrinters() that will return an array of the available printers connected to my system.
As an example, the Serial library provides Serial.list() which is a static method returning an array of strings corresponding to the ports available in my system. I am trying to create something similar, but I am getting an error "No enclosing instance of type SLPDriver is accessible. Must qualify the allocation with an enclosing instance of type SLPDriver"
What is the correct way to implement this design pattern?
SLPDriver:
SerialPrinter myPrinter;
void setup()
{
SerialPrinter[] availablePrinters = PrinterLocator.FindPrinters();
if(availablePrinters.length > 0)
{
myPrinter = availablePrinters[0];
}
}
void draw()
{
}
SerialPrinter:
import processing.serial.*;
static class PrinterLocator
{
static final int baudRates[] = {2400, 4800, 9600, 19200, 38400, 57600, 115200};
static final int baudCount = baudRates.length;
static SerialPrinter[] FindPrinters()
{
SerialPrinter[] foundPrinters, tempPrinters;
foundPrinters = new SerialPrinter[0];
String[] foundPorts = Serial.list();
int numPorts = foundPorts.length;
int numPrintersFound = 0;
if(numPorts <= 0) return foundPrinters;
SerialPrinter testPrinter;
tempPrinters = new SerialPrinter[numPorts];
for(int i = 0; i < numPorts; i++)
{
for(int b = 0; b < baudCount; b++)
{
testPrinter = new SerialPrinter("test", foundPorts[i], baudRates[b]);
if(testPrinter.IsValid())
{
tempPrinters[numPrintersFound] = testPrinter;
numPrintersFound++;
break;
}
}
}
if(numPrintersFound > 0)
{
foundPrinters = new SerialPrinter[numPrintersFound];
for(int i = 0; i < numPrintersFound; i++)
{
foundPrinters[i] = tempPrinters[i];
}
}
return foundPrinters;
}
}
class SerialPrinter
{
//Members
private Serial myPort;
private String printerName;
private boolean valid;
private String portName;
private int baudRate;
public SerialPrinter()
{
this("","",0);
}
public SerialPrinter(String name, String port, int baud)
{
printerName = name;
Configure(port, baud);
}
public boolean IsValid()
{
return valid;
}
public boolean Configure(String port, int baud)
{
print("Configuring Printer ");
print(port);
print("#");
print(baud);
print(": ");
try
{
myPort.stop();
myPort = null;
}
catch (Exception e) {}
portName = port;
baudRate = baud;
try
{
myPort = new Serial(this, port, baud);
myPort.clear();
myPort.write(0xA5);
int timeout = millis() + 1000;
while((millis() < timeout) && (myPort.available() == 0)) { }
if(myPort.available() > 0)
{
int inByte = myPort.read();
if(inByte == 0xC9)
{
valid = true;
}
else
{
valid = false;
}
}
else
{
valid = false;
}
}
catch (Exception e)
{
valid = false;
}
if(valid)
{
println("[OK]");
}
else
{
println("[ERR]");
}
return valid;
}
}
No enclosing instance of type SLPDriver is accessible. Must qualify the allocation with an enclosing instance of type SLPDriver
The error indicates that SerialPrinter is an inner class of SLPDriver.
You either need to change the class declaration to static:
static class SerialPrinter
{
...
Or if it's supposed to be inner, you need to use an SLPDriver instance to create a SerialPrinter:
someSLPDriver.new SerialPrinter(...)
http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
The Serial constructor appears to need an instance of PApplet. The instantiation expression should really give you an error too because this refers to the SerialPrinter, not the PApplet.
Something you could try is like the following:
...
static class PrinterLocator {
...
static SerialPrinter[] FindPrinters(SLPDriver context) {
...
... = context.new SerialPrinter(...);
}
}
class SerialPrinter {
...
public boolean Configure(...) {
...
... = new Serial(PApplet.this, ...);
}
}
When you call FindPrinters within the context of the applet, you need to call it like
... = PrinterLocator.FindPrinters(this);
I have got the following code Simmulation.java, but when I tried compiling it, it is coming up with an error saying,
error: constructor CashewPallet in class CashewPallet cannot be applied to give types;
CashewPallet c1 = new CashewPallet();
Required: String,int, found: no arguments
Reason: Actual and formal arguments differ in length
I know what this error means, and when I tried to fix the line to CashewPallet c1 = new CashewPallet(String, int); and again CashewPallet c1 = new CashewPallet(nutType, id); but either didn't work! AND now I am not sure how can this be solved.
I am new to this, any help is much appreciated.
Many Thanks in advance!
Please bear with me.
EDIT : Thank you for the answers everyone! It has worked now and compiled successfully BUT when I executed it, it is coming up with error: ArrayIndexOutOfBoundsException :0 at Simmulation.main(Simmulation.java:201) Any help in fixing it Please?
THANK YOU!
This is Simmulation.java file
import java.io.File;
import java.util.LinkedList;
import java.util.Queue;
import java.util.*;
import java.util.Scanner;
import java.io.*;
public class Simmulation implements Operation {
Queue<CashewPallet> inputQueue=new LinkedList<CashewPallet>();
Stack<CashewPallet> stBay1=new Stack<CashewPallet>();
Stack<CashewPallet> stBay2=new Stack<CashewPallet>();
FileOutputStream fout4;
PrintWriter pw;
static int tick=0;
CashewPallet c1;
String temp;
Scanner sc;
public Simmulation(String fn)
{
int index=0;
String nutType="";
int id=0;
Scanner s2 ;
try
{
sc = new Scanner(new File(fn));
fout4=new FileOutputStream("nuts.txt");
pw=new PrintWriter(fout4,true);
String eol = System.getProperty("line.separator");
while(sc.hasNextLine())
{
tick++;
s2 = new Scanner(sc.nextLine());
if(s2.hasNext())
{
while ( s2.hasNext())
{
String s = s2.next();
if(index==0)
{
nutType=s;
}
else
{
id=Integer.parseInt(s);
}
index++;
}
System.out.println("Nuttype "+nutType+" Id is "+id+"tick "+tick);
if((nutType.equalsIgnoreCase("A")||nutType.equalsIgnoreCase("P")|| nutType.equalsIgnoreCase("C")|| nutType.equalsIgnoreCase("W")) && id!=0)
inputQueue.add(new CashewPallet(nutType.toUpperCase(),id));
System.out.println("Size of Queue "+inputQueue.size());
int k=0;
if(!inputQueue.isEmpty())
{
while(inputQueue.size()>k)
{
// stBay1.push(inputQueue.poll());
process(inputQueue.poll());
k++;
}
// System.out.println("Size of input "+inputQueue.size() +" Size of stay "+stBay1.size());
}
}
else
{
fout4.write(" ".getBytes());
}
index=0;
if(!stBay2.isEmpty())
{
while(!stBay2.isEmpty())
{
c1=stBay2.pop();
temp=tick+" "+c1.getNutType()+" "+c1.getId()+eol;
fout4.write(temp.getBytes());
}
// System.out.println("Nut final "+ stBay2.peek().getNutType());
}
else
{
temp=tick+eol;
fout4.write(temp.getBytes());
}
}
}
catch(Exception e)
{
System.out.println("Exception "+e);
}
closeStream();
}
public CashewPallet process( CashewPallet c)
{
//CashewPallet c=new CashewPallet();
int k=0;
//while(stBay.size()>k)
//{
//c=stBay.pop();
String operation=c.getNutType();
if(c.getPriority()==1)
{
shelling(c);
washing(c);
packing(c);
//stBay2.push(c);
}
else
{
switch(operation)
{
case "A": shelling(c);
washing(c);
packing(c);
break;
case "C": washing(c);
packing(c);
break;
case "W" : washing(c);
shelling(c);
packing(c);
break;
}
}
return c;
}
public void closeStream()
{
try
{
fout4.close();
}
catch(Exception e)
{
}
}
public boolean shelling(CashewPallet c)
{
// for(int i=0;i<20; i++)
{
System.out.println("Performing Shelling for "+c.getNutType());
}
return true;
}
public boolean washing(CashewPallet c)
{
// for(int i=0;i<20; i++)
{
System.out.println("Performing Washing for "+c.getNutType());
}
return true;
}
public boolean packing(CashewPallet c)
{
//for(int i=0;i<20; i++)
{
System.out.println("Performing Packing for "+c.getNutType());
}
stBay2.push(c);
return true;
}
public static void main(String args[])
{
new Simmulation(args[0]);
}
This is CashewPallet.java file
public class CashewPallet {
private String nutType;
private int id;
private int priority;
private int opTick;
public int getOpTick() {
return opTick;
}
public void setOpTick(int opTick) {
this.opTick = opTick;
}
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
this.priority = priority;
}
public CashewPallet(String nutType, int id) {
this.nutType = nutType;
this.id = id;
if(this.nutType.equalsIgnoreCase("p"))
{
priority=1;
}
else
{
priority=0;
}
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNutType() {
return nutType;
}
public void setNutType(String nutType) {
this.nutType = nutType;
}
When you use CashewPallet's constructor, you need to supply actual values for the arguments. The arguments are String nutType and int id, which means you need to supply a String that will go into nutType and an int that will go into id. For example:
CashewPallet c1 = new CashewPallet("Pecan", 42);
The constructor in CashewPallet requires a String and an int; you didn't provide them:
public CashewPallet(String nutType, int id)
You called it like this:
CashewPallet c1 = new CashewPallet();
Change it to something like:
CashewPallet c1 = new CashewPallet("Peanut",1337);
EDIT:
You're getting an ArrayIndexOutOfBoundsException on this line:
new Simmulation(args[0]);
That's in your args[0]. If that's out of bounds, then that means that you don't have command line arguments.
You should call it with command line arguments (or specifically one command line argument).
java Simmulation myarg
In this case your argument is the name of your file.
I have two classes, main and timex. I want to display the value of a variable in my timex class, but I always get the answer 0.
public class mainaxe {
public static void main (String arg[]) {
timex n = new timex();
int n2 = timex.a;
n.timedel();
for(int i=0; i<20; i++) {
System.out.println("the time is :" + n2);
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {}
}
}
}
And this is my timex class:
public class timex extends Thread{
public static int a;
public int timedel(){
for(int i=0; i<200; i++) {
try {
Thread.sleep(1000);
a = a + 5;
}
catch (InterruptedException e){}
// start();
}
return a;
}
}
I want to get the value from the timex class and use it in my main class to print the value for every 1 sec.
I guess you need something like,
Mainaxe.java
package mainaxe;
public class Mainaxe {
public static void main(String arg[]) {
Timex n = new Timex();
n.start();
// int n2 = Timex.a;
// n.timedel();
for (int i = 0; i < 20; i++) {
System.out.println("the time is :" + Timex.a);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
}
Timex.java
package mainaxe;
public class Timex extends Thread {
public static int a;
public Timex() {
super();
}
#Override
public void run() {
timedel();
}
public int timedel() {
for (int i = 0; i < 200; i++) {
try {
Thread.sleep(1000);
a = a + 5;
} catch (InterruptedException e) {
}
// start();
}
return a;
}
}
If you want a multi-threaded program, then in your class that extends Thread, declare a method exactly like this:
#Override
public void run () {
// in here, put the code your other thread will run
}
Now, after you create a new object of this class:
timex n = new timex();
you have to start the thread like this:
n.start();
This causes the object to start running its run method in a new thread. Having your main thread call other methods in n won't do anything with the new thread; any other method called by the main thread will be performed in the main thread. So you can't communicate with the new thread with a function call. You have to do it with other means, such as you were trying to do with your variable a.