[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

java creating

Posted on 2014-08-15
7
Medium Priority
?
261 Views
Last Modified: 2014-08-16
Hi Guys, I am a nob to this so sorry if I am incorrect in terminology.

So I have a variable int j, which I set to 0
I have a variable String tempS which I place a value in here, say e0, e1, e2 etc etc for a loop

I want to use the constructor method from the class Clipmap  and I want to dynamically create new objects, named e0 etc...

how can I do this?

for (CellEntry cell : cellFeed.getEntries()) {
     tempS = "e" + Integer.toString(j);
     Clipmap <tempS> = new Clipmap(cell.getCell().getValue());
     j++;
}

Open in new window

0
Comment
Question by:adamc817
  • 4
  • 3
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 40264303
That's not possible, you should probably use  Clipmap[] or List<Clipmap>
0
 

Author Comment

by:adamc817
ID: 40264324
The problem is if I make it an array I would have to use a multi dimensional and some data is text other is string (I would like not to keep it all in string)

A ArrayList is flat correct?


You can't dynamically make constructors, that is so sad :(
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 40264329
Then you need an array or List of a dedicated class that can hold all the data your require
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:adamc817
ID: 40264352
Ohhh, that sounds awesome! :)

to keep things simple:

My constructor in Clipmap class:
	public Clipmap(String n, String v, int s, int m, int y, int d){
		name = n;
		version = v;
		dbSize = s;
		month = m;
		day = d;
		year = y;
	}

Open in new window



Example of a set:
	public void setName(String n){
		name =n;
	}

Open in new window



I try (to keep it simple):

Clipmap [] mylist = new Clipmap[3];

mylist[1].setName("hi");

Open in new window


I get:
Exception in thread "main" java.lang.NullPointerException
      at MySpreadsheetIntegration.main(MySpreadsheetIntegration.java:56)

56 is the line where I try and use the set, I don't think I can use the constructor can I?
0
 

Author Comment

by:adamc817
ID: 40264390
figured it out                                      

 Clipmap [] a = new Clipmap[3];
a[0] = new Clipmap("hi");      

give me a few and think i'll close this out.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40264763
Your class should probably look like the following, allowing you to have Clipmap[] or List<Clipmap>:

public class Clipmap {
    private String name;
    private String version;
    private int dbSize;
    private int month;
    private int year;
    private int day;

    public Clipmap() {
    }

    public Clipmap(String name, String version, int dbSize, int month,
        int year, int day) {
        this.name = name;
        this.version = version;
        this.dbSize = dbSize;
        this.month = month;
        this.year = year;
        this.day = day;
    }

    public String getName() {
        return this.name;
    }

    public String getVersion() {
        return this.version;
    }

    public int getDbSize() {
        return this.dbSize;
    }

    public int getMonth() {
        return this.month;
    }

    public int getYear() {
        return this.year;
    }

    public int getDay() {
        return this.day;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public void setDbSize(int dbSize) {
        this.dbSize = dbSize;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public String toString() {
        return String.format("%s=%s,%s=%s,%s=%d,%s=%d,%s=%d,%s=%d", "name",
            name, "version", version, "dbSize", dbSize, "month", month, "year",
            year, "day", day);
    }
}

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40265485
:)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month19 days, 5 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question