How to add a Friend's name (element) into a stack from my Driver Class? [duplicate]
This question already has an answer here:
How do I print my Java object without getting “SomeType@2f92e0f4”?
10 answers
The user has to input name (ex: "jeff") and add it to the stack of friends. After that, the compiler prints the name of the friend added
(ex: print("Friend added: " + //friend's name)
Ive already tried this myself, but the compiler gives me some weird output (I enter the name "jeff", compiler prints back this:
("Friend added: Friend@3d4eac69").
Where did I go wrong? Here's my code:
Friend Class:
public class Friend{
private Stack friends;
public Friend(){
friends = new Stack(2);
}
public void addFriend(String name){
friends.push(name);
}
}
Profile class:
public class Profile{
private Friend f;
public Profile(String first, String last, int age){
f = new Friend();
}
}
Driver:
public class FBL{
private Scanner sc;
private Profile profiles;
private idx numPro;//number of profiles
public void FBL{
sc = new Scanner(System.in);
idx = -1;
nop = 0;
profiles = new Profile[3];
}
public void lolFriend(){
Friend newFriend = new Friend();
newFriend.addFriend(sc.nextLine());
System.out.println("Friend added: "+newFriend);
//main
//call lolFriend() method
}
I already have a class dedicated to Stacks, so here it is: https://pastebin.com/3y2sv4ia
java stack
marked as duplicate by Scary Wombat
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 4:08
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How do I print my Java object without getting “SomeType@2f92e0f4”?
10 answers
The user has to input name (ex: "jeff") and add it to the stack of friends. After that, the compiler prints the name of the friend added
(ex: print("Friend added: " + //friend's name)
Ive already tried this myself, but the compiler gives me some weird output (I enter the name "jeff", compiler prints back this:
("Friend added: Friend@3d4eac69").
Where did I go wrong? Here's my code:
Friend Class:
public class Friend{
private Stack friends;
public Friend(){
friends = new Stack(2);
}
public void addFriend(String name){
friends.push(name);
}
}
Profile class:
public class Profile{
private Friend f;
public Profile(String first, String last, int age){
f = new Friend();
}
}
Driver:
public class FBL{
private Scanner sc;
private Profile profiles;
private idx numPro;//number of profiles
public void FBL{
sc = new Scanner(System.in);
idx = -1;
nop = 0;
profiles = new Profile[3];
}
public void lolFriend(){
Friend newFriend = new Friend();
newFriend.addFriend(sc.nextLine());
System.out.println("Friend added: "+newFriend);
//main
//call lolFriend() method
}
I already have a class dedicated to Stacks, so here it is: https://pastebin.com/3y2sv4ia
java stack
marked as duplicate by Scary Wombat
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 4:08
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
System.out.println("Friend added: "+newFriend);
=> you attempt to print out the name of the class, an @ symbol and the hashcode of the object in hexadecimal. Because that how it works my friend. should get thestack
inside theFriend
then print out first element
– Dang Nguyen
Nov 20 '18 at 3:53
How do i print the first element of my stack thats from another class?
– lol k
Nov 20 '18 at 4:03
add a comment |
This question already has an answer here:
How do I print my Java object without getting “SomeType@2f92e0f4”?
10 answers
The user has to input name (ex: "jeff") and add it to the stack of friends. After that, the compiler prints the name of the friend added
(ex: print("Friend added: " + //friend's name)
Ive already tried this myself, but the compiler gives me some weird output (I enter the name "jeff", compiler prints back this:
("Friend added: Friend@3d4eac69").
Where did I go wrong? Here's my code:
Friend Class:
public class Friend{
private Stack friends;
public Friend(){
friends = new Stack(2);
}
public void addFriend(String name){
friends.push(name);
}
}
Profile class:
public class Profile{
private Friend f;
public Profile(String first, String last, int age){
f = new Friend();
}
}
Driver:
public class FBL{
private Scanner sc;
private Profile profiles;
private idx numPro;//number of profiles
public void FBL{
sc = new Scanner(System.in);
idx = -1;
nop = 0;
profiles = new Profile[3];
}
public void lolFriend(){
Friend newFriend = new Friend();
newFriend.addFriend(sc.nextLine());
System.out.println("Friend added: "+newFriend);
//main
//call lolFriend() method
}
I already have a class dedicated to Stacks, so here it is: https://pastebin.com/3y2sv4ia
java stack
This question already has an answer here:
How do I print my Java object without getting “SomeType@2f92e0f4”?
10 answers
The user has to input name (ex: "jeff") and add it to the stack of friends. After that, the compiler prints the name of the friend added
(ex: print("Friend added: " + //friend's name)
Ive already tried this myself, but the compiler gives me some weird output (I enter the name "jeff", compiler prints back this:
("Friend added: Friend@3d4eac69").
Where did I go wrong? Here's my code:
Friend Class:
public class Friend{
private Stack friends;
public Friend(){
friends = new Stack(2);
}
public void addFriend(String name){
friends.push(name);
}
}
Profile class:
public class Profile{
private Friend f;
public Profile(String first, String last, int age){
f = new Friend();
}
}
Driver:
public class FBL{
private Scanner sc;
private Profile profiles;
private idx numPro;//number of profiles
public void FBL{
sc = new Scanner(System.in);
idx = -1;
nop = 0;
profiles = new Profile[3];
}
public void lolFriend(){
Friend newFriend = new Friend();
newFriend.addFriend(sc.nextLine());
System.out.println("Friend added: "+newFriend);
//main
//call lolFriend() method
}
I already have a class dedicated to Stacks, so here it is: https://pastebin.com/3y2sv4ia
This question already has an answer here:
How do I print my Java object without getting “SomeType@2f92e0f4”?
10 answers
java stack
java stack
asked Nov 20 '18 at 3:44
lol klol k
113
113
marked as duplicate by Scary Wombat
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 4:08
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Scary Wombat
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 4:08
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
System.out.println("Friend added: "+newFriend);
=> you attempt to print out the name of the class, an @ symbol and the hashcode of the object in hexadecimal. Because that how it works my friend. should get thestack
inside theFriend
then print out first element
– Dang Nguyen
Nov 20 '18 at 3:53
How do i print the first element of my stack thats from another class?
– lol k
Nov 20 '18 at 4:03
add a comment |
System.out.println("Friend added: "+newFriend);
=> you attempt to print out the name of the class, an @ symbol and the hashcode of the object in hexadecimal. Because that how it works my friend. should get thestack
inside theFriend
then print out first element
– Dang Nguyen
Nov 20 '18 at 3:53
How do i print the first element of my stack thats from another class?
– lol k
Nov 20 '18 at 4:03
System.out.println("Friend added: "+newFriend);
=> you attempt to print out the name of the class, an @ symbol and the hashcode of the object in hexadecimal. Because that how it works my friend. should get the stack
inside the Friend
then print out first element– Dang Nguyen
Nov 20 '18 at 3:53
System.out.println("Friend added: "+newFriend);
=> you attempt to print out the name of the class, an @ symbol and the hashcode of the object in hexadecimal. Because that how it works my friend. should get the stack
inside the Friend
then print out first element– Dang Nguyen
Nov 20 '18 at 3:53
How do i print the first element of my stack thats from another class?
– lol k
Nov 20 '18 at 4:03
How do i print the first element of my stack thats from another class?
– lol k
Nov 20 '18 at 4:03
add a comment |
1 Answer
1
active
oldest
votes
You need to override the toString()
method of the Friend
class as follows :
@Override
public String toString() {
return "Friend [friends=" + friends + "]";
}
What are seeing now is the address of the object represented in the format
NameOfObjectType@MemoryLocationOfObject
Also, your code is not compiling change this friends = new Stack(2);
to friends = new Stack();
. There is no parameterized constructor for the class Stack
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to override the toString()
method of the Friend
class as follows :
@Override
public String toString() {
return "Friend [friends=" + friends + "]";
}
What are seeing now is the address of the object represented in the format
NameOfObjectType@MemoryLocationOfObject
Also, your code is not compiling change this friends = new Stack(2);
to friends = new Stack();
. There is no parameterized constructor for the class Stack
add a comment |
You need to override the toString()
method of the Friend
class as follows :
@Override
public String toString() {
return "Friend [friends=" + friends + "]";
}
What are seeing now is the address of the object represented in the format
NameOfObjectType@MemoryLocationOfObject
Also, your code is not compiling change this friends = new Stack(2);
to friends = new Stack();
. There is no parameterized constructor for the class Stack
add a comment |
You need to override the toString()
method of the Friend
class as follows :
@Override
public String toString() {
return "Friend [friends=" + friends + "]";
}
What are seeing now is the address of the object represented in the format
NameOfObjectType@MemoryLocationOfObject
Also, your code is not compiling change this friends = new Stack(2);
to friends = new Stack();
. There is no parameterized constructor for the class Stack
You need to override the toString()
method of the Friend
class as follows :
@Override
public String toString() {
return "Friend [friends=" + friends + "]";
}
What are seeing now is the address of the object represented in the format
NameOfObjectType@MemoryLocationOfObject
Also, your code is not compiling change this friends = new Stack(2);
to friends = new Stack();
. There is no parameterized constructor for the class Stack
edited Nov 20 '18 at 4:08
answered Nov 20 '18 at 3:58
Nicholas KNicholas K
6,16751031
6,16751031
add a comment |
add a comment |
System.out.println("Friend added: "+newFriend);
=> you attempt to print out the name of the class, an @ symbol and the hashcode of the object in hexadecimal. Because that how it works my friend. should get thestack
inside theFriend
then print out first element– Dang Nguyen
Nov 20 '18 at 3:53
How do i print the first element of my stack thats from another class?
– lol k
Nov 20 '18 at 4:03