Results 1 to 6 of 6
  1. #1
    Ms.Ranjan is offline Member
    Join Date
    Jun 2008
    Posts
    85
    Rep Power
    0

    Default 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());
    }
    }

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    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.

  3. #3
    Reiyn is offline Member
    Join Date
    Sep 2008
    Posts
    29
    Rep Power
    0

    Default

    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...
    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);
        }
    
    }
    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:
    for ( iListCount = 0; iListCount < sNames.length; iListCount++)
            {
                if (sNames.length == 1)
                {
                sOutput = sOutput + " " + sNames[iListCount];
                }
                else if (sNames.length > 1)
                {
                    sOutput = sOutput + " " + sNames[iListCount] + ", ";
                }
                }
    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..
    thanks for answering the first, and helping with the second

  4. #4
    rama.katreddy is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Default

    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(";")));
    }
    }

  5. #5
    Ms.Ranjan is offline Member
    Join Date
    Jun 2008
    Posts
    85
    Rep Power
    0

    Default

    Hi Rama and xcallme

    Thanks a lot,i solved the issue using
    Java Code:
    public ArrayList<String> stud=new ArrayList<String>();
    public String studname;
    public void setGene(String studname) {
     	   stud.add(studname); 	  
                   }
    anyway Rama i will try to use your way..

    Thanks

  6. #6
    Ms.Ranjan is offline Member
    Join Date
    Jun 2008
    Posts
    85
    Rep Power
    0

    Default

    sorry its actually

    Java Code:
    public void setStud(String studname)

Similar Threads

  1. Modify context root in application.xml
    By Saurabh321 in forum New To Java
    Replies: 1
    Last Post: 04-17-2009, 11:12 AM
  2. How to Modify,Delete data in File Txt???
    By hungleon88 in forum Advanced Java
    Replies: 9
    Last Post: 09-24-2008, 03:19 AM
  3. My code is not working properly ..modify it
    By Shyam Singh in forum New To Java
    Replies: 14
    Last Post: 07-16-2008, 05:48 PM
  4. How to modify HTML title tag
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-07-2008, 04:44 PM
  5. Modify A* Algorithm
    By prakharbirla in forum Advanced Java
    Replies: 1
    Last Post: 02-13-2008, 06:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •