• Post Reply Bookmark Topic Watch Topic
  • New Topic

Dividing Array or separate Ints  RSS feed

 
Cory Marcus
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
I'm meant to be making code which can take 5 numbers> see if they can be divided by 3> if they can, add these together. If they can't, they're ignored. The result of the sum should then be printed to the console.
This is what I've got so far. It was an Array at first, but I've never used one, and became confused when I ran into trouble. Any help is appreciated. I realize that there is most likely a much tidier solution to testing each numbers ability to be divided by 3, but I'm very new to this.



 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly is your problem? The code you have posted is not doing what was requested because it isn't adding together the numbers that are divisible by 3 and printing the sum of those numbers. Is there a reason you haven't done that?

Also, the code you have posted has a lot of unecessary repetition in it. Notice how your code for checking if each number is divisible by 3 differs only in the name of the variable it is checking. There is a better way to do this that doesn't need a different variable for each number.

Have you learnt about loops yet?
 
Cory Marcus
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm aware that the code is unfinished.
I've simply finished the "Divide by 3" section, and thought I'd ask for advice, because it looks messy.

We've brushed on loops, although as this is my 6 day of a 5 month course, I'm still an amateur.
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After you get hold of loops you can restructure your logic.
For now this logic is OK.
And UseCodeTags <- This is a link. (go through this). It will make your code more readable after posting.
One advice for now...Your else conditions are missing braces. Never ever omit the curly braces, even for single line statement. It is a good programming practice.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I ahve added code tags to your post. Always use the tags: doen't it look better. I also removed some of the unnecessary empty lines. Unfortunately it highlights inconsistent indentation and probably inconsistent {}s. Look at lines 41‑42. I have my own way to maintain indentation and pair up {}s. You have to set the automatic indentation and automatic conversion of 1 tab → 4 spaces on your text editor and then you write backwards.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should not be doing all that lot in the main method. You should consider methods which test whether the number divides by 3, print it, etc.

You know you can delete all those else statements and not notice the difference because System.out.print(""); has no noticeable effect on anything? Except that it takes a bit of time to execute.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know how to add numbers together and store the result?
 
Cory Marcus
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see.
I'll edit the else statements. Thanks for the suggestions!

As of yet, I'm unsure how to add the numbers together and store the result.



 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you do know how to declare variables (because you have declared 5 variables in the code you posted). You also initialized them with values.

So the only thing left is how to do some addition. The Oracle tutorial will help you with that.

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
 
Cory Marcus
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, so just providing an update. I've actually got my head around Arrays.. and I don't know why I wasn't using one earlier. Pretty silly, really.

Here's my new code! Any feedback is helpful.

 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks pretty good. Do your instructions specify that you need to use an array? If so you're nearly there.

Just some minor points:

1) You have created an array with size 10, but yo know you only need 5 elements. You should create the array with size 5. Currently when you loop over your array you're going to have 5 extra elements all of value 0. Your sum will be correct because 0 won't increase it, but technically you will be including values the user didn't enter.

2) You can declare and initialize sum in one statement, rather than using two statements on separate lines.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote: . . .
1) You have created an array with size 10, but yo know you only need 5 elements. You should create the array with size 5. Currently when you loop over your array you're going to have 5 extra elements all of value 0. Your sum will be correct . . .
But you will get 0 printed out five times.

You can create the array like this:-
int[] anArray = {scan.nextInt(), scan.nextInt(), scan.nextInt(), scan.nextInt(), scan.nextInt()};
That will become very tedious if you have many elements.

Or you can loop through the array (probably with a for statement) assigning from the Scanner as you go. Don't use a for‑each loop for such assignments.
In Java8 you might try a Stream. There are examples here. That will require changes for reading ints rather than lines.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!