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
java class variables input methods
|
show 1 more comment
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
java class variables input methods
Yes? Because that is whatsetNamedoes? 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 anArrayList<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
|
show 1 more comment
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
java class variables input methods
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
java class variables input methods
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 whatsetNamedoes? 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 anArrayList<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
|
show 1 more comment
Yes? Because that is whatsetNamedoes? 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 anArrayList<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
|
show 1 more comment
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]);
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
add a comment |
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]);
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
add a comment |
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]);
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
add a comment |
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]);
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]);
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Yes? Because that is what
setNamedoes? 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