• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Exception Stuff - Need help please... :o)

 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK guys...I'm at it again. I've got this far, and I am so proud of myself, but I am kinda stuck. See if you can give me some advice.

This is what I have so far...


public class anySentence
{
public static void main(String[] args)
throws MessageTooLongException
{

String sentence;



System.out.println("Please enter any sentence of your choice");
System.out.println("with no more than 20 characters.");
sentence = SavitchIn.readLine();
sentence.length();

{
if (sentence.length() <=20)

System.out.print("You entered "+ sentence.length());
System.out.println(" characters, which is ");
System.out.println("an acceptable length.");

}

{

if (sentence.length() >=20);
throw new MessageTooLongException();
}




}


{
{



}
}

}


___________________________________________

public class MessageTooLongException extends Exception
{
public MessageTooLongException()
{
super("Message Too Long!");
}

public MessageTooLongException(String message)
{
super(message);
}
}

I have to have an error message on my program if a user enters a sentence with more than 20 characters...it will throw the Message Too Long thing at them.
I'm almost there, just a little stuck.

Any help would be greatly appreciated.
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK I haven't tried to run your code but straight away a few errors jump out at me.
Your if statements aren't formatted correctly. try

not


There is no need for the line on it's own .. it doesn't do anything.
Lastly your logic states if sentence <= 20 do one thing and if sentence >= 20 do something. What happens if the length is exactly 20 .. both statements will be executed. Try something like


Hope this helps ... oh and for future reference please try and format your code using the "code button" bside where you get the smiley faces.
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right not your if statements aren't structured to behave like you probably think they should. As Barry pointed out the brackets are in the wrong place for what you probably wanted the program to do...

Let's take this one as an example. The braces aren't related to the if part of your logic, in this case it's just a block of code:


{
if (sentence.length() >=20);
throw new MessageTooLongException();
}


In that block it has an if statement. Without braces after the if statement then one and only one statement will be executed if the if condition is true. And in this case the one statement is empty (the semicolon after "if (sentence.length() >= 20)"). Semicolons like that can be really sneaky.

This code is more what I think you wanted:



I compiled your code and "SavitchIn" isn't defined. Maybe that wasn't where you were having the problem.
 
Carol Enderlin
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I forgot to mention I agree with Barry's suggestion to use an if-else.
 
Rose Evans
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok thanks you two, I'll see what I can do. The SavitchIn is a thing to get input from the user....
You have to have the savitchin file in the same file as your program or you'll get the savitchin compiler error. My program runs fine, it just has some flaws in it that I need to work on. I'll print out your suggestions so I can read them better, and see what I can do.

Thanks a million for your help.
 
Rose Evans
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You guys...this is the output I am getting...

Please enter any sentence of your choice
with no more than 20 characters.
I loveeeeeeeeeeeeeeeeeeee you.
characters, which is //THIS SHOULDNT BE HERE.
an acceptable length. //THIS SHOULDNT BE HERE.
Exception in thread "main" MessageTooLongException: Message Too Long!
at anySentence.main(anySentence.java:32)
Press any key to continue . . .

AND it should show the Exception in thread "main" MessageTooLOngException part before the actual message.
and it shouldnt show at anySentence.main(anySentence.java:32)....

What 'da heck am I doing wrong??
 
Carol Enderlin
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think you fixed your if statements yet.

Why don't you post your new code for us, hopefully you can copy/paste it in).

Regarding the exception...when it is thrown you aren't catching it. When you run a java program that throws an unhandled exception it prints out the stack trace.

For that matter you don't really need to throw an exception, you could just print an error message out.
 
Rose Evans
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Ok I'm hoping I did that code thing right, and please pay no attention to all those curly braces at the end of my program...I'll fix that last.
 
Rose Evans
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh Im not so sure that printed out right ahhahahaa....I tried...
 
Rose Evans
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See my output is doing what it is supposed to do if I enter a sentence with 20 or less characters...

Please enter any sentence of your choice
with no more than 20 characters.
I love you.
You entered 11 characters, which is
an acceptable length.
Would you like to enter another sentence?
Enter y for yes and n for no.

Except that if a user enters Y...it just sits there and does nothing...if they enter a n it tells them to press any key to end..like its supposed to do.
 
Carol Enderlin
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code is structured so it might LOOK like if the length of the sentence is <= 20 then those three lines would be executed, but the bracket after the if is missing so only ONE line is executed in that case. The other 2 lines are ALWAY executed.



You've got some extra unneeded braces elsewhere, too. You should clean that up so it isn't confusing.

[ February 08, 2005: Message edited by: Carol Enderlin ]
[ February 08, 2005: Message edited by: Carol Enderlin ]
 
Carol Enderlin
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Explanation about the block of code executed with curly braces vs. a single line without them from something I googled


To give a typical example
int i=0;
if (i==1){
System.out.println("Good morning");
}

The curly braces create a block of code, which means if the test is true it will execute multiple lines of code. You can use the if statement without the braces so it is only the next line that is executed conditionally, using this format the previous example would appear as
int i=0;
if (i==1)
System.out.println(" Good morning");


However, many people consider this to be bad style as it is too easy to get confused as to where the impact of the if statement starts and stops.

 
Barry Higgins
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think you have quite grasped the concept of an if-else statement.
If follows the following format


In your code you want one thing to happen if the line is less than 20 in length otherwise you want to throw an exception.

Also I don't think you should leave it until the end to clear up your eroneous curly brackets. They are bound to confuse you and are more than likely to change the actual functionality of your code.
 
Rose Evans
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Barry, Yes I understand IF statements...it's just that I can't ever seem to know exactly where to put my braces...thats the part that confuses me.
I was great with IF statements in QBasic....aced that class...but in Java..those curly braces just kill me.
I'll see what I can do with it, thanks so much for your input. I greatly appreciate it.
 
Barry Higgins
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The best way to get your head around the curly braces is to always use them in all of your constructs. Disregard the cases where you can make in-line statements ie. if/for/while etc. where there is only one statement following the construct. This code is no different functionally and offers nothing to the reader.

The only thing you have to make sure of is that you always have a curly opening brace and a closing one and that these follow your construct.
I often write an if statement with full braces first and then add the code between them. This is an effort to make sure I'm not left with this problem at the end of a long list of nested constructs.

Lastly and probably most importently to allow you to visualise the scope (start and end of curly braces for all intents and purposes) of each construct make sure your indentation is clear.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic