Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 22 of 22

Thread: Invalid String Parameter Exception

  1. #1
    Member
    Join Date
    Aug 2011
    Posts
    34
    My Mood
    Inspired
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Invalid String Parameter Exception

    Hi All,

    I've encountered another issue which I need the teams help from. I am required to write a program that accepts three string values as parameters. They are entered at the command line. I need to add an error message to be the output when any non string value is entered as a parameter.

    My last program required this same feature, but with integers instead of strings as parameters.

    In that scenario, I imported java.lang.numberformatexception - and it worked beautifully.

    Is there a StringFormatException?

    Thank You for your help.

    ch103


  2. #2
    Member
    Join Date
    Aug 2011
    Posts
    34
    My Mood
    Inspired
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Invalid String Parameter Exception

    I might add that the compiler is pointing at this code as the reason for my errors. I am trying to Parse the parameters as Strings. Please let me know if I am doing this correctly.

    try
    {
    s1 = String.parseString(args[0]); // String one
    s2 = String.parseString(args[1]); // String two
    s3 = String.parseString(args[2]); // String three
    }


    catch(IllegalArgumentException S)
    {
    System.out.println("Must enter String Values Only.");
    return;

    }

    s1, s2, s3 have all been declared as strings and are set to either A, B, or C if nothing is entered.

  3. #3
    Member
    Join Date
    Aug 2011
    Posts
    34
    My Mood
    Inspired
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Invalid String Parameter Exception

    ok so i found out that parse.String doesnt even exist.

    I need to know how to get an illegal argument exception thrown when any non string parameter is passed on the command line and I am absolutely lost.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Invalid String Parameter Exception

    how to get an illegal argument exception thrown when any non string parameter is passed on the command line
    Your code should look at the arguments and if there is a problem with them, then it should use the throw statement to throw the exception you want thrown.

    What is the definition of an invalid String? Can you give some examples?

  5. #5
    Member
    Join Date
    Aug 2011
    Posts
    34
    My Mood
    Inspired
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Invalid String Parameter Exception

    Thank You for your help. Below is my code for my String Program.

    The compiling error I keep getting begins and ends with the "try catch" exception. The program has to understand when any non int value is passed as a parameter on the command line.

    For example: java ThreeStrings Alex Bob Carl

    output will order the strings.

    The trouble I have is that I cannot get my try catch exception to throw an illegalargumentexception when the following occurs:

    java ThreeStrings Alex Bob 32

    below is my code

    /** This program accepts three strings as parameters from the command line and will display an error message if anything
    other than three parameters are passed. It will determine the Greatest String and Print it out
    */

    import java.lang.IllegalArgumentException;

    public class ThreeStrings{
    /**
    This is main the method, args String array
    */
    public static void main (String args[]) {

    String s1 = null;
    String s2 = null;
    String s3 = null;

    // test arguments
    if (args.length >= 1)
    {
    s1 = args[0];
    }

    if (args.length >= 2)
    {
    s2 = args[1];
    }

    if (args.length >= 3)
    {
    s3 = args[2];
    }

    if (s1 == null)
    {
    s1 = "Alessandro";
    System.out.println("String 1 missing, setting it to 'Alessandro'\n");
    }

    if (s2 == null)
    {
    s2 = "Bernd";
    System.out.println("String 2 missing, setting it to 'Bernd'\n");
    }

    if (s3 == null)
    {
    s3 = "Colin";
    System.out.println("String 3 missing, setting it to 'Colin'\n");
    }

    try
    {
    s1 = String.format(s1); // String one
    s2 = String.format(s2); // String two
    s3 = String.format(s3); // String three
    }


    catch(IllegalArgumentException S)
    {
    System.out.println("Must enter String Values Only.");
    return;

    }

    ThreeStringsTest(s1, s2, s3);
    } // end of main

    /**
    This method compares three strings.
    @param first String
    @param second String
    @param third String
    */

    public static void ThreeStringsTest(String first, String second, String third){
    int difference1;
    int difference2;
    int difference3;

    difference1 = first.compareTo(second);
    difference2 = first.compareTo(third);
    difference3 = second.compareTo(third);
    if ((difference1 > 0) && (difference2 > 0))
    {
    if (difference3 > 0)

    {

    System.out.println("The 1st string '"+first+"' is greater than the 2nd string '"+second+"' and they are both greater than the 3rd string '"+third+"'.");

    }

    else

    {

    System.out.println("The 1st string '"+first+
    "' is greater than both the 2nd string '"+second+"' and 3rd string '"+third+"',but the second string '"+second+"' is less than the 3rd string '"+third+"'.");
    }
    }

    else {
    if ((difference1 < 0) && (difference2 < 0))
    {
    if (difference3 < 0)

    {
    System.out.println("The 3rd string '"+third+" 'is the greatest. Followed by the 2nd String'"+second+"'. They are both greater than the 1st string '"+first+"'.");
    }

    else

    {
    System.out.println("The 2nd string '"+second+" 'is the greatest. Followed by the 3rd string '"+third+"'. They are both greater than the 1st string '"+first+"'.");
    }
    }

    else {
    System.out.println(
    "All strings are the same '"+first+"'");
    }
    }
    } // end of testit
    } // end of ThreeStrings
    Last edited by ch103; November 5th, 2011 at 05:31 PM. Reason: added s1, s2, s3 in the try catch exception

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Invalid String Parameter Exception

    compiling error I keep getting
    You forgot to post the full text of the error message you get.

  7. #7
    Member
    Join Date
    Aug 2011
    Posts
    34
    My Mood
    Inspired
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Invalid String Parameter Exception

    Hi Norm, I am eternally grateful for all of your help. I went back and edited my first post. it now compiles but still does not work.

    The project still allows int to be passed as parameters and they should not be allowed to. An exception should be thrown but I do not know how to "parse" a string in the way an integer is parsed.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Invalid String Parameter Exception

    allows int to be passed as parameters
    There are no int values passed as parameters in the String[] args that are passed to main().

    What is the definition of an invalid String?
    Is "12" valid?
    Is "A1" valid?
    Is ".3" valid?

    You need a definition before you can decide if a String is valid or not.

  9. The Following User Says Thank You to Norm For This Useful Post:

    ch103 (November 5th, 2011)

  10. #9
    Member
    Join Date
    Aug 2011
    Posts
    34
    My Mood
    Inspired
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Invalid String Parameter Exception

    how do I define a String? As of now I have assumed it is any String of Char(s).

    An invalid string is an int or anyother special character.

    Thanks again Norm.

  11. #10
    Member
    Join Date
    Aug 2011
    Posts
    34
    My Mood
    Inspired
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Invalid String Parameter Exception

    Int's can be passed as arguments as my code stands right now. I do not know how I can get an exception thrown when non Int values are entered on the command line.

    None of the three examples you gave, (12, A1, .3) would be valid in my program. however, you can entered anyone of them and my code will process it, when it should kick out an error message.

  12. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Invalid String Parameter Exception

    '1' is a char

    Look at the Character class. It has lots of methods to test the type of a character. You'd have to look at them one by one.

  13. #12
    Member
    Join Date
    Aug 2011
    Posts
    34
    My Mood
    Inspired
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Invalid String Parameter Exception

    looking now!

  14. #13
    Member
    Join Date
    Aug 2011
    Posts
    34
    My Mood
    Inspired
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Invalid String Parameter Exception

    Some good news. After re-reading my program requirements I realized that my requirement was not to create an error message when non Ints were passed, but instead I need to create an error message when more than three arguments are passed.

    I want to use an ArrayIndexOutOfBoundsException. I am having a hard time writing the "try" block. My "Catch" block reads -

    try
    {

    }


    catch(ArrayIndexOutOfBoundsException S)
    {
    System.out.println("Users must enter exactly three String values.");
    return;

    }

    I need the program to end when the exception message is thrown which I assume is what it will do. What can I write for my "try" block?

    I have tried

    if (args.length !=3)

    {

    System.Out.Println(Exception Message)

    }

    And it had not worked... Any help would be appreciated.

    Thanks again!

    ch103

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Invalid String Parameter Exception

    I need the program to end when the exception message is thrown
    I'm not sure what you mean by "throwing an exception message". Messages are contained in an exception, they are not thrown. Exceptions are thrown.
    What happens when you throw the exception?

    You need to post the code that you are executing and the contents of the console window when you execute it or the error messages you get when you try to compile it.

  16. #15
    Member
    Join Date
    Aug 2011
    Posts
    34
    My Mood
    Inspired
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Invalid String Parameter Exception

    Hi Norm,

    Here is my code - whenever a user enters more or less then Three Strings i need the program to exit and display an error message. I am trying to acheive this via a try catch exception.

    Thank you again for all of your help.

    /** This program accepts three strings as parameters from the command line and will display an error message if anything
    other than three parameters are passed. It will determine the Greatest String and Print it out
    */

    import java.lang.ArrayIndexOutOfBoundsException;

    public class ThreeStrings{
    /**
    This is main the method, args String array
    */
    public static void main (String args[]) {

    String s1 = null;
    String s2 = null;
    String s3 = null;

    // test arguments
    if (args.length >= 1)
    {
    s1 = args[0];
    }

    if (args.length >= 2)
    {
    s2 = args[1];
    }

    if (args.length >= 3)
    {
    s3 = args[2];
    }

    if (s1 == null)
    {
    s1 = "Alessandro";
    System.out.println("String 1 missing, setting it to 'Alessandro'\n");
    }

    if (s2 == null)
    {
    s2 = "Bernd";
    System.out.println("String 2 missing, setting it to 'Bernd'\n");
    }

    if (s3 == null)
    {
    s3 = "Colin";
    System.out.println("String 3 missing, setting it to 'Colin'\n");
    }


    try
    {
    if (args.length != 3);
    }


    catch(ArrayIndexOutOfBoundsException S)
    {
    System.out.println("Users must enter exactly three String values.");
    return;

    }

    ThreeStringsTest(s1, s2, s3);
    } // end of main

    /**
    This method compares three strings.
    @param first String
    @param second String
    @param third String
    */

    public static void ThreeStringsTest(String first, String second, String third){
    int difference1;
    int difference2;
    int difference3;

    difference1 = first.compareTo(second);
    difference2 = first.compareTo(third);
    difference3 = second.compareTo(third);
    if ((difference1 > 0) && (difference2 > 0))
    {
    if (difference3 > 0)

    {

    System.out.println("The 1st string '"+first+"' is greater than the 2nd string '"+second+"' and they are both greater than the 3rd string '"+third+"'.");

    }

    else

    {

    System.out.println("The 1st string '"+first+
    "' is greater than both the 2nd string '"+second+"' and 3rd string '"+third+"',but the second string '"+second+"' is less than the 3rd string '"+third+"'.");
    }
    }

    else {
    if ((difference1 < 0) && (difference2 < 0))
    {
    if (difference3 < 0)

    {
    System.out.println("The 3rd string '"+third+" 'is the greatest. Followed by the 2nd String'"+second+"'. They are both greater than the 1st string '"+first+"'.");
    }

    else

    {
    System.out.println("The 2nd string '"+second+" 'is the greatest. Followed by the 3rd string '"+third+"'. They are both greater than the 1st string '"+first+"'.");
    }
    }

    else {
    System.out.println(
    "All strings are the same '"+first+"'");
    }
    }
    } // end of testit
    } // end of ThreeStrings

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Invalid String Parameter Exception

    What happens when you compile it? Any errors?
    What happens when you execute it with invalid input?

    What do you want to happen if there is invalid input?

    For testing the invalid input to the main method you only need a very short program. The rest of the code is in the way for solving the validation of the input problem.

  18. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Invalid String Parameter Exception

    Please put your code in code tags.
    Use the Go Advance button below the input box and then use the # icon on the middle line to the right.

  19. #18
    Member
    Join Date
    Aug 2011
    Posts
    34
    My Mood
    Inspired
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Invalid String Parameter Exception

    my code compiles - no errors there.

    as of now when i enter 4 strings on the command line the program compares the first three (which it is supposed to do) and ranks them. It just ignores the fourth String alltogether.

    when 4 or more strings are entered I want an error message stating "Users must enter 3 strings" to be printed and for the program to end.

  20. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Invalid String Parameter Exception

    when 4 or more strings are entered I want an error message stating "Users must enter 3 strings" to be printed and for the program to end.
    Use an if statement to test the length of the String array passed to main, print out your message and either return or call System.exit(0);

  21. #20
    Member
    Join Date
    Aug 2011
    Posts
    34
    My Mood
    Inspired
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Invalid String Parameter Exception

    ok i will - thank you, sorry for coming off as ameteurish.

    /**  This program accepts three strings as parameters from the command line and will display an error message if anything 
    other than three parameters are passed.  It will determine the Greatest String and Print it out
    */
     
    import java.lang.ArrayIndexOutOfBoundsException;
     
    public class ThreeStrings{
    	/**
    	This is main the method, args String array
    	*/
    	public static void main (String args[]) {
     
    		String s1 = null;
    		String s2 = null;
    		String s3 = null;
     
    		// test arguments
    		if (args.length >=  1) 
    		{
    			s1 = args[0];
    		}
     
    		if (args.length >=  2) 
    		{
    			s2 = args[1];
    		}
     
    		if (args.length >= 3)  
    		{
    			s3 = args[2];
    		}
     
    		if (s1 == null) 
    		{
    			s1 = "Alessandro";
    			System.out.println("String 1 missing, setting it to 'Alessandro'\n");
    		}
     
    		if (s2 == null)
    		{
    			s2 = "Bernd";
    			System.out.println("String 2 missing, setting it to 'Bernd'\n");
    		}
     
    		if (s3 == null)
    		{
    			s3 = "Colin";
    			System.out.println("String 3 missing, setting it to 'Colin'\n");
    		}
     
     
    	try
    	{
    		if (args.length != 3);
    	}	
     
     
    	catch(ArrayIndexOutOfBoundsException S)
    	{
    		System.out.println("Users must enter exactly three String values.");
    		return;
     
    	}
     
    		ThreeStringsTest(s1, s2, s3);
    	} // end of main
     
    	/**
    	This method compares three strings.
    	@param  first String
    	@param  second String
    	@param  third String
    	*/
     
    	public static void ThreeStringsTest(String first, String second, String third){
          	int    difference1;
    	int    difference2;
    	int    difference3;
     
    		difference1 = first.compareTo(second);
    		difference2 = first.compareTo(third);
    		difference3 = second.compareTo(third);
    		if ((difference1 > 0) && (difference2 > 0))  
    		{
    			if (difference3 > 0)
     
    			{
     
    			System.out.println("The 1st string '"+first+"' is greater than the 2nd string '"+second+"' and they are both greater than the 3rd string '"+third+"'.");
     
    			}
     
    			else
     
    			{
     
    			System.out.println("The 1st string '"+first+
    				"' is greater than both the 2nd string '"+second+"' and 3rd string '"+third+"',but the second string '"+second+"' is less than the 3rd string '"+third+"'.");
    			}
    		}
     
    		else {
    			if ((difference1 < 0) && (difference2 < 0))
    			{
    				if (difference3 < 0)
     
    				{
    					System.out.println("The 3rd string '"+third+" 'is the greatest.  Followed by the 2nd String'"+second+"'. They are both greater than the 1st string '"+first+"'.");
    				}
     
    				else
     
    				{
    					System.out.println("The 2nd string '"+second+" 'is the greatest.  Followed by the 3rd string '"+third+"'. They are both greater than the 1st string '"+first+"'.");
    				}
    			}
     
    			else {
    				System.out.println(
    					"All strings are the same '"+first+"'");
    			}
    		}
    	} // end of testit
    } // end of ThreeStrings

  22. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Invalid String Parameter Exception

    Where is the test for the wrong number of args? It should be the first thing in main().
    After you are sure that you have the correct number of args, then go ahead and work with them.

    Once you have verified the correct number of args, you should not get an ArrayIndexOutOfBoundsException

  23. #22
    Member
    Join Date
    Aug 2011
    Posts
    34
    My Mood
    Inspired
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Invalid String Parameter Exception

    ok everyone I need up mark this thread as complete/resolved.

    My error was that I did not place my If statement directly beneath my main method.

    Here is my code that generates an error message when more than 3 strings are passed as parameters, when only three are permitted.

    public static void main (String args[]) {
    		if (args.length > 3) 
    		{
    			System.out.println("You have exceeded the Input Limit.\n  Please enter three values only.");
    			return;
    		}

    I was making things far too complicated with trying to achieve this functionality using try/catch exceptions but hey, im still new to this and need to try things to make my code work.

    thanks for all of your help Norm, it is always appreciated.

    ch103

Similar Threads

  1. Replies: 5
    Last Post: September 5th, 2011, 10:31 AM
  2. [SOLVED] How to compare a parameter value to a string literal inside EL?
    By Lord Voldemort in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: July 28th, 2011, 06:58 PM
  3. Need help with exception handling for a string
    By toppcon in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 6th, 2011, 06:59 AM
  4. Passing JTextField as String parameter
    By Christophe in forum Java Theory & Questions
    Replies: 3
    Last Post: April 11th, 2010, 05:32 AM
  5. The character '' is an invalid XML character exception getting
    By sumanta in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 9th, 2010, 12:13 PM