• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Hello, I am having trouble making try catch work with do while.

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello there,

I am following a course in C#, and am having trouble making try catch work with a do while loop.
I have searched the internet for the correct way of doing this, but it all doesn't seem to work.

Here is the code thusfar.


it is a program that shows the multiplication table of a entered number, as long as the number is between 2 and 12.
I am looking for the right code and hopefully an explanation.
( i can make try catch work without do while)

thanks in advance for any help,

Greetings,

Stefan


 
Saloon Keeper
Posts: 28319
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's kind of hard to make a try/catch work when you haven't coded either a try or catch statement.

Also, all those blank lines make your sample nearly impossible to read!
 
stefan nesic
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Tim,

I apologize for my initial post.. it was late at night and it was driving me crazy ( new at programming crazy)

Here is the code with a try catch block:




the above adding of while (number>12) doesn't work. It does freeze up the program when >12 is entered.
I have looked online for the solution, as a understand it can work with a if statement, but i couldn't make this function either.
Hopefully someone here has a answer.
If someone does, and has the time, I would also like to know how a try catch work with a for loop.

thanks for any help in advance,

Stefan.
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

stefan nesic wrote:


That while isn't part of the do-while above it - it's a separate while with an empty body. It's equivalent to this:
So either this loop ends directly if number <= 12, or it loops forever because number > 12 will always be true.
 
stefan nesic
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Rob,

I have though about what you were saying, number is either <12 or >12, its just that there is no code that is executed when  >12.
I cant find anywhere in the textbook how to code this..

am i thinking in the right direction if i make a do while loop, with a try catch, using if statements?
if so where?

I have already tried this, but i keep getting bracket problems.

Is anybody kind enough to provide the functioning code (or construction as i call it)??

thanks a lot.

Stefan.
 
stefan nesic
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have tried several things including code below, and i cant get it to work.


code:




in this code it just repeats the number if above 12...

I will work on some other parts of homework that need final touches because this is driving me crazy..
 
Marshal
Posts: 79962
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

stefan nesic wrote:. . . it was late at night . . .

You should probably then go to bed; you can only program when you are awake.
The comments add nothing to the code; it should be obvious that you are reading and multiplying two numbers.
What good does your try‑catch do? You are not going back to the original input and correcting it, and I think the error message isn't going to help the user. You can stop the method and issue a message saying to correct the input, and click the button again. Or you can wrap the try‑catch in a loop:-The first technique is probably better: put the reading in one method and the multiplication into another method.
Look up Hungarian Notation; I don't like the format of variable names like txbGetal.
 
Those cherries would go best on cherry cheesecake. Don't put those cherries on this 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