kevinb4940
asked on
Print array of objects
Hello
I am trying to print out the values of my array of objects. Been at it a couple of hours and I've decided to ask for help
public class DataBase1 {
int id;
String firstName;
String lastName;
String skill;
Boolean bool;
DataBase1(int id2, String name1, String name2, String skill2, boolean b){ //constructor
int id = id2;
firstName = name1;
lastName = name2;
skill = skill2;
bool = b;
}
public String toString(int id, String firstName, String lastName, String skill, boolean b){
return toString();
}
}
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- --
public class Server {
public static void main(String[] args) {
int size = 5;
DataBase1 staff[] = new DataBase1[size];
staff[0] = new DataBase1(1, "John", "Doe", "Java", true);
staff[1] = new DataBase1(2, "John", "Doe", "Java", true);
staff[2] = new DataBase1(3, "John", "Doe", "Java", true);
staff[3] = new DataBase1(4, "John", "Doe", "Java", true);
staff[4] = new DataBase1(5, "John", "Doe", "Java", true);
for(int i = 0; i < staff.length; i++){
System.out.println(staff[i ]);
}
System.out.println(Arrays. toString(s taff));
}
}
I am trying to print out the values of my array of objects. Been at it a couple of hours and I've decided to ask for help
public class DataBase1 {
int id;
String firstName;
String lastName;
String skill;
Boolean bool;
DataBase1(int id2, String name1, String name2, String skill2, boolean b){ //constructor
int id = id2;
firstName = name1;
lastName = name2;
skill = skill2;
bool = b;
}
public String toString(int id, String firstName, String lastName, String skill, boolean b){
return toString();
}
}
--------------------------
public class Server {
public static void main(String[] args) {
int size = 5;
DataBase1 staff[] = new DataBase1[size];
staff[0] = new DataBase1(1, "John", "Doe", "Java", true);
staff[1] = new DataBase1(2, "John", "Doe", "Java", true);
staff[2] = new DataBase1(3, "John", "Doe", "Java", true);
staff[3] = new DataBase1(4, "John", "Doe", "Java", true);
staff[4] = new DataBase1(5, "John", "Doe", "Java", true);
for(int i = 0; i < staff.length; i++){
System.out.println(staff[i
}
System.out.println(Arrays.
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks again. If you have time I've been playing around with an arraylist also. How would I print this?
ArrayList<DataBase1> callDb = new ArrayList();
callDb.add(new DataBase1(1, "John Doe", "Java", true));
callDb.add(new DataBase1(2, "Jane Dee", "Php", true));
callDb.add(new DataBase1(3, "Jim Downs", "Js", true));
callDb.add(new DataBase1(4, "Jack Jones", "C#", true));
callDb.add(new DataBase1(5, "Jill Doods", "C++", false));
ArrayList<DataBase1> callDb = new ArrayList();
callDb.add(new DataBase1(1, "John Doe", "Java", true));
callDb.add(new DataBase1(2, "Jane Dee", "Php", true));
callDb.add(new DataBase1(3, "Jim Downs", "Js", true));
callDb.add(new DataBase1(4, "Jack Jones", "C#", true));
callDb.add(new DataBase1(5, "Jill Doods", "C++", false));
public class Server {
public static void main(String[] args) {
int size = 5;
DataBase1 staff[] = new DataBase1[size];
staff[0] = new DataBase1(1, "John", "Doe", "Java", true);
staff[1] = new DataBase1(2, "John", "Doe", "Java", true);
staff[2] = new DataBase1(3, "John", "Doe", "Java", true);
staff[3] = new DataBase1(4, "John", "Doe", "Java", true);
staff[4] = new DataBase1(5, "John", "Doe", "Java", true);
List<DataBase1> callDb = new ArrayList<DataBase1>();
callDb.add(new DataBase1(1, "John", "Doe", "Java", true));
callDb.add(new DataBase1(2, "Jane", "Dee", "Php", true));
callDb.add(new DataBase1(3, "Jim", "Downs", "Js", true));
callDb.add(new DataBase1(4, "Jack", "Jones", "C#", true));
callDb.add(new DataBase1(5, "Jill", "Doods", "C++", false));
for(int i = 0; i < staff.length; i++){
System.out.println(staff[i].firstName+" "+staff[i].lastName+" "+staff[i].skill+" "+staff[i].bool);
}
for(int i = 0; i < callDb.size(); i++){
System.out.println(callDb.get(i).firstName+" "+callDb.get(i).lastName+" "+callDb.get(i).skill+" "+callDb.get(i).bool);
}
System.out.println(Arrays.toString(staff));
}
}
ASKER
Thank you very much
Are you sure you needed that? From me I mean.
ASKER
Yes It was very helpful. It's part of the bigger question. Thanks. I'm doing an intense java course and I'm struggling little but Ill catch up.
Still struggling with the toString ...............getting this output on my following program
Hello , I have attached 3 classes this question, A client class and a server class and a database class. I am trying to print a quote but I keep getting hashcode output instead of the text.
Enter command: 1:get 2:add 3:delete 0:exit
1
Enter quote number to get:
3
QuoteAnswer.DataBase1@548a 9f61
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- --
===========Client Class ============
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;
public class QuoteOfTheDayClient_2 {
public static void main(String[] args) throws IOException {
Socket echoSocket = null; // holds the Socket object
Scanner fromServer = null; // input stream from remote host
PrintWriter toServer = null; // output stream to remote host
Scanner stdIn = new Scanner(System.in);
String remoteServer = "localhost"; // are you 'local'?!
//String remoteServer = "193.61.190.96"; // remote host is not 'local'!
int port;
try {
port = 4444; // 17 for localhost
echoSocket = new Socket(remoteServer, port);
fromServer = new Scanner(echoSocket.getInpu tStream()) ;
toServer = new PrintWriter(echoSocket.get OutputStre am(), true);
} catch (Exception e) {
System.err.println("Except ion: " + e);
System.exit(1);
} // try-catch
String reply; // holds reply from remote host
int command;
System.out.println("Enter command: 1:get 2:add 3:delete 0:exit");
command = stdIn.nextInt();
while (command > 3 || command < 0) {
System.out.println("Comman d not recognised");
System.out.println("Please enter one of the following options: 1:get 2:add 3:delete 0:exit");
command = stdIn.nextInt();
}
while (command != 0) {
switch (command) {
case 1: {
// get quote
toServer.println(command);
System.out.println("Enter quote number to get: ");
int quoteNum = stdIn.nextInt();
toServer.println(quoteNum) ;
while (!(reply = fromServer.nextLine()).isE mpty()) {
System.out.println(reply); // print reply to screen
}
fromServer.nextLine(); // Consume the newline
System.out.println();
break;
}
case 2: {
// add quote
toServer.println(command);
System.out.println("Enter quote:");
stdIn.nextLine(); // flush newline
String quoteString = stdIn.nextLine();
toServer.println(quoteStri ng);
System.out.println("quoteS tring entered: " + quoteString);
break;
}
case 3: {
// delete quote
toServer.println(command);
System.out.println("Enter quote number to delete: ");
int quoteDel = stdIn.nextInt();
toServer.println(quoteDel) ;
break;
}
default:
System.out.println("*** Command not recognised ***");
break;
}
System.out.println("Enter command: 1:get 2:add 3:delete 0:exit");
command = stdIn.nextInt();
}
toServer.println(command);
echoSocket.close();
fromServer.close();
toServer.close();
stdIn.close();
} // main
} // EchoClient
===========Server Class ========================== ========== ========== ========
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class QuoteOfTheDayServer_2 {
public static void main(String[] args) throws IOException {
ArrayList quotesAL = new ArrayList <>();
quotesAL.add(new DataBase1(1, "Martin L", "King", "Quote 1", true));
quotesAL.add(new DataBase1(2, "Ablert", "Enstein", "Quote 2", true));
quotesAL.add(new DataBase1(3, "William", "Shakespeare", "Quote 3", true));
quotesAL.add(new DataBase1(4, "Mahatma", "Gandi", "Quote 4", true));
quotesAL.add(new DataBase1(5, "Winston", "Churchill", "Quote 5", false));
// Wrap the standard System.in stream with a Scanner so that
Scanner stdIn = new Scanner(System.in);
Scanner fromClient = null;
PrintWriter toClient = null;
int pNum = 4444;
ServerSocket sSocket = null;
Socket cSocket = null;
try {
sSocket = new ServerSocket(pNum);
} catch (IOException e) {
System.out.println("Couldn 't listen on port: " + pNum + "; " + e);
System.exit(1);
}
// Continuously wait for client; accept them; and send a quote to them.
while (true) {
try {
System.out.println("Waitin g for client ....");
// Blocking 'accept' i.e. wait for a client
cSocket = sSocket.accept();
System.out.println("Client connected from: " + cSocket.getInetAddress());
// Set up the output stream for the server, i.e. a PrintWriter stream
toClient = new PrintWriter(cSocket.getOut putStream( ), true);
fromClient = new Scanner(cSocket.getInputSt ream());
int command = fromClient.nextInt();
System.out.println("Receiv ed command: " + command + " from client at " + cSocket.getInetAddress());
while (command != 0) {
switch (command) {
case 1: // get quote
{
System.out.println("GET quote");
int quoteNum = fromClient.nextInt();
toClient.println(quotesAL. get(quoteN um - 1));
System.out.println("Pushed quote number: " + quoteNum + " to client at " + cSocket.getInetAddress());
//toClient.println(quotes[ quoteNum]) ; // return quote
//System.out.println(quote Num.get(i) .firstName +" "+quoteNum.get(i).lastName +" "+quoteNum.get(i).skill
// +" "+quoteNum.get(i).bool);
break;
}
case 2: // add quote
{
System.out.println("ADD quote");
fromClient.nextLine(); // flush newline
String quote = fromClient.nextLine();
System.out.println("Receiv ed quote from client:" + quote);
quote = quote + "\n\n";
quotesAL.add(quote);
System.out.println("Added quote" + quote);
break;
}
case 3: // delete quote
{
System.out.println("DEL quote");
int quoteNum = fromClient.nextInt();
System.out.println("DELeti ng quote number: " + quoteNum);
quotesAL.get(quoteNum - 1);
quotesAL.remove(quoteNum - 1);
break;
}
default:
break;
}
command = fromClient.nextInt();
System.out.println("Receiv ed command: " + command + "from client at " + cSocket.getInetAddress());
}
// Close all streams
toClient.close();
cSocket.close();
fromClient.close();
stdIn.close();
} catch (IOException e) {
System.out.println("Accept failed: " + pNum + "; " + e);
System.exit(1);
}
}
} // main
} // QuoteOfTheDayServer_2
===========Database Class ========================== ========== ========== ======
import java.util.Arrays;
public class DataBase1 {
int id;
String firstName;
String lastName;
String skill;
Boolean bool;
DataBase1(int id2, String name1, String name2, String skill2, boolean b){ //constructor
int id = id2;
firstName = name1;
lastName = name2;
skill = skill2;
bool = b;
}
public String toString(int id, String firstName, String lastName, String skill, boolean b){
System.out.println(" Id: " + id + "\n Name: " + firstName + " " + lastName + "\n Skill: " + skill + "\n Available: " + b);
return toString();
}
}
Still struggling with the toString ...............getting this output on my following program
Hello , I have attached 3 classes this question, A client class and a server class and a database class. I am trying to print a quote but I keep getting hashcode output instead of the text.
Enter command: 1:get 2:add 3:delete 0:exit
1
Enter quote number to get:
3
QuoteAnswer.DataBase1@548a
--------------------------
===========Client Class ============
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;
public class QuoteOfTheDayClient_2 {
public static void main(String[] args) throws IOException {
Socket echoSocket = null; // holds the Socket object
Scanner fromServer = null; // input stream from remote host
PrintWriter toServer = null; // output stream to remote host
Scanner stdIn = new Scanner(System.in);
String remoteServer = "localhost"; // are you 'local'?!
//String remoteServer = "193.61.190.96"; // remote host is not 'local'!
int port;
try {
port = 4444; // 17 for localhost
echoSocket = new Socket(remoteServer, port);
fromServer = new Scanner(echoSocket.getInpu
toServer = new PrintWriter(echoSocket.get
} catch (Exception e) {
System.err.println("Except
System.exit(1);
} // try-catch
String reply; // holds reply from remote host
int command;
System.out.println("Enter command: 1:get 2:add 3:delete 0:exit");
command = stdIn.nextInt();
while (command > 3 || command < 0) {
System.out.println("Comman
System.out.println("Please
command = stdIn.nextInt();
}
while (command != 0) {
switch (command) {
case 1: {
// get quote
toServer.println(command);
System.out.println("Enter quote number to get: ");
int quoteNum = stdIn.nextInt();
toServer.println(quoteNum)
while (!(reply = fromServer.nextLine()).isE
System.out.println(reply);
}
fromServer.nextLine(); // Consume the newline
System.out.println();
break;
}
case 2: {
// add quote
toServer.println(command);
System.out.println("Enter quote:");
stdIn.nextLine(); // flush newline
String quoteString = stdIn.nextLine();
toServer.println(quoteStri
System.out.println("quoteS
break;
}
case 3: {
// delete quote
toServer.println(command);
System.out.println("Enter quote number to delete: ");
int quoteDel = stdIn.nextInt();
toServer.println(quoteDel)
break;
}
default:
System.out.println("*** Command not recognised ***");
break;
}
System.out.println("Enter command: 1:get 2:add 3:delete 0:exit");
command = stdIn.nextInt();
}
toServer.println(command);
echoSocket.close();
fromServer.close();
toServer.close();
stdIn.close();
} // main
} // EchoClient
===========Server Class ==========================
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class QuoteOfTheDayServer_2 {
public static void main(String[] args) throws IOException {
ArrayList quotesAL = new ArrayList <>();
quotesAL.add(new DataBase1(1, "Martin L", "King", "Quote 1", true));
quotesAL.add(new DataBase1(2, "Ablert", "Enstein", "Quote 2", true));
quotesAL.add(new DataBase1(3, "William", "Shakespeare", "Quote 3", true));
quotesAL.add(new DataBase1(4, "Mahatma", "Gandi", "Quote 4", true));
quotesAL.add(new DataBase1(5, "Winston", "Churchill", "Quote 5", false));
// Wrap the standard System.in stream with a Scanner so that
Scanner stdIn = new Scanner(System.in);
Scanner fromClient = null;
PrintWriter toClient = null;
int pNum = 4444;
ServerSocket sSocket = null;
Socket cSocket = null;
try {
sSocket = new ServerSocket(pNum);
} catch (IOException e) {
System.out.println("Couldn
System.exit(1);
}
// Continuously wait for client; accept them; and send a quote to them.
while (true) {
try {
System.out.println("Waitin
// Blocking 'accept' i.e. wait for a client
cSocket = sSocket.accept();
System.out.println("Client
// Set up the output stream for the server, i.e. a PrintWriter stream
toClient = new PrintWriter(cSocket.getOut
fromClient = new Scanner(cSocket.getInputSt
int command = fromClient.nextInt();
System.out.println("Receiv
while (command != 0) {
switch (command) {
case 1: // get quote
{
System.out.println("GET quote");
int quoteNum = fromClient.nextInt();
toClient.println(quotesAL.
System.out.println("Pushed
//toClient.println(quotes[
//System.out.println(quote
// +" "+quoteNum.get(i).bool);
break;
}
case 2: // add quote
{
System.out.println("ADD quote");
fromClient.nextLine(); // flush newline
String quote = fromClient.nextLine();
System.out.println("Receiv
quote = quote + "\n\n";
quotesAL.add(quote);
System.out.println("Added quote" + quote);
break;
}
case 3: // delete quote
{
System.out.println("DEL quote");
int quoteNum = fromClient.nextInt();
System.out.println("DELeti
quotesAL.get(quoteNum - 1);
quotesAL.remove(quoteNum - 1);
break;
}
default:
break;
}
command = fromClient.nextInt();
System.out.println("Receiv
}
// Close all streams
toClient.close();
cSocket.close();
fromClient.close();
stdIn.close();
} catch (IOException e) {
System.out.println("Accept
System.exit(1);
}
}
} // main
} // QuoteOfTheDayServer_2
===========Database Class ==========================
import java.util.Arrays;
public class DataBase1 {
int id;
String firstName;
String lastName;
String skill;
Boolean bool;
DataBase1(int id2, String name1, String name2, String skill2, boolean b){ //constructor
int id = id2;
firstName = name1;
lastName = name2;
skill = skill2;
bool = b;
}
public String toString(int id, String firstName, String lastName, String skill, boolean b){
System.out.println(" Id: " + id + "\n Name: " + firstName + " " + lastName + "\n Skill: " + skill + "\n Available: " + b);
return toString();
}
}
Before we go any further, I would strongly recommend you put your code into the code tags. The Big Boys often don't look at code that is just dumped in the body.
ASKER
Thank you very much you have really helped. So i do not need to use a toString method??