Question about try, catch  
Author Message
digibooster@gmail.com





PostPosted: 2006-12-11 7:20:00 Top

java-programmer, Question about try, catch How do I make the code go back so that once user type more than 20
characters in the string, it will print a message and then continue to
process more strings?

Here is the code:

//StringTooLongExceptionDriver

import java.util.Scanner;
import java.io.*;

public class StringTooLongExceptionDriver
{
//
// The main method prompts and reads strings. If the string has more
// than 20 characters, it throws a StringTooLongException exception.
//
public static void main(String[] args){
//System.out.println("Enter a string: ");
String strInput = "";
int newStringCounter;
int arrayCounter=0;
char[] myCharArray = new char[20];

InputStreamReader myRead = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(myRead);

try{
while (!(strInput.equals("DONE"))){
System.out.println("enter your string: ");
int x = 0;
strInput = in.readLine();
newStringCounter = strInput.length();
if (!(strInput.equals("DONE"))){
while (newStringCounter > 0){
myCharArray[arrayCounter] = strInput.charAt(x);
newStringCounter--;
System.out.print("the last char at array is " +
myCharArray[arrayCounter]);
System.out.println(" and array counter is " + arrayCounter);
x++;
arrayCounter++;
}
}
}
if (strInput.equals("DONE")){
System.out.print("you have enterd: ");
for (int i=0; i<=arrayCounter-1; i++){
System.out.print(myCharArray[i]);
}
System.out.println("");
System.out.println("End of the string you have enterd!");
}
}
catch(Exception e){
StringTooLongException error = new StringTooLongException("Total
string character now is larger than 20 characters");
error.EndHere();
}
}
}
class StringTooLongException
{
String statement = "";
public StringTooLongException(String myError){
statement = myError;
System.out.println(statement);
}
public void EndHere(){
System.out.println("please try again");
System.exit(0);
}
}

 
trippy





PostPosted: 2006-12-11 7:36:00 Top

java-programmer >> Question about try, catch In article <email***@***.com>,
email***@***.com took the hamburger meat, threw it on the grill,
and I said "Oh Wow"...

> How do I make the code go back so that once user type more than 20
> characters in the string, it will print a message and then continue to
> process more strings?

public void checkForLength(String aString) {

if aString.length() > 20 {

system.out.print("This string is longer than 20 letters.");


}

}


--
trippy
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

NP: "Heart And Soul" -- T'Pau

"Now, technology's getting better all the time and that's fine,
but most of the time all you need is a stick of gum, a pocketknife,
and a smile."

-- Robert Redford "Spy Game"



 
Lew





PostPosted: 2006-12-11 10:16:00 Top

java-programmer >> Question about try, catch email***@***.com wrote:
> How do I make the code go back so that once user type more than 20
> characters in the string, it will print a message and then continue to
> process more strings?

Don't exit the program upon receiving an exception. Put the try {} catch {}
logic entirely inside your processing loop.

Exception classes pretty much never should control program flow, as the
System.exit() call in StringTooLongException does. Exceptions by their very
name are for exceptions, and should not take control. Think of them as clever
value objects.

You should seriously consider extending java.lang.Exception in your
StringTooLongException class.

And please do not embed TAB characters in your postings to Usenet.


> try{
> while (!(strInput.equals("DONE"))){

The try should be inside the while.

...
> }
...
> }
> catch(Exception e){
> StringTooLongException error = new StringTooLongException("Total
> string character now is larger than 20 characters");
> error.EndHere();

Of course the program will not continue now!

> }
> }
> }
> class StringTooLongException

extends Exception

> {
> String statement = "";

Tnis initialization is completely superfluous. If you extend Exception, you
don't need this member anyway.

> public StringTooLongException(String myError){

super( myError );

> statement = myError;
no longer needed

> System.out.println(statement);

Better not to embed such a statement in a constructor.

> }
> public void EndHere(){
> System.out.println("please try again");
> System.exit(0);

Not advisable from an Exception.

> }
> }
>

- Lew
 
 
digibooster@gmail.com





PostPosted: 2006-12-11 14:03:00 Top

java-programmer >> Question about try, catch Thank you so much, Lews! You are right about the structure.

 
 
digibooster@gmail.com





PostPosted: 2006-12-11 14:13:00 Top

java-programmer >> Question about try, catch Thank you so much, Lew. You are absolutely right about the structure.