Results 1 to 4 of 4
  1. #1
    nmvictor is offline Member
    Join Date
    Oct 2009
    Posts
    31
    Rep Power
    0

    Question How do I list values of an arrays in a comma seperted list

    HI everyone, I am working on an application to help students in our university register units(well don't mind units, something like subjects to be undertaken in a particular semester) on-line.Now I have everything working up to the point of registration so definitely it would be a good idea to tell the student of not only the successful unit registration but also list the successfully registered units.During the registration, the students input is a line containing the following information
    Java Code:
     CAMPUSNAME REG-NUMBER UNIT1-UNIT2-UNIT3-...-UNIT8.
    I split the above with the code below
    Java Code:
    System.out.println("Enter CAMPUSNAME REG-NUMBER UNIT1-UNIT2-UNIT3-...-UNIT8 to register units.The units-string must be hyphen-separated and the units should not exceed 8: ")
    String studentsInput = new Scanner(System.in).nextLine( );
    
    String inputParts [ ]  = studentsInput.split( " ") ;
    /*  campus, regNumber and units were  declared as instance fields somewhere up there ^ /*
    campus = inputParts[ 0 ]; 
    regNumber = inpuParts[ 1 ];
    units = inputParts[ 2 ] ;
    
    //units is our point of interest here
    //so the units is something like SMA100-SCH300-SBC400-SPH100-SCH200
    //I further split the units with '-' as the delimiter,Note that unitCodes [ ] also declared above ^ as an instance field.
    unitCodes  = units.split ( "-") ;
    /*Now I have the students campus, regNumber and an array unitCodes the studentwishes to register, just what the function registerUnits (String _campus, String _regNumber, String _unitCodes [] ) needs, so I call the function and passes the parameters to it, and the rest is up to me.Now here is what you might be up to, registerUnits( )  did everything right and now I have to tell the student that the unit registration was successful.I can easily do that, with .../*
    
    System.out.println(regNumber+": You successfully registered  "+unitCodes.length +" units.");
    /*However, I would really appreciate listing the units in a coma separated list, so I worked this out but it just isn't taking me nowhere,
    
    I left this  commented because it isn't working any way
    
    :o
    for(int i = 0;i<unitCodes.length;i++)
           String  registeredUnits = unitCodes[ i ]+",";
    
    then i would append the registeredUnits to what I would tell the student
    
    System.out.println("You successfully registered the units: "+registeredUnits+".Thank you for using this service.);
    
    But like I said the output of the above isn't the best several modifications have just appended either the first unit "unitCodes[0]" or the first and the last units to the output,which isn't encouraging to a young programmer.Anyone with a better idea on how I would go about this?

  2. #2
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    Just to make it more easy to read. Hope thats ok?
    Java Code:
    System.out.println("Enter CAMPUSNAME REG-NUMBER UNIT1-UNIT2-UNIT3-...-UNIT8 to register units.
    The units-string must be hyphen-separated and the units should not exceed 8: ")
    
    String studentsInput = new Scanner(System.in).nextLine( );
    String inputParts [ ]  = studentsInput.split( " ") ;
    campus = inputParts[ 0 ]; 
    regNumber = inpuParts[ 1 ];
    units = inputParts[ 2 ] ;
    unitCodes  = units.split ( "-") ;
    
    System.out.println(regNumber+": You successfully registered  "+unitCodes.length +" units.");
    
    for(int i = 0;i<unitCodes.length;i++)
           String  registeredUnits = unitCodes[ i ]+",";
    
    System.out.println("You successfully registered the units: "+registeredUnits+".Thank you for using this service.);

  3. #3
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    Have you not forgott the { } on the loop? or is it just here on forum.

    Java Code:
    for(int i = 0;i<unitCodes.length;i++)
           String  registeredUnits = unitCodes[ i ]+",";

  4. #4
    nmvictor is offline Member
    Join Date
    Oct 2009
    Posts
    31
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 11-22-2009, 06:24 PM
  2. Replies: 27
    Last Post: 08-27-2009, 04:05 PM
  3. List in Combobox database values
    By Glouze in forum Advanced Java
    Replies: 1
    Last Post: 01-16-2009, 06:26 PM
  4. trying to set() values of in list of arraylist
    By alvations in forum New To Java
    Replies: 15
    Last Post: 10-13-2008, 10:35 PM
  5. Getting values of an Option List
    By mutuah in forum Advanced Java
    Replies: 0
    Last Post: 08-07-2007, 04:42 AM

Posting Permissions

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