Variable not passing into anther class Java [closed] - java

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
got a strange one, i have a variable t i use it in one class, it changes, (e.g. 1 becomes 5) and then i call it from another class to use in that class, problem is t is always 0 when it is passed, what am i doing wrong
here is t in the class where it is edited
public int t = 1; //defualt value for amount of seconds in the future the job should wait untill sent
public int getT() {
return (t);
}
public void setT(int t) {
this.t = t;
}
and this is the class that i am using that calls t from the above class to use:
public class DealyTillPrint {
public int t;
public String CompletefileName;
private String printerindx;
private static int s;
private static int x;
public static int SecondsTillRelase;
public void countDown() {
System.out.println("Countdown called");
s = 1; // interval
t = (t * 60); // number of seconds
System.out.println("t is : " + t);
while (t > 0) {
System.out.println("Printing in : " + t);
try {
Thread.sleep(s * 1000);
} catch (Exception e) {
}
t--;
}
and here is where i set t using a spinner
<p:spinner min="1" max="1000" value="#{printerSettings.t}" size ="1">
<p:ajax update="NewTime"/>
</p:spinner>
How can i call t where the value is passed that is not zero

In DealyTillPrint you declare public int t; That t is different than the t you declare in the first code sample. Since you give it no value, it's default value of 0 is assigned. You are doing nothing to share t in the first sample with t in the second sample.
Change t = (t * 60); // number of seconds to t = (printerSettings.getT() * 60);

You need to get the printerSettings object from web page into your DealyTillPrint object. I can't tell you how to do that looking at the code you've submitted.

Related

Printing integers between 2 given integers [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I'm trying to create a class with a method that prints all integers between any two given integers. This is what I've got now-
public class IntList {
public static void main(String[] args) {
int start = Integer.parseInt(args[0]);
int stop = Integer.parseInt(args[1]);
for (int i = start + 1; i < stop; i++) {
System.out.print(i);
}
}
}
This won't compile, I get 2 errors saying "reached end of file while parsing", once each for lines 4 and 5.
Wrong main() method declaration. You have to pass an array as the only parameter for this function.
Then either declare your start and stop variables as local, and do the job inside the main method itself, or create a new function you call from the main() method.
No more explanation needed, this is Java basics. You should read a Java lesson.
There are 2 problem with this code
Main method doesn't have proper signature
make main like
public static void main(String ar[]){
}
and create another static method to accept two int variable
duplicate local variable declaration
remove
int i;
You already declare and initialize as a part of for loop
It will give you duplicate local variable error
Your main method declaration is incorrect. The argument list in a Java application's main method is required to be a String array. Read the start and stop values from the first 2 values of the String array after removing the duplicate declaration of the variable i:
public static void main(String[] args) {
int start = Integer.parseInt(args[0]);
int stop = Integer.parseInt(args[1]);
for (int i = start + 1; i < stop; i++) {
System.out.print(i);
}
}
Don't forget to pass in the start & stop values to the application
java IntList 1 10
You need public static void main(String[]) in your class in order it to be executed.
import java.util.Random;
public class IntList {
public static void main(int start, int stop){
for (int i = start + 1; i < stop; i++) {
System.out.print(i);
}
}
public static void main(String args[]){
main(random.nextInt(20),random.nextInt(100));
}
}

Method what returns the string of ArrayList [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
So I have:
private static ArrayList<AbstractAnalyser> analysers = new ArrayList<>();
public static String getAnalyser(String analyser){
if(analysers.contains(analyser)){
return "The full name of the analyser";
}
return null;
}
So what I want is..
If the the arraylist contains the parameter analyser, I want it to return the full name of the object what is in the arraylist.
Let's say these values are in the arraylist:
analyser, method and second. <- random names
If the parameter is analyser and the arraylist contains analyser. The method needs the return that name.
Even when the parameter is "analy".
I would use a Map instead of a List:
private static Map<String, AbstractAnalyser> analysers = new HashMap<>();
public static AbstractAnalyser getAnalyser(String analyserName){
AbstractAnalyser result = null;
if ((analyserName != null) && (analyserName.trim().length() > 0)) {
if (analysers.containsKey(analyserName)) {
result = analysers.get(analyserName);
} else {
for (String key : analysers.keySet()) {
// put the logic to find the one you want here.
}
}
}
return result;
}
But, if you must, you can do it this way if the AbstractAnalyser has a way to give you its name:
private static List<AbstractAnalyser> analysers = new ArrayList<>();
public static AbstractAnalyser getAnalyser(String analyserName){
AbstractAnalyser result = null;
if ((analyserName != null) && (analyserName.trim().length() > 0)) {
for (AbstractAnalyser analyser : analysers) {
// Here's how you look by name
if (analyser.getName().equals(analyserName)) {
result = analyser;
break;
} else {
// put special logic to find the one you want here.
}
}
}
return result;
}
Using the Map is always more efficient when you give the exact name because the lookup is O(1). The List lookup is always O(N).

Java how to print two different values from one array [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I am striving to print two different values from one array of stored models of computers. At the moment my program print first computer from the index but I cannot get how to print just one of particular model.
This is a fragment of my Main class
ComputerList list = new ComputerList();
Coputer item;
String model;
switch (option)
{
case 'M':
model = Console.askString("Enter model?: ");
item = list.findModel(model);
if (item == null)
System.out.println("Cannot find " + model);
else
item.print("Computer details..." + model);
... and this is my ComputerList class
ArrayList<Laptop> laptops;
private ArrayList<String> models;
public SerialList()
{
laptops = new ArrayList<Laptop>();
models = new ArrayList<String>();
}
public void add(Computer anComputer)
{
laptops.add(anComputer);
models.add(anComputer.getModel());
}
public void print()
{
int nItems = computer.size();
for (int i=0; i<nItems; i++)
{
System.out.println(computers.get(i));
}
public Computer findModel(String aModel)
{
int index = models.indexOf(aModel);
if (index == -1)
return null;
else
return computers.get(index);
}
}
I really struggling resolve this matter for few days but most of tutorials are based on numbers, values etc.
I will be very grateful of any help to this matter.
Regards
When you print an object you get the identifier #222222 etc...
You have to print attributes.
System.out.println(computer.getName + computer.getID);
You need a list iteration through your ComputerList.
ComputerList list = new ComputerList();
for (ComputerList currentComputer: list) {
currentComputer.print();
}
Just memorize the format. Also, I would use a HashMap or a different collection for that one to one String -> Laptop correspondence.

What variable do i use when i'm declaring a variable such as A+ [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Im making a program that calculates one's gpa. My variables include the grade A+ etc...
Any help would be greatly appreciated,
Thanks
I would create your own enum class.
enum Grade {
A(4.0, "A"), B_PLUS(3.5, "B+"), B(3.0, "B"), // etc.
F(0, "F");
private float gradePoints;
private String asString;
private Grade(float gradePoints, String asString) {
this.gradePoints = gradePoints;
this.asString = asString;
}
public float getGradePoints() {
return gradePoints;
}
#Override
public String toString() {
return asString;
}
}
This maybe a good candidate for Value Object from Domain-Driven Design.
public class GpaGrade {
private Int32 _inPercent;
public GpaGrade(Int32 inPercent) {
// TODO: check that inPercent is between 1 and 100
_inPercent = inPercent;
}
public Int32 AsPercents() {
return _inPercent;
}
public Double In40Scale() {
// TODO: convert to 4.0 scale
}
public String AsLetterCode() {
// TODO: convert to letter code
}
// TODO: override Equals and GetHashCode
}

Java HashMap not working [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I think I did every thing but HashMap.get returns null.
hashCode returns same integer, equals returns true, key is immutable, but still now working.
What am I missing?
Here is my code:
public enum MyEnum_1 `AA1, AA2, AA3, AA4`;
public enum MyEnum_2 `BB1, BB2, BB3, BB4`;
public void MyClass()
{
...
final MyEnum_1 enum1;
final MyEnum_2 enum2;
public int hashCode()
{
return (enum1.ordinal() * 100 + enum2.ordinal());
}
public boolean equals(MyClass obj2)
{
if (obj2 == null) return false;
else return (enum1.equals(obj2.getEnum1()) && enum2.equals(obj2.getEnum2()));
}
...
}
...
Map<MyClass, MyOtherClass> mappp = new HashMap<MyClass, MyOtherClass>();
...
mappp.put(obj1, other_obj1);
MyClass obj2 = new MyClass(obj1.getEnum1(), obj1.getEnum2());
System.out.println("hashCode: " + (obj1.hashCode() == obj2.hashCode()));
System.out.println("equals: " + obj1.equals(obj2));
System.out.println("Map Size: " + mappp.size());
MyOtherClass other_objjj = mappp.get(obj2);
System.out.println("other_objjj: " + other_objjj);
...
Print RESULTS are as follows:
hashCode: true
equals: true
Map Size: 1
other_objjj: null
Can any one see what I am missing, please?
You haven't overridden the equals(Object) method; you overloaded it with equals(MyClass).
Do it like this:
#Override
public final boolean equals(Object obj2)
{
if (obj2 == this) return true;
if (!(obj2 instanceof MyClass)) return false;
MyClass that = (MyClass) obj2;
return (enum1.equals(that.getEnum1()) && enum2.equals(that.getEnum2()));
}

Categories

Resources