Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Improve this question
I'm new to Java. I want to create Java Object with test data and access the object from remote class. I created this object:
public class TestAgentData
{
public TestAgentDataObj tad;
public class TestAgentDataObj
{
public int agentId = 1234;
public String agentName = "AgentName";
public String description = "AgentDscription";
public TestAgentDataObj(int agentId, String agentName, String description)
{
this.agentId = agentId;
this.agentName = agentName;
this.description = description;
}
public int getAgentId()
{
return agentId;
}
public void setAgentId(int agentId)
{
this.agentId = agentId;
}
public String getAgentName()
{
return agentName;
}
public void setAgentName(String agentName)
{
this.agentName = agentName;
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
}
public TestAgentDataObj getTad()
{
return tad;
}
public void setTad(TestAgentDataObj tad)
{
this.tad = tad;
}
}
I tried to access the object from remote class:
Object eded = new TestAgentData.getTad();
But I get error in Netbeans. Can you tell what is the proper way to access data in a Java Object?
I think you need a better understanding about java. There are big errors in this.
You cannot way you create your object is wrong its new TestAgentData()
You can't call getTad() from an object of type Object because there is no getTad() method defined in Object class. Rather do the following
TestAgentDataObj obj=new TestAgentData().new TestAgentDataObj();
TestAgentData eded = new TestAgentData();
eded.setTad(obj);
TestAgentDataObj result=eded.getTad();
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
{"userId":"vincent","favTracks":{"favourite":"15","unFavourite":"121"}}
What can be the Java object for the above JSON String?
It really depends on how you want to map it. If you're using Jackson, for example, with the default mapping settings, your classes could look something like:
class MyObject {
private String userId;
private FavTracks favTracks;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public FavTracks getFavTracks() {
return favTracks;
}
public void setFavTracks(FavTracks favTracks) {
this.favTracks = favTracks;
}
}
class FavTracks {
private String favourite;
private String unFavourite;
public String getFavourite() {
return favourite;
}
public void setFavourite(String favourite) {
this.favourite = favourite;
}
public String getUnFavourite() {
return unFavourite;
}
public void setUnFavourite(String unFavourite) {
this.unFavourite = unFavourite;
}
}
One remark: in your current example, the favourite and unFavourite properties are of a string type. Maybe a numeric type is more suitable?
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Hello I have the following problem: I want to create an arraylist and want to add some items.
But somehow the .add Method is not there.
import java.util.ArrayList;
public class Chairing{
private int numbers;
ArrayList<Chairs>myList = new ArrayList<Chairs>();
myList.add(5,new Chairset("10"));
}
public class Chair{
int price;
String info;
public Chair(int price, Chairset c){
this.price = price;
info = c.getInfo();
}
}
public class Chairset{
String info;
public Chairset(String id){
id = info;
}
}
For some Reasons I can't add something in my new ArrayList. The constructor for Chair needs a price and an object Chairset. Chairset needs an id.
The problem is your classes have no common type, the tightest generic bound the list can have would be Object. Either use a marker interface, or notice the similarity between Chair and Chairset and have one extend the other - giving them a common type.
Also note that the line in your code where you add to the list is not in a legal location - it must be within a method.
Try this:
public class Chairing {
private int numbers;
List<Chairset> myList = new ArrayList<Chairset>();
public void someMethod() {
myList.add(5,new Chairset("10"));
}
}
public class Chair extends Chairset {
int price;
public Chair(int price, Chairset c){
super(c.getInfo());
this.price = price;
}
}
public class Chairset {
String info;
public Chairset(String id){
id = info;
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a variable that contains a return value.
Value:
Team ID:
111111
Founded:
Feb 13, 2014 By USER
Dispute Percentage:" 0%
Reputation: -
What I am looking to keep is stickly (11111) and save it back to the value teamID. How can I manipulate the return string to only store that value and delete the rest.
If I understand what you want, you can do something like this
String value = "Team ID:\n" + "19288568\n"
+ "Founded:\n" + "Feb 13, 2014 By MLGQA\n"
+ "\n" + "Dispute Percentage: 0%\n"
+ "Reputation: -\n";
System.out.println(value.split("\n")[1]);
Outputs
19288568
Since your returned String seems somewhat complex to me, I would suggest returning a custom object (a bean) containing the information you want, each with its own field.
By doing that, you will have a quick access to any of the fields you want, by simply calling the appropriate getter method on the returned object.
For example:
public class MyContainer {
private int teamID;
private String foundationDate;
private String foundator;
private int disputePercentage;
private int reputation;
public MyContainer() {
// Constructor code.
}
public int getTeamID() {
return teamID;
}
public void setTeamID(int teamID) {
this.teamID = teamID;
}
public String getFoundationDate() {
return foundationDate;
}
public void setFoundationDate(String foundationDate) {
this.foundationDate = foundationDate;
}
public String getFoundator() {
return foundator;
}
public void setFoundator(String foundator) {
this.foundator = foundator;
}
public int getDisputePercentage() {
return disputePercentage;
}
public void setDisputePercentage(int disputePercentage) {
this.disputePercentage = disputePercentage;
}
public int getReputation() {
return reputation;
}
public void setReputation(int reputation) {
this.reputation = reputation;
}
}
And your original returning method would look to something like this:
public MyContainer returningMethod(Object args) {
// Your code.
MyContainer bean = new MyContainer();
// Fill the container.
return bean;
}
I do not know the exact types of data you use, so feel free to adjust this example for your needs!
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have array list of type Warehouse. Each Warehouse has a stock amount. The method getStock() returns the stock level.
I have an ArrayList of Warehouse. I want to get the stock of each warehouse in the list and add it to a list.
My code:
import java.util.*;
public class Warehouses {
ArrayList<Warehouse> warehouses = new ArrayList<Warehouse>();
public Warehouses() {
warehouses.add(new Warehouse("W1", 20, "RM13 8BB"));
warehouses.add(new Warehouse("W2", 28, "RM13 8BB"));
warehouses.add(new Warehouse("W3", 17, "RM13 8BB"));
}
public void stockList() {
ArrayList<Integer> stockList = new ArrayList<Integer>();
for(Warehouse warehouse : warehouses) {
Integer stock = warehouse.getStock();
System.out.println(stock);
}
}
}
class Warehouse
{
// instance variables - replace the example below with your own
private String warehouseID;
private int warehouseStock;
private String location;
/**
* Constructor for objects of class Warehouse
*/
public Warehouse(String warehouseID, int warehouseStock, String location)
{
// initialise instance variables
warehouseID = warehouseID;
warehouseStock = warehouseStock;
location = location;
}
public int getStock(){
return warehouseStock;
}
public String getLocation() {
return location;
}
}
When I call stockList() I just get three empty values. What is wrong here?
Thanks
Assign the constructor arguments of Warehouse to the class member variables rather than re-assigning the local variables themselves
public Warehouse(String warehouseID, int warehouseStock, String location) {
this.warehouseID = warehouseID;
this.warehouseStock = warehouseStock;
this.location = location;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
class Course {
private String courseName;
ArrayList<String> students = new ArrayList<>();
private int numberOfStudents;
public Course(String courseName) {
this.courseName = courseName;
}
public void addStudent(String student) {
students[numberOfStudents] = student;//<-- Line 15
numberOfStudents++;
}
public ArrayList getStudents() {
return students;
}
public int getNumberOfStudents() {
return numberOfStudents;
}
public String getCourseName() {
return courseName;
}
}
Line 15 I am getting error "Array required, but ArrayList found.
I am unsure what to do here as I am new to strings and such.
students is declared as an ArrayList. This notation
students[numberOfStudents] = student;
only works for array types. You should use
students.add(student);
Please read the javadoc for ArrayList.
You also don't need to keep a field to hold the number of students, as
students.size();
will give you that.
If you want to use ArrrayList in your program:
public void addStudent(String student) {
students.add(student);
}
public int getNumberOfStudents() {
return students.length();
}
Also then you do not require numberOfStudents variable