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
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.
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.
Re: Invalid String Parameter Exception
Quote:
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?
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
Re: Invalid String Parameter Exception
Quote:
compiling error I keep getting
You forgot to post the full text of the error message you get.
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.
Re: Invalid String Parameter Exception
Quote:
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.
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.
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.
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.
Re: Invalid String Parameter Exception
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
Re: Invalid String Parameter Exception
Quote:
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.
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
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.
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.
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.
Re: Invalid String Parameter Exception
Quote:
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);
Re: Invalid String Parameter Exception
ok i will - thank you, sorry for coming off as ameteurish.
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.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
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
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.
Code :
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