• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help in understanding code

 
matthew aldron
Greenhorn
Posts: 11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello my name is matthew and I am new to java,

I followed a simple tutorial for working out the average of 10 numbers.

here is the original code



which works

i then modified to the code to change the first and last user prompt, so it says "enter first number" for the first and "enter last number" for the last,




This also works.

Then I modified the code to validate the user input to check if integer,


The validation works for the first integer, but it does not work for the other 9 grade inputs(if that makes sense)

any help would be very much appreciated.

Thanks

Matthew
 
Carey Brown
Bartender
Posts: 2231
27
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once you successfully enter the first int then "isInt" gets set to true and never reset.

You have a lot of code redundancy.

 
matthew aldron
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok I figured out a couple of things wrong,

I changed the boolean "isInt"  back to false after each input
and I also did not have a user prompt asking for the grades 2-9

so I changed my code to this



What do you mean by code redundancy?  I am very new to programming; do you mean I could shorten my code and clean it up?

Thank you for reply

Matthew
 
Knute Snortum
Sheriff
Posts: 3253
82
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are several other issues with your code:

1) Don't write something == true just write something.  Don't write something == false, just write !something.

2) You have everything in main.  Look at this Wiki: MainIsAPain (that's a link).
3) You should write a method that gets an integer from the keyboard and call that method.
 
matthew aldron
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ah ok I see your code now, much cleaner and simpler, thanks
 
Knute Snortum
Sheriff
Posts: 3253
82
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Redundancy means you have written the same code many times.  This violates the DRY principle (don't repeat yourself).  That's why I recommended that you create a method to get an integer from the keyboard (that is, STDIN).
 
matthew aldron
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:There are several other issues with your code:

1) Don't write something == true just write something.  Don't write something == false, just write !something.

2) You have everything in main.  Look at this Wiki: MainIsAPain (that's a link).
3) You should write a method that gets an integer from the keyboard and call that method.


thanks for this, it makes everything neater and makes sense

 
matthew aldron
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I only have everything in main at the moment as I am very new to java and dont quite fully understand object orientated programming yet (just getting used to it)
 
matthew aldron
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies, I am now going to look at writing methods

I am starting a degree in Computing & IT in January and java is the language I have to use.

do you think its ! a good language to start with?

 
Junilu Lacar
Sheriff
Posts: 9918
103
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's as good as any to start with, IMO. However, when they have you writing a lot of static methods like main() for the better part of your introduction to Java, it gets you into bad habits that just make it all the more confusing and difficult to move into proper object-oriented thinking. OOP is only as difficult as you make it. IMO, there are many ways to keep it simple to start with and gradually build up as you gain more familiarity and comfort in the OO way of thinking about programs. Come back here often and we can guide you. You seem like someone who will not have a hard time getting it.
 
Knute Snortum
Sheriff
Posts: 3253
82
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although Java is a little verbose, it is still a good language to start with -- and to end with!  It will take you from beginner to seasoned developer without a problem. 
 
Junilu Lacar
Sheriff
Posts: 9918
103
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am quite busy today, getting ready for Christmas but if you have time to check back here in a few days, I will lay out an informal course of study for you that will help you get ready for your classes in January.
 
matthew aldron
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:I am quite busy today, getting ready for Christmas but if you have time to check back here in a few days, I will lay out an informal course of study for you that will help you get ready for your classes in January.


ok thank you that would be brilliant

thank you so much and enjoy your christmas
 
matthew aldron
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to all replies thank you so much for your help so far, its replies like this that wont put beginners off (as ive had more elitist replies on other sites and it makes you feel like you should not be learning)
have a good christmas

regards

matthew
 
Martin Machajewski
Greenhorn
Posts: 1
Eclipse IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A bit offtopic but from my experience i can tell :
1.Java for dummies is a very good book for beginners. It teaches you java+oop.
2.Try to write small programms like a calculator or a programm that will load a text File and searches for strings etc.
3.Dont use tutorials, use Forums to ask if your Code is correct or why something is wrong.
4.Never give up, as an example: you did Not learned english in 1 year(i hope you are still learning)
 
matthew aldron
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Machajewski wrote:A bit offtopic but from my experience i can tell :
1.Java for dummies is a very good book for beginners. It teaches you java+oop.
2.Try to write small programms like a calculator or a programm that will load a text File and searches for strings etc.
3.Dont use tutorials, use Forums to ask if your Code is correct or why something is wrong.
4.Never give up, as an example: you did Not learned english in 1 year(i hope you are still learning)


thanks for advice I will get the book and create my own projects

and I will post any problems here as it is a very friendly forum, which means a lot for a beginner

I know I will get there because I enjoy it and I am a fast learner.

thanks
 
Knute Snortum
Sheriff
Posts: 3253
82
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and Welcome to the Ranch, Martin Machajewski.
 
matthew aldron
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
Junilu Lacar
Sheriff
Posts: 9918
103
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew,

Here's what I'd advise for learning Java and OOP properly:

1. Move away from static methods and fields as quickly as possible. Most introductory books and courses give examples that use static methods and fields exclusively. Keep in mind that the goal of those examples is to demonstrate certain language constructs and capabilities like how if-statements or for-loops work. They often don't show how OO programs in general and Java programs in particular should be written. If you get used to writing Java programs made up entirely of static methods, you're going to find the switch to proper OO programming more difficult and confusing, IMO. You should especially avoid writing all your code in main(). We have a wiki page that explains why Main Is A Pain and what you can do about it.

This is a more OO version of the traditional HelloWorld program:

It's not any more difficult to understand than the traditional version that only has the static main method.

2. Programming is about problem solving. Learn problem solving techniques. The book by V. Anton Spraul, Think Like a Programmer (No Starch Press, 2012) is all about problem solving techniques. I encourage you to get that book and read it. This book teaches you things that many introductory programming courses unfortunately fall short of teaching.

3. Learn to refactor your code. Programmers, even experienced ones, seldom ever write correct code right off the bat. There's always something wrong. Refactoring helps you correct those mistakes. Concentrate on these three refactorings: Rename, Extract, and Compose Method

4. When refactoring, focus on clarity, expressiveness, and organization of your code. Choose good names for your methods and variables. Read Robert Martin's Clean Code book and the advice given in Chapter 2 - Meaningful Names

Most of this has nothing to do with Java specifically but they have a lot do with programming in general. If you learn these things early on, you will have a good foundation of principles and practices that you can use in learning any new computer programming language.
 
matthew aldron
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for this information.

That should give me enough to get on with for the time being

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!