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

    Unhappy Will someone pliz look through this code, I need suggestions...

    Java Code:
    //Please read through the file and help whenever yo can.Thanks in Advance!
    /*
    ****************MY PROBLEMIN THE CODE BELOW********************************************
    * 1) The ArrayIndexOutOfBoundsException is thrown no matter the number of array values splited from the input.
    * 2) How can I include the invalid unitCode in the catch block statement so that the user knows exactly which units 
    *    are/is invalid? Something like : 
    * try{.....}  //try block
    * catch (IlegalArgumentEXception e){
    *      System.out.printf("%-5s"+" is an invalid unit.",someRefToTheInvalidUnit)
    *                          id place unitCode[index]  here ^^^^^^^^^   but that would be stupid!
    *    }
    **********************MY PLEA
    *I'll be glad if you have suggestions on how I can make registerUnits( ) a better method.
    ******************************************************************************************************
    */
    
    //the method starts here
    public void registerUnits(){
           final int MAX_UNITS = 8;
            String units;
    //sample unitcodes: SPH100 SPH200 SPH202 SCH100 SMA102 SMA203
    //which are defined as enums in an enum file UniversityUnits.java included in this project
            System.out.println("Enter the codes of units to register separated with a hyphen(-):");
                units = input.next().toUpperCase();    
            try  {
               
                String unitCode [] = new String [MAX_UNITS];
                unitCode = units.split("-");
                for (int index = 0; index < MAX_UNITS;index++){
                     UniversityUnits.valueOf(unitCode[index]);
                    System.out.printf("%-5s"+" accepted!\n",unitCode[index]);
                }
             }
            catch(IllegalArgumentException e) {
                System.out.println("Wrong unit code!");
            }
                catch(ArrayIndexOutOfBoundsException iobe) {
                    System.out.println("Up to 8 units only per semester!!");
    //  I'd appreciate this >> System.out.printf("%-5s"+" is an invalid unit.",someRefToTheInvalidUnit);
    //Any suggestions regarding this ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ are impatiently awaited.  
                }
    
    */
    ***********************EXPLANATION****************************
    *registerUnits is a method that gets input from the user in the e.g SPH100-SCH100-SCH100-SPH200.
    * It spilts( with (-) as the delimiter) the input into arrays whose values are 
    *the strings SPH100  SPH200 SCH200 SCH100 e.t.c .
    *It verifies if the units are valid university units by calling the values for an enum class 
    *i.e UniversityUnits.valueOf(String toBeChecked) 
    *and checking the values of each array against the called values.An illegalArgumentException is thrown
    * if the array value[s] does not match the values returned by UniversityUnits.valueOf(unitCode[index]).
    */
    Last edited by nmvictor; 10-29-2009 at 11:17 AM.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Put printls to see the value of the String before splitting it.

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

    Unhappy

    Quote Originally Posted by r035198x View Post
    Put printls to see the value of the String before splitting it.
    Thanks,
    I did that and it prints the input exactly as I typed.
    My real problem is that the ArrayIndexOutOfBoundsEXception is thrown even when the array index are not out of bounds.So I get the message
    "Upto 8 units per semester only!"
    at the end of the execution.

    I have edited the post, you might want to read through again.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    After
    Java Code:
    unitCode = units.split("-");
    do
    Java Code:
    System.out.println(Arrays.toString(unitCode));
    to see what's going on.

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

    Angry

    Quote Originally Posted by r035198x View Post
    After
    Java Code:
    unitCode = units.split("-");
    do
    Java Code:
    System.out.println(Arrays.toString(unitCode));
    to see what's going on.

    I'm surprised and obviously confused by this output:
    //program starts executing:
    Enter the codes of units you wish to register separated with a hyphen(-).e.g SPH100-
    SMA102-SCH100-...: |
    sph100-sch100-sch200 //my input
    [null, null, null, null, null, null, null, null]
    // ^^^^^^^^^^^^^^^^^^^output of System.out.println(Arrays.toString(unitCode));
    You entered a non integer value.Choose between 1-4!
    //^^^^^^^^^^^^^^^^^^^^^^^array index out of bound exception cought by a
    //catch block in my main.java and not by the catch block in registerUnits( ) as required

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Did you print out units as well to see what was entered?
    Also if input is a Scanner then read the API specs of the next and nextLine methods to understand the differences between them.

    P.S printlns are very useful when you don't get expected results.

  7. #7
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    828
    Rep Power
    6

    Default

    Might be input entered will be lesser or greater.u do it like this and test
    get the units..dynamically assign to array like this below.
    String unitCode [] = units.split("-")

    then iterate the array and print the value to check
    for(int i=0; i< uniCode.length;i++)
    S.O.P(uniCode[i]);
    Ramya:cool:

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    No need for the loop. See my reply #4 above.

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

    Unhappy

    Quote Originally Posted by r035198x View Post
    After
    Java Code:
    unitCode = units.split("-");
    do
    Java Code:
    System.out.println(Arrays.toString(unitCode));
    to see what's going on.
    I finally came up with this
    Java Code:
    public void registerUnits(){
           final int MAX_UNITS = 8;
                System.out.println("Enter the codes of units you wish to register separated with a" +" a space.e.g SPH100 SMA102 SCH100 ...: ");
            try  {
                 String unitCode [] = new String [MAX_UNITS];
                   for(int count = 0;count < MAX_UNITS;count++){
                     unitCode[count] = input.next().toUpperCase();
                    UniversityUnits.valueOf(unitCode[count]);    
                     System.out.println(Arrays.toString(unitCode));
               }
                
             }
            
            catch(IllegalArgumentException e) {
                System.out.println("Wrong unit code!");
            }
                catch(ArrayIndexOutOfBoundsException iobe) {
                    System.out.println("Up to 8 units only per semester!!");
                }
            
           
        }
    the output is here:
    Enter the codes of units you wish to register separated with a a space.e.g SPH100 SMA102 SCH100 ...:
    sph100 sph200 sch100 sch102 sch103 sch200 sch201 sch202 sma102
    [SPH100, null, null, null, null, null, null, null]
    [SPH100, SPH200, null, null, null, null, null, null]
    [SPH100, SPH200, SCH100, null, null, null, null, null]
    [SPH100, SPH200, SCH100, SCH102, null, null, null, null]
    [SPH100, SPH200, SCH100, SCH102, SCH103, null, null, null]
    [SPH100, SPH200, SCH100, SCH102, SCH103, SCH200, null, null]
    [SPH100, SPH200, SCH100, SCH102, SCH103, SCH200, SCH201, null]
    [SPH100, SPH200, SCH100, SCH102, SCH103, SCH200, SCH201, SCH202]
    BUILD SUCCESSFUL (total time: 1 minute 24 seconds)


    it doesn't:( throw the ArrayIndexOutOfBounds Exception, in case you dint notice.
    Please give any suggestions if you can

Similar Threads

  1. Replies: 1
    Last Post: 10-27-2009, 07:33 PM
  2. Please post your suggestions about this code
    By mathansj in forum Advanced Java
    Replies: 8
    Last Post: 02-15-2009, 03:41 PM
  3. any suggestions?
    By PureAwesomeness in forum New To Java
    Replies: 4
    Last Post: 01-19-2009, 07:34 AM
  4. Suggestions for my Java-project?
    By shabbee in forum New To Java
    Replies: 1
    Last Post: 01-01-2009, 10:11 PM
  5. Suggestions of crufty or otherwise bad APIs
    By Andre in forum Forum Lobby
    Replies: 8
    Last Post: 11-17-2008, 11:06 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
  •