Results 1 to 6 of 6
Thread: How can I Modify set method?
- 04-21-2009, 06:04 PM #1
Member
- Join Date
- Jun 2008
- Posts
- 85
- Rep Power
- 0
How can I Modify set method?
Can someone give me suggestion on how can I modify my setmethod to accept string and store it in array.so that when i call the get method i get the array of student names?
Java Code:import java.util.ArrayList; import java.util.StringTokenizer; public class voa { String[] stud; String studname; public String[] getStud() { return stud; } public void setStud(String studname) { this.stud = studname; } public static void main(String args[]) { String sname="Sam;Steve;Rob"; voa test=new voa(); test.setStud(sname); ArrayList<String> data=new ArrayList<String>(); StringTokenizer st=new StringTokenizer(sname,";"); while(st.hasMoreTokens()) { data.add(st.nextToken()); } for(int i=0;i<data.size();i++) { test.setStud(data.get(i)); } System.out.println(test.getStud()); } }
- 04-21-2009, 06:14 PM #2
you need a voa constructor because you can attempt to manipulate a voa object. inside the construtor just like data.add(this)
Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
- 04-21-2009, 08:30 PM #3
Member
- Join Date
- Sep 2008
- Posts
- 29
- Rep Power
- 0
I am doing a similar project, please tell me what the difference in her way and mine is? We are taught to do things this way, hers is completely different.
This code will accept user input of names, then put up a msg box saying "hello" + name 1 name 2 etc...
I would like to add code to insert commas after each name if the array contains more than one, haven't worked that out yet lol..I've tried different locations for the following code to do that:Java Code:import javax.swing.JOptionPane; public class Main { /** Creates a new instance of Main */ public Main() { } public static void main(String[] args) { String sListNum, sNames[]; int iListNum, iListCount = 0; String sOutput = "Hello"; String sTempHold; sListNum = JOptionPane.showInputDialog(null, "How many people?"); iListNum = Integer.parseInt(sListNum); sNames = new String[iListNum]; mGetNames(sNames, iListCount); mDisplayNames(sOutput); } static void mGetNames(String [] sNames, int iListCount) { for ( iListCount = 0; iListCount < sNames.length; iListCount++) { sNames[iListCount] = JOptionPane.showInputDialog("Their name please?"); } } static void mDisplayNames(String sOutput) { JOptionPane.showMessageDialog (null, sOutput, "Title", JOptionPane.QUESTION_MESSAGE); System.exit(0); } }
Problem being I need to recognize the last array entry to prevent a comma showing up there, and it ignores this if it isn't in the mDisplayNames method, or returns a type error on my calling of the mDisplayNames if I put it inside my mDisplayNames method..Java Code:for ( iListCount = 0; iListCount < sNames.length; iListCount++) { if (sNames.length == 1) { sOutput = sOutput + " " + sNames[iListCount]; } else if (sNames.length > 1) { sOutput = sOutput + " " + sNames[iListCount] + ", "; } }
thanks for answering the first, and helping with the second
- 04-21-2009, 08:43 PM #4
Member
- Join Date
- Apr 2009
- Posts
- 4
- Rep Power
- 0
Ms. Ranjan...Following code splits the string and sets in the array.. I hope this is what you are in need of...
public class voa {
String[] studname;
public String[] getStud() {
return studname;
}
public void setStud(String[] studname) {
this.studname = studname;
}
public static void main(String args[])
{
String sname="Sam;Steve;Rob";
voa test=new voa();
test.setStud( (sname.split(";")));
}
}
- 04-21-2009, 10:08 PM #5
Member
- Join Date
- Jun 2008
- Posts
- 85
- Rep Power
- 0
Hi Rama and xcallme
Thanks a lot,i solved the issue using
anyway Rama i will try to use your way..Java Code:public ArrayList<String> stud=new ArrayList<String>(); public String studname; public void setGene(String studname) { stud.add(studname); }
Thanks
- 04-21-2009, 10:16 PM #6
Member
- Join Date
- Jun 2008
- Posts
- 85
- Rep Power
- 0
Similar Threads
-
Modify context root in application.xml
By Saurabh321 in forum New To JavaReplies: 1Last Post: 04-17-2009, 11:12 AM -
How to Modify,Delete data in File Txt???
By hungleon88 in forum Advanced JavaReplies: 9Last Post: 09-24-2008, 03:19 AM -
My code is not working properly ..modify it
By Shyam Singh in forum New To JavaReplies: 14Last Post: 07-16-2008, 05:48 PM -
How to modify HTML title tag
By Java Tip in forum SWTReplies: 0Last Post: 07-07-2008, 04:44 PM -
Modify A* Algorithm
By prakharbirla in forum Advanced JavaReplies: 1Last Post: 02-13-2008, 06:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks