How to not overwrite the value at the time of inputting values in a class?









up vote
1
down vote

favorite












In this case, I input a name. I want to input a name from main class and then pass it to newClass. When I try to input another name again, I've seen that the name before is overwritten.



public class JavaApplication107 
public static void main(String args)
byte number;
NewClass obj = new NewClass();
do
System.out.println("MENU : ");
System.out.println("1. Show Data");
System.out.println("2. Input Data");
System.out.println("3. Exit");
Scanner sc1 = new Scanner(System.in);
Scanner sc2 = new Scanner(System.in);
System.out.print("Input Number : ");
number = sc1.nextByte();
switch (number)
case 1:
obj.setShowBiodata();
break;
case 2:
System.out.print("Input your name : ");
String strTmp = sc2.nextLine();
obj.setName(strTmp);
break;

while(number != 3);




Blockquote




public class NewClass 
private String mName;
void setName(String name)
mName = name;

void setShowBiodata()
System.out.println("Name : " + mName);



So I can get more than one name










share|improve this question























  • Yes? Because that is what setName does? Where else is the new (or old) name supposed to be stored - that's unclear from your description
    – UnholySheep
    Mar 11 '17 at 17:30










  • So, how I can make the method to not overwrite the value? Should I use array or another?
    – Mark Martin
    Mar 11 '17 at 17:33










  • You still haven't explained what's supposed to happen. But it sounds like you want to use an ArrayList<String> (in this case preferable over a simple array)
    – UnholySheep
    Mar 11 '17 at 17:34










  • You should use either an array or a list.
    – Austin
    Mar 11 '17 at 17:34










  • Do you just want the program to print out a list of names?
    – Austin
    Mar 11 '17 at 17:35














up vote
1
down vote

favorite












In this case, I input a name. I want to input a name from main class and then pass it to newClass. When I try to input another name again, I've seen that the name before is overwritten.



public class JavaApplication107 
public static void main(String args)
byte number;
NewClass obj = new NewClass();
do
System.out.println("MENU : ");
System.out.println("1. Show Data");
System.out.println("2. Input Data");
System.out.println("3. Exit");
Scanner sc1 = new Scanner(System.in);
Scanner sc2 = new Scanner(System.in);
System.out.print("Input Number : ");
number = sc1.nextByte();
switch (number)
case 1:
obj.setShowBiodata();
break;
case 2:
System.out.print("Input your name : ");
String strTmp = sc2.nextLine();
obj.setName(strTmp);
break;

while(number != 3);




Blockquote




public class NewClass 
private String mName;
void setName(String name)
mName = name;

void setShowBiodata()
System.out.println("Name : " + mName);



So I can get more than one name










share|improve this question























  • Yes? Because that is what setName does? Where else is the new (or old) name supposed to be stored - that's unclear from your description
    – UnholySheep
    Mar 11 '17 at 17:30










  • So, how I can make the method to not overwrite the value? Should I use array or another?
    – Mark Martin
    Mar 11 '17 at 17:33










  • You still haven't explained what's supposed to happen. But it sounds like you want to use an ArrayList<String> (in this case preferable over a simple array)
    – UnholySheep
    Mar 11 '17 at 17:34










  • You should use either an array or a list.
    – Austin
    Mar 11 '17 at 17:34










  • Do you just want the program to print out a list of names?
    – Austin
    Mar 11 '17 at 17:35












up vote
1
down vote

favorite









up vote
1
down vote

favorite











In this case, I input a name. I want to input a name from main class and then pass it to newClass. When I try to input another name again, I've seen that the name before is overwritten.



public class JavaApplication107 
public static void main(String args)
byte number;
NewClass obj = new NewClass();
do
System.out.println("MENU : ");
System.out.println("1. Show Data");
System.out.println("2. Input Data");
System.out.println("3. Exit");
Scanner sc1 = new Scanner(System.in);
Scanner sc2 = new Scanner(System.in);
System.out.print("Input Number : ");
number = sc1.nextByte();
switch (number)
case 1:
obj.setShowBiodata();
break;
case 2:
System.out.print("Input your name : ");
String strTmp = sc2.nextLine();
obj.setName(strTmp);
break;

while(number != 3);




Blockquote




public class NewClass 
private String mName;
void setName(String name)
mName = name;

void setShowBiodata()
System.out.println("Name : " + mName);



So I can get more than one name










share|improve this question















In this case, I input a name. I want to input a name from main class and then pass it to newClass. When I try to input another name again, I've seen that the name before is overwritten.



public class JavaApplication107 
public static void main(String args)
byte number;
NewClass obj = new NewClass();
do
System.out.println("MENU : ");
System.out.println("1. Show Data");
System.out.println("2. Input Data");
System.out.println("3. Exit");
Scanner sc1 = new Scanner(System.in);
Scanner sc2 = new Scanner(System.in);
System.out.print("Input Number : ");
number = sc1.nextByte();
switch (number)
case 1:
obj.setShowBiodata();
break;
case 2:
System.out.print("Input your name : ");
String strTmp = sc2.nextLine();
obj.setName(strTmp);
break;

while(number != 3);




Blockquote




public class NewClass 
private String mName;
void setName(String name)
mName = name;

void setShowBiodata()
System.out.println("Name : " + mName);



So I can get more than one name







java class variables input methods






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 15:37









Cœur

16.9k9102139




16.9k9102139










asked Mar 11 '17 at 17:25









Mark Martin

3211




3211











  • Yes? Because that is what setName does? Where else is the new (or old) name supposed to be stored - that's unclear from your description
    – UnholySheep
    Mar 11 '17 at 17:30










  • So, how I can make the method to not overwrite the value? Should I use array or another?
    – Mark Martin
    Mar 11 '17 at 17:33










  • You still haven't explained what's supposed to happen. But it sounds like you want to use an ArrayList<String> (in this case preferable over a simple array)
    – UnholySheep
    Mar 11 '17 at 17:34










  • You should use either an array or a list.
    – Austin
    Mar 11 '17 at 17:34










  • Do you just want the program to print out a list of names?
    – Austin
    Mar 11 '17 at 17:35
















  • Yes? Because that is what setName does? Where else is the new (or old) name supposed to be stored - that's unclear from your description
    – UnholySheep
    Mar 11 '17 at 17:30










  • So, how I can make the method to not overwrite the value? Should I use array or another?
    – Mark Martin
    Mar 11 '17 at 17:33










  • You still haven't explained what's supposed to happen. But it sounds like you want to use an ArrayList<String> (in this case preferable over a simple array)
    – UnholySheep
    Mar 11 '17 at 17:34










  • You should use either an array or a list.
    – Austin
    Mar 11 '17 at 17:34










  • Do you just want the program to print out a list of names?
    – Austin
    Mar 11 '17 at 17:35















Yes? Because that is what setName does? Where else is the new (or old) name supposed to be stored - that's unclear from your description
– UnholySheep
Mar 11 '17 at 17:30




Yes? Because that is what setName does? Where else is the new (or old) name supposed to be stored - that's unclear from your description
– UnholySheep
Mar 11 '17 at 17:30












So, how I can make the method to not overwrite the value? Should I use array or another?
– Mark Martin
Mar 11 '17 at 17:33




So, how I can make the method to not overwrite the value? Should I use array or another?
– Mark Martin
Mar 11 '17 at 17:33












You still haven't explained what's supposed to happen. But it sounds like you want to use an ArrayList<String> (in this case preferable over a simple array)
– UnholySheep
Mar 11 '17 at 17:34




You still haven't explained what's supposed to happen. But it sounds like you want to use an ArrayList<String> (in this case preferable over a simple array)
– UnholySheep
Mar 11 '17 at 17:34












You should use either an array or a list.
– Austin
Mar 11 '17 at 17:34




You should use either an array or a list.
– Austin
Mar 11 '17 at 17:34












Do you just want the program to print out a list of names?
– Austin
Mar 11 '17 at 17:35




Do you just want the program to print out a list of names?
– Austin
Mar 11 '17 at 17:35












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Try this code it should print out all of the names



public class NewClass 
private String mName ="";
void setName(String name)
mName = mName+"n"+name;

void setShowBiodata()
System.out.println("Name : " + mName);



Here is a way you could use an array



public class NewClass 
private String names = new String[20];
private int number = 0;
void setName(String name)
names[number]= name;
number++:

void setShowBiodata()
for (int i =0; i<number; i++)

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







share|improve this answer






















  • Thanks bro. But, can you help me if sometimes I want use an array?
    – Mark Martin
    Mar 11 '17 at 17:55










  • I want to make a name for an index
    – Mark Martin
    Mar 11 '17 at 17:56










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42738305%2fhow-to-not-overwrite-the-value-at-the-time-of-inputting-values-in-a-class%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










Try this code it should print out all of the names



public class NewClass 
private String mName ="";
void setName(String name)
mName = mName+"n"+name;

void setShowBiodata()
System.out.println("Name : " + mName);



Here is a way you could use an array



public class NewClass 
private String names = new String[20];
private int number = 0;
void setName(String name)
names[number]= name;
number++:

void setShowBiodata()
for (int i =0; i<number; i++)

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







share|improve this answer






















  • Thanks bro. But, can you help me if sometimes I want use an array?
    – Mark Martin
    Mar 11 '17 at 17:55










  • I want to make a name for an index
    – Mark Martin
    Mar 11 '17 at 17:56














up vote
1
down vote



accepted










Try this code it should print out all of the names



public class NewClass 
private String mName ="";
void setName(String name)
mName = mName+"n"+name;

void setShowBiodata()
System.out.println("Name : " + mName);



Here is a way you could use an array



public class NewClass 
private String names = new String[20];
private int number = 0;
void setName(String name)
names[number]= name;
number++:

void setShowBiodata()
for (int i =0; i<number; i++)

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







share|improve this answer






















  • Thanks bro. But, can you help me if sometimes I want use an array?
    – Mark Martin
    Mar 11 '17 at 17:55










  • I want to make a name for an index
    – Mark Martin
    Mar 11 '17 at 17:56












up vote
1
down vote



accepted







up vote
1
down vote



accepted






Try this code it should print out all of the names



public class NewClass 
private String mName ="";
void setName(String name)
mName = mName+"n"+name;

void setShowBiodata()
System.out.println("Name : " + mName);



Here is a way you could use an array



public class NewClass 
private String names = new String[20];
private int number = 0;
void setName(String name)
names[number]= name;
number++:

void setShowBiodata()
for (int i =0; i<number; i++)

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







share|improve this answer














Try this code it should print out all of the names



public class NewClass 
private String mName ="";
void setName(String name)
mName = mName+"n"+name;

void setShowBiodata()
System.out.println("Name : " + mName);



Here is a way you could use an array



public class NewClass 
private String names = new String[20];
private int number = 0;
void setName(String name)
names[number]= name;
number++:

void setShowBiodata()
for (int i =0; i<number; i++)

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








share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 11 '17 at 18:00

























answered Mar 11 '17 at 17:45









Austin

584219




584219











  • Thanks bro. But, can you help me if sometimes I want use an array?
    – Mark Martin
    Mar 11 '17 at 17:55










  • I want to make a name for an index
    – Mark Martin
    Mar 11 '17 at 17:56
















  • Thanks bro. But, can you help me if sometimes I want use an array?
    – Mark Martin
    Mar 11 '17 at 17:55










  • I want to make a name for an index
    – Mark Martin
    Mar 11 '17 at 17:56















Thanks bro. But, can you help me if sometimes I want use an array?
– Mark Martin
Mar 11 '17 at 17:55




Thanks bro. But, can you help me if sometimes I want use an array?
– Mark Martin
Mar 11 '17 at 17:55












I want to make a name for an index
– Mark Martin
Mar 11 '17 at 17:56




I want to make a name for an index
– Mark Martin
Mar 11 '17 at 17:56

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42738305%2fhow-to-not-overwrite-the-value-at-the-time-of-inputting-values-in-a-class%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

27

Top Tejano songwriter Luis Silva dead of heart attack at 64

Category:Rhetoric