Jump to content

String[]

- - - - -

  • Please log in to reply
6 replies to this topic

#1
qutazs

qutazs

    Newbie

  • Members
  • PipPip
  • 23 posts
Hello,

I'm having issues with the following code:
public void setPicture(String[] picture) {

        if (picture == null) {

            this.picture = new String[] { null };

        } else {

            this.picture = new String[picture.length];

            for(int i = 0;i<picture.length;i++) {

            this.picture[i] = picture[i];

            }

        }

    }

When I try to do the following in my main it underlines it red:
    public static void main(String[] args) {

        Person kasper = new Person();

        Person newton = new Person();

        kasper.setPicture({"test"}); // Red underline

    }

How do I give the input for this? :/

I have a field:
private String[] picture;


#2
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
This:
public void setPicture(String[] picture)

Takes a String Array argument.

kasper.setPicture({"test"});

I don't think {"test"} qualifies as a string array.

a safer way:
public class Person {

    private String[] picture;

    public void setPicture(String[] picture) 
    {
        if (picture == null) {
            this.picture = new String[] { null };
        } else {
            this.picture = new String[picture.length];
            for(int i = 0;i<picture.length;i++) {
            this.picture[i] = picture[i];
            }
        }
    }
    public static void main(String args[])
    {
        Person kasper = new Person();
        Person newton = new Person();
        String [] test = {"Tdoay", "Tomorrow", "TheDayAfter"};

        kasper.setPicture( test); 
    }
}

Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#3
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
Java allows you to use shortcuts sometimes, when declaring something.
You can declare Strings like that String[] people = {"Hans", "Grete"};
And java compiler understands that you actually meant String[] people = new String[]{"Hans", "Grete"};
But i guess it's not okay write it directly in the method as the argument setPicture({"test"});.
So instead you should use setPicture(new String[]{"test"});.

I wasn't actually aware, that you couldn't write it directly. I guess you rarely, if ever, need to pass constants to your methods like that.
.

#4
qutazs

qutazs

    Newbie

  • Members
  • PipPip
  • 23 posts
Thanks both of you. :-)

I have another question.. If I make a person:
Person kasper = new Person();

and then:
kasper.setPicture(new String[] {"test", "test2", "test3"});

if I want other persons like:
Person newton = new Person();

to be able to use a viewPicture method to see another persons picture, how can this be done?
I tried with something like this (this wont work obviously.. But what does work?):

public void viewPicture(Person name) {

    for(int i=0;i<picture.length;i++) {

        System.out.println(picture[i]);

    }

}

But it doesn't work.. How would I go about this?

---------- Post added at 10:06 PM ---------- Previous post was at 08:37 PM ----------

public void viewPicture(Person name) {

        if(name.picture[0] == null) {

            System.out.println(name + " got no picture.");

        } else {

        for(int i=0;i<name.picture.length;i++) {

            System.out.println(name.picture[i]);

        }

        }

    }

I made it work :)

Edited by qutazs, 01 December 2011 - 12:20 PM.


#5
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
Is is safe to assume you are iterating over all your person objects and calling that method? I ask this question because objects usually don't have access to each others data.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#6
qutazs

qutazs

    Newbie

  • Members
  • PipPip
  • 23 posts
Well its part of an assignment.. Its stated, that other persons from class Person, should be able to view other persons pictures, but not change them.

Anyway, I ran into another issue..

public boolean sendMessage(Person receiver, String message) {

        Message msg = new Message(Person.this.getName(), message);

        if(receiveMessage(msg)) {

            return true;

        }

        else {

            return false;

        }

    }

    

    public boolean requestFriendship(Person otherPerson) {

        Message msg = new Message(Person.this.getName(), "/friends");

        if (receiveMessage(msg)) {

            friends.add(otherPerson.getName());

            return true;

        } else {

            enemies.add(otherPerson.getName());

            return false;

        }

        

    }

    

    private boolean receiveMessage(Message message) {

        if(2+2==4) {

        message.getAuthor();

        }

        return true;

    }

if I do (in my main) a:

Person person1 = new Person("Einstein");

Person person2 = new Person("Newton");

person1.sendMessage(person2, "test");

person2.receiveMessage(msg); // This is the real problem atm.. I don't know what I should wrote instead of 'msg'


the commented part is where there is a issue. My methods create a Message called msg, but I don't know how to view the message again.

---------- Post added at 07:34 PM ---------- Previous post was at 07:32 PM ----------

It refers to this class:
/**

 *

 * @author rooty

 */


public class Message {

    

    String author;

    String text;

    

    public Message(String author, String text) {

        this.author = author;

        this.text = text;

    }

    

    public String getAuthor() {

        return author;

    }

    

    public String getMessage() {

        return text;

    }

    

}



#7
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts

Quote

......to be able to use a viewPicture method to see another persons picture, how can this be done?

Examine this class:
public class Human {
    
    int age;
    String dob;
    char sex;
    
    public Human(int age, String dob, char sex)
    {
        this.age = age;
        this.dob = dob;
        this.sex = sex;
    }
    
    public void setAge(int age)
    {
        this.age = age;
    }
    public int getAge()
    {
        return age;
    }

    public static void main(String args[])
    {
        ArrayList <Human> list = new ArrayList<Human>();
        Human fread = new Human(22,"1-1-2011",'M');
        Human qutazs = new Human(21,"1-2-2011",'F');       
       
        
        list.add(fread);
        list.add(qutazs);         
    }
}
As you can see there are two handles fread an qutazs. Each handle has access to its own of variables and methods. Now although these handles have a similar set of variables and methods, they do not have access to each others data.
When you say:
qutazs.mutator();
qutazs.accessor();
you only get access to your copy of the data. You cannot directly access minds, unless you make your handle equal to minds.
Doing something like this:
qutazs = fread;
will give you direct access to my copy of the data; unfortunately at the price of you own(unless you use a temp Human variable to hold your handle, then restore).

If you wish to view data from similar objects you will need to walk through the objects in question and request whatever you need.
Think of the mess you would have if you were able to just access my data.

You can access all the data of these objects my calling them individually like this:
for(int i = 0; i < list.size(); i++)
            System.out.println(list.get(i).getAge());

As for:
person2.receiveMessage(msg); // This is the real problem atm.. I don't know what I should wrote instead of 'msg'
Try:
person2.receiveMessage(new Message(person2.getName(), "msg"));

Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users