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

The first program in this wonderful forum[SmartCalculator]

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all programmer , I am a novice programmer and I have programmed this simple calculator without a graphical interface. I am still studying Java.Possible tips to improve my code or anything else and thank you very much.

 
Marshal
Posts: 79278
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I have added code tags to your code (always use the tags: doesn't it look better ) we can see its structure. You haven't indented the code correctly: look at line 141. You shoul‍d not have two } above each other like that; there is an indentation error somewhere. And indentation errors can hide mistakes from you, so you need correct indentation.
The next thing I notice, which is very wrong, is that you have the entire code in the main method. The ideal length for the main method is one statement. All the other code shou‍ld be moved out of the main method.
Another formatting thing is that you have too much empty space: look at lines 2‑9 which only have three words in.
Look carefully at your comments. You are right to write comments, but they shou‍ld tell us what we don't know. Comments about printing messages are no use, because anybody can see that you are printing a message.
Your three “loops” are also incorrect. I am always suspicious when I see break; anywhere. What you have done is write three apparently infinite loops, and then broken out of each. That is poor practice, I am afraid.
So I think the structure of your program isn't correct. There are lots of other smaller things, but let's sort one thing out at a time.
 
alexe john
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for these tips😀. I will improve my code.Can you give me a book or anything that can help me improve🙏❤
 
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you get any error ?
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

alexe john wrote:Thank you very much for these tips😀. I will improve my code.Can you give me a book or anything that can help me improve🙏❤


Head First Java is a good place to start, then when you've been coding a while, try Effective Java, 2nd Edition.
 
Saloon Keeper
Posts: 10750
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your while() "loop" is not really being used as a loop because you only go through it once and break out at the bottom. This would be better expressed as an if(). Likewise, the next "loop" should be replaced but this time with an "else if()". Of course when you change these you'll no longer need the break.
 
Sheriff
Posts: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Choose better names for your variables. This means nothing to whoever reads it:

Anyone reading that will have to scan back over the previous code and figure out what the variables a and b represent. Ignoring the problems with the structure of the code itself, this would have been much better:

Now you have attached a meaningful word that describes the purpose of the value that it represents.

Also, please save other people the frustration of not being able to pronounce your variables. These names are horrific.

Ignore whoever or whatever source you got the advice to use variable names like that from. If you came up with those names yourself, then stop it. Do you have anything against vowels? Don't say that you're trying to save on typing out long names either because every modern IDE has a code completion feature that makes that an invalid reason and one that would likely get you rebuked in a code review.  

Choose good names for your program variables, methods, classes, etc. (← go over those search results and read the top articles).

Names in your program should not be treated lightly. They are VERY important, so choose them well. Good names give meaning to your code and make your intent clear. Code that is not meaningful is probably going to be misunderstood or worse, will have bugs in it because the programmer who wrote the code confused themselves with all the abbreviations and shortcuts.
 
Junilu Lacar
Sheriff
Posts: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're looking for a book that teaches you how to improve your code, here are a couple:

1. Clean Code by Robert C. Martin
2. Refactoring: Improving the Design of Existing Code by Martin Fowler

These should be required reading for every aspiring and practicing programmer.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic