• Post Reply Bookmark Topic Watch Topic
  • New Topic

incorporate some error messages  RSS feed

 
Joshua Braud
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please bare with me this is only my first programming class! But I needed to expand on my conversion program with developer messages that worked just fine, from Tsubo to Feet and the other way around. Now I have to incorporate some error messages in it. I am trying to put them in if the user enters a number to convert that is a negative number. I am using NetBeans 8.1 if that makes a difference. Really need help! Thanks in advanced




 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I added code tags to your post and sorted out one long line. Always do the same yourself: doesn't it look better
Always use an informative subject for your threads, and avoid writing ALL UPPER CASE, as in all websites.
What's Tsubo? Several other people have asked related questions recently. You have much too much code in your main method; it ought to be moved into other methods. If you have an incorrect value passed, how do you think you will write an error message? You are actually better off throwing an IllegalArgumentException(←link), if you know how to. Beware of System.exit, which can have nasty effects in multi‑threaded programs.
 
Joshua Braud
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tsubo is a Japanese standard of measurement for square feet. 1 Tsubo is equal to 35.58 square feet. I am a college student with  two kids and a job that I work on 50 hours a week and a wife that has health issues that require a ton of attention. Because of this I really don't have a lot of time to dive into programming like I need to but am doing my best. My college has me converting Tsubo to Square Feet and the reverse in a program, so that's probably why you have seen that here. It worked fine and I received an A for the assignment last week. This week I have to build off of that with error messages for input values that are negative or invalid. I just don't know how to do it.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joshua Braud wrote:It worked fine and I received an A for the assignment last week.
That's a good grade. Congratulations.

However, Campbell is right. Code is maintainable up to the certain point, later it just gets too complicated without structuring it properly.
Have you been taught about the methods? All that code lacking of methods use.

For instance, it would help if you'd have methods for:
  • Convert feets to tsubo, method signature and name could be -> feetsToTsubo(parameter feets)
  • Convert tsubo to feets, method signature and name could be -> tsuboToFeets(parameter tsubo)
  • Each of those two methods could have a check, if the argument, passed to the method is valid. For instance non-negative. In case it is negative - throw an exception. Or another approach is to keep asking user for the right input. For that purpose loop could be used.

    What exactly your instructions say about that part? And what subjects you have covered in the college already?
     
    Joshua Braud
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks you  for responding. I am not exactly sure what you are meaning. Could you post an example code for me. you can edit mine. The instructions were: Implement  some error checking and display an error if the user does not enter a valid number (like a letter) to convert or a negative number. school is online so not any one on one time with the teacher/professor at all!
     
    Liutauras Vilda
    Sheriff
    Posts: 4917
    334
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Joshua Braud wrote:Could you post an example code for me.


    Later...
    How the prompt method could look like. More methods better, that way is easier to cope with the amount of code you need to follow in your head.
     
    Joshua Braud
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    We also now have this option: . Implement a loop to keep allowing the user to perform multiple conversions either way I'm stuck
     
    Joshua Braud
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I tried the throw illegal arguments and it didn't work. It keeps giving me error messages. "error: cannot find symbol" and "error: incompatible types: unexpected return value"
                   
     
    Junilu Lacar
    Sheriff
    Posts: 11477
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Since you are using NetBeans, you should use the auto-format code feature to fix your code indentation. Press Ctrl+Shiift+F to do that. Do that often.

    From what I can tell, your code won't even compile, right? Properly formatting your code could have helped make the problem easier to spot.
     
    Henry Wong
    author
    Sheriff
    Posts: 23295
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Joshua Braud wrote:We also now have this option: . Implement a loop to keep allowing the user to perform multiple conversions either way I'm stuck


    Basically, the assignment say to take the code that you have done so far, and place it within  a loop. This way, when you are done with one iteration/conversion, it will go back to the beginning, and do another number.

    And BTW, I noticed that your strategy for encountering an error, is to print a message and exit the program. Obviously, you can't do the latter anymore, as you need to go back and do another iteration.

    Henry
     
    Joshua Braud
    Greenhorn
    Posts: 6
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you all very much  for responding and taking time to offer more options, some of which I didn't see until now. I eventually got it! I just had to step away for a minute and clear my head of frustration. I went back and altered the code a lot since then, pretty much started over. I took out the exit out and used a loop. Works just fine with error messages on both conversions,  wrote the paper to go with  it and it is submitted. Thanks again for all of your help. I will definitely be coming here for any problems and recommending this site/forum
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!