I have a class named PointOfSale which have a public method getTpid, but I failed when I want to call this method in my main class, I can only call "TPID_FIELD_NUMBER", how to solve this problem?
PointOfSale ps = new PointOfSale();
ps.TPID_FIELD_NUMBER; //correct
ps.getTpid();// error
public final class PointOfSale implements PointOfSaleOrBuilder {
public static final int TPID_FIELD_NUMBER = 1;
private int tpid_;
#java.lang.Override
public int getTpid() {
return tpid_;
}
}
public interface PointOfSaleOrBuilder {
int getTpid();
}
I think that you just need to add () at the end of your call.
ps.TPID_FIELD_NUMBER is a call for a constant.
If you want to call a method, you should call like this: ps.getTpid();
public static void main(String[] args) {
PointOfSale ps = new PointOfSale();
ps.getTpid();
}
The issue is with the line "ps.TPID_FIELD_NUMBER;". This will give "Not a statement" error. You should assign it to a variable for successful execution.
Your code should look like,
public static void main(String[] args) {
PointOfSale ps = new PointOfSale();
int num = ps.TPID_FIELD_NUMBER;
ps.getTpid();
}
Related
I have created an array which I wanted to control from main. My code runs, but I don't know how to add integers to the array from the main class. Also as each ConcreteSubject has its own storage array, how would i change this to store them all in the same array?
public class ConcreteSubject extends AbstractSpy
{
private AbstractSpy[] spies = new AbstractSpy[10];
private int i = 0;
public void addSpy(AbstractSpy s) {
if (i < spies.length) {
spies[i] = s;
System.out.println("spy added at index " + i);
i++;
}
}
}
public class TestClass
{
public static void main(String[] args) {
ConcreteSubject cs = new ConcreteSubject();
AbstractSpy spies = new AbstractSpy() {
#Override
public void addSpy(AbstractSpy spies) {
}
};
cs.addSpy(cs);
spies.addSpy(spies);
}
}
It seems like your program logic is a little borked. This bit in particular doesn't make much sense:
***AbstractSpy spies = new AbstractSpy() {
#Override
public void addSpy(AbstractSpy spies) {
}
};
cs.addSpy(cs);
***spies.addSpy(spies);
What you're doing is creating TWO AbstractSpy instances, one named cs and one named spies. On that last line you're adding spies to itself! That doesn't help you at all.
Note that AbstractSpy is the most granular unit in your setup - it shouldn't have an addSpy() method and its own internal array, it should be the thing that's added to something else's array!
Here's the same code, but cleaned up a bit:
public abstract class AbstractSpy { }
public class ConcreteSpy extends AbstractSpy { }
public class ConcreteSubject {
private AbstractSpy[] spies = new AbstractSpy[10];
private int i = 0;
public void addSpy(AbstractSpy spy) {
if (i < spies.length)
{
spies[i] = spy;
System.out.println("spy added at index " + i);
i++;
}
}
}
public class TestClass {
public static void main(String[] args) {
ConcreteSubject cs = new ConcreteSubject();
AbstractSpy spy = new ConcreteSpy();
cs.addSpy(spy);
}
}
The big difference here is that ConcreteSpy is an implementation of AbstractSpy that you can add to your ConcreteSubject's array of spies. I think you might have been confused by Java's insistence that you can't create an instance of an abstract class on its own unless you supply an anonymous class that inherits from the abstract class.
I apologize if this is a basic question.
I am attempting to create several unique objects in one class, then get the values of one of the objects in another class.
I have created two classes and followed some examples to end with this
public class Type {
public String name;
public int healthmod;
public int strmod;
public int accmod;
public int armmod;
public int refmod;
public int intmod;
public String advantages;
public String disadvantages;
public Type() {
Type fire = new Type();
fire.name = "Fire";
fire.healthmod = 0;
fire.strmod = 1;
fire.accmod = 0;
fire.armmod = 0;
fire.refmod = 0;
fire.intmod = 1;
}
}
and then in the main class:
Player.typename = Type.fire.name;
Edit
public class Player {
public static String name, classname, racename, elementname;
public static int maxhealth, healthpts, healthptscost, healthupgnum, healthmod, currenthealth, basehealth;
public static int str, strpts, strptscost, strupgnum, strmod;
public static int acc, accpts, accptscost, accupgnum, accmod;
public static int arm, armpts, armptscost, armupgnum, armmod;
public static int ref, refpts, refptscost, refupgnum, refmod;
public static int intelligence, intpts, intptscost, intupgnum, intmod;
public static int mana, maxmana, managain, managainak;
public static int hitChance, critChance, Level, statPts, statTotal, damage, damageDealt, goldmult, itemmod, itemdefboost, itemdamboost, itemmodboost;
public static String[] focusStats;
}
What I am trying to do is create a few objects in the Type class and access them in the Main class in order to store the values in the Player class, but in the Main class, fire "cannot be resolved or is not a field"
Edited to provide more clarity on the purpose.
You are completely mixing things here.
In the concstructor of Type you are creating a new local Object named fire. This object is only available in this constructor and not outside of it, e.g. in the main class.
A valid solution can only be found if you give more information about what you try to accomplish.
I could write like that your Type construct:
public Type() {
this.name = "Fire";
this.healthmod = 0;
this.strmod = 1;
this.accmod = 0;
this.armmod = 0;
this.refmod = 0;
this.intmod = 1;
}
So when using the Player class in your Main method, like that:
public static void main(String args[]) {
Type type = new Type();
Player.typename = type.name;
}
You can also put a reference of type inside Player class like that:
public class Player {
public static Type fire;
}
So in your main method like that:
public static void main(String args[]) {
Player.fire = new Type();
System.out.println(Player.fire.name);
}
I need to assign a value to a println statement so that i can declare it as a variable and then use it anywhere in the code. i want to be able to assign a value to the "result" in the println, however i do not know how to do this. Does anyone know how to assign a value to this so that it can be used anywhere?
I have tried the following, however i get an error saying that void cannot be converted to string...
You can define a method:
private void print(String value) {
System.out.println(value);
}
This can be called from anywhere in your class.
If you would like to re-use this functionality in different classes you could create a separate class for it:
public class Printer {
public void print(String value) {
System.out.println(value);
}
}
You can then add it as a dependency in the class that wants to print:
public class MyApp {
private Printer printer;
public MyApp(Printer printer) {
this.printer = printer;
}
public void doSomething() {
printer.print("Hello world");
}
}
You can probably write it as a JAVA lambda expression, such as:
Consumer<String> print = it-> System.out.println(it);
Whenever you want to run, you can run it like this:
print.accept("hello");
Br,
Tim
Make a Consumer and pass a object whenever you want to print taht object.
Consumer consumer= System.out::println;
consumer.accept(5);
Updated
In your case define consumer as :
public static int myHouseValue;
public static final Consumer CONSUMER= System.out::println;
and any where and in any class you can use this consumer. just include this static member. and pass the object you want to print.
CONSUMER.accept(5);
CONSUMER.accept("String");
CONSUMER.accept(new Object());
CONSUMER.accept(myHouseValue);
Your Code (Updated):
public class Weka {
public static int Lotsize;
public static int Bedrooms;
public static int LocalSchools;
public static int Age;
public static int Garages;
public static int Bathrooms;
public static double myHouseValue = 0d;// here is the default value zero
public static final Consumer CONSUMER = System.out::println;
public static void main(String[] args) throws Exception {
System.out.println("Server up and running");
. . .
your code
. . .
// donot declare myHouseValue again , its already defined wher we set it to default value. only use here
myHouseValue = (coef[0] * Lotsize) +
(coef[1] * Bedrooms) +
(coef[2] * LocalSchools) +
(coef[3] * Age) +
(coef[4] * Garages) +
(coef[5] * Bathrooms) +
coef[7];
CONSUMER.accept(myHouseValue);
I have the object numberlist that i created in create() method and i want to access it so i can use it in the question() method.
Is there another way to do this that I probably missed? Am I messing something up? If not, how should I do this to get the same functionality as below?
private static void create() {
Scanner input = new Scanner(System.in);
int length,offset;
System.out.print("Input the size of the numbers : ");
length = input.nextInt();
System.out.print("Input the Offset : ");
offset = input.nextInt();
NumberList numberlist= new NumberList(length, offset);
}
private static void question(){
Scanner input = new Scanner(System.in);
System.out.print("Please enter a command or type ?: ");
String c = input.nextLine();
if (c.equals("a")){
create();
}else if(c.equals("b")){
numberlist.flip(); \\ error
}else if(c.equals("c")){
numberlist.shuffle(); \\ error
}else if(c.equals("d")){
numberlist.printInfo(); \\ error
}
}
While interesting, both of the answers listed ignored that fact that the questioner is using static methods. Thus, any class or member variable will not be accessible to the method unless they are also declared static, or referenced statically.
This example:
public class MyClass {
public static String xThing;
private static void makeThing() {
String thing = "thing";
xThing = thing;
System.out.println(thing);
}
private static void makeOtherThing() {
String otherThing = "otherThing";
System.out.println(otherThing);
System.out.println(xThing);
}
public static void main(String args[]) {
makeThing();
makeOtherThing();
}
}
Will work, however, it would be better if it was more like this...
public class MyClass {
private String xThing;
public void makeThing() {
String thing = "thing";
xThing = thing;
System.out.println(thing);
}
public void makeOtherThing() {
String otherThing = "otherThing";
System.out.println(otherThing);
System.out.println(xThing);
}
public static void main(String args[]) {
MyClass myObject = new MyClass();
myObject.makeThing();
myObject.makeOtherThing();
}
}
You would have to make it a class variable. Instead of defining and initializing it in the create() function, define it in the class and initialize it in the create() function.
public class SomeClass {
NumberList numberlist; // Definition
....
Then in your create() function just say:
numberlist= new NumberList(length, offset); // Initialization
Declare numberList outside your methods like this:
NumberList numberList;
Then inside create() use this to initialise it:
numberList = new NumberList(length, offset);
This means you can access it from any methods in this class.
Sorry, just learning Java; but, can someone tell me why I'm getting a "cannot find symbol" error?
My code is as follows:
public class NumberHolder {
public int anInt;
public float aFloat;
public NumberHolder(int setAnInt, float setAFloat) {
setAnInt = anInt;
setAFloat = aFloat;
}
public static void main(String[] args) {
NumberHolder newNumber = NumberHolder(12, 24F);
}
}
Looks like you're missing a new before the call to the constructor:
NumberHolder newNumber = new NumberHolder(12, 24F);
EDIT:
Also, as Tassos Bassoukos points out in his answer, you need to turn around the assignments in the constructor:
anInt = setAnInt;
aFloat = setAFloat;
Although personally, I like to write my constructors like this:
public NumberHolder(int anInt, float aFloat) {
this.anInt = anInt;
this.aFloat = aFloat;
}
This is a matter of style and personal preference, though.
Since
public NumberHolder(int anInt, float aFloat);
is a constructor and not an ordenary method, you need to use the keyword new in order to obtain the actual object. You are calling it like a method and you don't have any method named NumberHolder (but it would be valid if you'd have)
Beyond the new keyword that you're missing, the assignment in the constructor should be the other way around.
You need to instanciate new objects with the new keyword.
public class NumberHolder {
public int anInt;
public float aFloat;
public NumberHolder(int anInt, float aFloat) {
this.anInt = anInt;
this.aFloat = aFloat;
}
public static void main(String[] args) {
NumberHolder newNumber = new NumberHolder(12, 24F);
}
}