Jump to content

Printing the contents of an array object

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
Jtbrown

Jtbrown

    Newbie

  • Members
  • Pip
  • 2 posts
I have created two classes, Message and Phone. I would like to print the contents of a single Message from the array e.g. System.out.printf("Number: %s\nName: %s\nMessage: %s\nTime: %s\n",Argument1, Argument2, Argument3, Argument4);

What arguments would I need to print these messages? I would like to print something along the lines of:

Number: 999 999
Message: Hello this is a test message
Name: Jack
Time: 05:10:30


I'm pretty new to programming with Java so i'm unsure do I need get / set methods, please explain anything that you can see wrong with my code that can be improved.

Message:

public class Message {

	//Instance variables

	private static String phoneNumber = "0";//Default 0 

	private static String phoneMessage = "0";//Default 0 

	private static String phoneTime = "0";//Default 0 

	private static String callerName = "0";//Default 0 

	

	//Constructor

	public Message(String number, String message, String time, String name){

		this.phoneNumber = number;

		this.phoneMessage = message;

		this.phoneTime = time;

		this.callerName = name;

	}

	public void displayMessage(){

		System.out.println("Number: " + this.phoneNumber);

		System.out.println("Message: " + this.phoneMessage);

		System.out.println("Name: " + this.callerName);

		System.out.println("Time: " + this.phoneTime);

		

	}

}


Phone:
//The phone must be able to Create, Store and delete messages

//Store the messages in an array as an object

import java.util.*;


public class Phone {


	public static void newMessage(){

		getNumber();

		

	}

	//-------------------------------Get methods-----------------------------------

	//Get the number of the client

	public static String getNumber(){

		String number;

		System.out.printf("Please enter your number:\t");

		Scanner scNumber = new Scanner(System.in);

		number = scNumber.nextLine();

		return number;

	}

	//Get the message

	public static String getMessage(){

		String message;

		System.out.printf("Please enter your message:\t");

		Scanner scMessage = new Scanner(System.in);

		message = scMessage.nextLine();

		return message;

	}

	//Get the time (in this case it wont be real time)

	public static String getTime(){

		String time = "05:10:30";

		return time;

	}

	//Get the Name

	public static String getName(){

		String name;

		System.out.printf("Please enter your name:\t");

		Scanner scName = new Scanner (System.in);

		name = scName.nextLine();

		return name;

	}

	

	//-----------------------------Displaying the message-----------------------------

	//Print the message

	public static void displayMessage(int msgArray[]){

		System.out.printf("Number: %s\nName: %s\nMessage: %s\nTime: %s\n",

				getNumber(),getName(),getMessage(),getTime());

	}

	

	public static void main (String[] args){

		//Array of Message objects (in this case maximum of 5)

		Message msgArray[] = new Message[2];

		for (int count = 0; count<msgArray.length; ){

		msgArray[count] = new Message(getNumber(),getMessage(),getTime(),getName());

		count++;

		}

	}

}



#2
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
instead of this:
System.out.printf("Number: %s\nName: %s\nMessage: %s\nTime: %s\n",Argument1, Argument2, Argument3, Argument4);

you should use:
(nb: variables must always start with lowercase letters.)

	System.out.println("Number: "+argument1);

        System.out.println("Name: "+argument2);

        System.out.println("Message: "+argument3);

        System.out.println("Time: "+argument4);




you should check out http://forum.codecal...ial-arrays.html
However, i would reccommend using ArrayList objects.... these are much easier to manipulate.

example:

                //this ArrayList can hold only "Message" type objects...

		ArrayList<Message> messages = new ArrayList<Message>();

		

		

		//creating new messages

		Message m1 = new Message("asd", "asd", "asd", "asd");

		Message m2 = new Message("asd2", "asd2", "asd2", "asd2");

		

		// add both messages to ArrayList

		messages.add(m1);

		messages.add(m2);

		

		// display all messages in ArrayList

		for(Message m: messages){

			m.display();

		}

		

		//removes first message. 

		messages.remove(0);

		// Please note, that the second message will then take first message's place, and can be removed by calling:

		messages.remove(0);

		


I hope this helps. You should also read about LinkedList.

also, there is no need to initialize fields in your Message class when you constructor requires it from the user/programmer anyway.


private static String phoneNumber; 

private static String phoneMessage; 

private static String phoneTime;

private static String callerName; 


These so called "defaults", which you had there, were useless, because there is no way to create object without initializing these, as your constructor requires these, when you call new object.

Edited by Sinipull, 03 July 2009 - 02:43 AM.


#3
Jtbrown

Jtbrown

    Newbie

  • Members
  • Pip
  • 2 posts
For some reason your example always prints m2, It might be because of my Message class. Any idea what the problem is?

Number: asd2
Message: asd2
Name: asd2
Time: asd2

Number: asd2
Message: asd2
Name: asd2
Time: asd2

//The phone must be able to Create, Store and delete messages

//Store the messages in an array as an object

import java.util.*;


public class Phone {


	public static void newMessage(){

		getNumber();

		

	}

	//-------------------------------Get methods-----------------------------------

	//Get the number of the client

	public static String getNumber(){

		String number;

		System.out.printf("Please enter your number:\t");

		Scanner scNumber = new Scanner(System.in);

		number = scNumber.nextLine();

		return number;

	}

	//Get the message

	public static String getMessage(){

		String message;

		System.out.printf("Please enter your message:\t");

		Scanner scMessage = new Scanner(System.in);

		message = scMessage.nextLine();

		return message;

	}

	//Get the time (in this case it wont be real time)

	public static String getTime(){

		String time = "05:10:30";

		return time;

	}

	//Get the Name

	public static String getName(){

		String name;

		System.out.printf("Please enter your name:\t");

		Scanner scName = new Scanner (System.in);

		name = scName.nextLine();

		return name;

	}

	

	//-----------------------------Displaying the message-----------------------------

	//Print the message

	public static void displayMessage(int msgArray[]){

		System.out.printf("Number: %s\nName: %s\nMessage: %s\nTime: %s\n",

				getNumber(),getName(),getMessage(),getTime());

	}

	

	public static void main (String[] args){

		//Array of Message objects (in this case maximum of 3)

		ArrayList<Message> messages = new ArrayList<Message>();

		//creating new messages

		Message m1 = new Message("asd", "asd", "asd", "asd");

		Message m2 = new Message("asd2", "asd2", "asd2", "asd2");

		

		messages.add(m1);

		messages.add(m2);

		

		//display all messages in ArrayList

		for(Message m: messages){

			m.displayCurrentMessage();

		}

	}

}


public class Message {

	//Instance variables

	private static String phoneNumber; 

	private static String phoneMessage;

	private static String phoneTime;

	private static String callerName;

	

	//Constructor

	public Message(String number, String message, String time, String name){

		this.phoneNumber = number;

		this.phoneMessage = message;

		this.phoneTime = time;

		this.callerName = name;

	}

	public void displayCurrentMessage(){

		System.out.println("Number: " + this.phoneNumber);

		System.out.println("Message: " + this.phoneMessage);

		System.out.println("Name: " + this.callerName);

		System.out.println("Time: " + this.phoneTime + "\n");

	}

}


#4
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
oh, how didn't i notice this before... :P

private static String phoneNumber; 
private static String phoneMessage; 
private static String phoneTime;
private static String callerName;

loose the static....

private String phoneNumber; 
private String phoneMessage; 
private String phoneTime;
private String callerName;

static means all objects share the same fields.... so if you change one, it will change other.

static fields can also be accessed without creating the actual object, like this:
       Message.phoneNumber = "3525252";

It's very useful in many ways... but not for what you are trying to do.

If i were you, i would make the ArrayList of messages static and put it inside the Message object like this:

static ArrayList<Message> messages = new ArrayList<Message>();

so it's easy to understand where it belongs and easy to access from other objects like this:

Message.messages.add(yourObject);
Message.messages.remove(yourObject);


#5
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts
I did only this....

Message Class
public class Message {

	//Instance variables

	private static String phoneNumber = "0";//Default 0 

	private static String phoneMessage = "0";//Default 0 

	private static String phoneTime = "0";//Default 0 

	private static String callerName = "0";//Default 0 

	

	//Constructor

	public Message(String number, String message, String time, String name){

		phoneNumber = number;

		phoneMessage = message;

		phoneTime = time;

		callerName = name;

	}

	public void displayMessage(){

		System.out.println("Number: " + phoneNumber);

		System.out.println("Message: " + phoneMessage);

		System.out.println("Name: " + callerName);

		System.out.println("Time: " + phoneTime);

		

	}

}
Phone Class
//The phone must be able to Create, Store and delete messages

//Store the messages in an array as an object

import java.util.*;

public class Phone {

	public static void newMessage(){

		

	}

	//-------------------------------Get methods-----------------------------------

	//Get the number of the client

	public static String getNumber(String number){

		System.out.printf("Please enter your number:\t");

		Scanner scNumber = new Scanner(System.in);

		number = scNumber.nextLine();

		return number;

	}

	//Get the message

	public static String getMessage(String message) {

		System.out.printf("Please enter your message:\t");

		Scanner scMessage = new Scanner(System.in);

		message = scMessage.nextLine();

		return message;

	}

	//Get the time (in this case it wont be real time)

	public static String getTime(String time){

		time = "05:10:30";

		return time;

	}

	//Get the Name

	public static String getName(String name){

		System.out.printf("Please enter your name:\t");

		Scanner scName = new Scanner (System.in);

		name = scName.nextLine();

		return name;

	}

	public static void displayMessage(int msgArray[]){

		System.out.printf("Number: %s\nName: %s\nMessage: %s\nTime: %s\n",

				getNumber(""),getName(""),getMessage(""),getTime(""));

	}

	

	public static void main (String[] args){

		Message m = new Message(getNumber(""),getMessage(""),getTime(""),getName(""));

		m.displayMessage();

	}

}
Output
Please enter your number:	1342

Please enter your message:	Hello

Please enter your name:	Turk

Number: 1342

Message: Hello

Name: Turk

Time: 05:10:30

So try to do whatever you want now, I don't know more or else about java :(
Posted Image

#6
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
oops... delete.

#7
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts

Sinipull said:

oops... delete.

What?
Posted Image

#8
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
Nothing, posted some stuff and then realized i was wrong..

#9
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts

Sinipull said:

Nothing, posted some stuff and then realized i was wrong..
There is nothing wrong, just misunderstanding :)
So don't mind it !
Posted Image