• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Problems with replacing indexs in an array

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,ive started learning java and im having trouble using arrays,i get the basic consept behind them but every time i need to replace values inside one of the indexes or something similler i get stuck.
Im currently doing an assigment which says "creat an array of 10 numbers,than move all the even numbers to the right side"
exe:
1,2,3,4,5,6,7,8,9,10
will show
1,3,5,7,9,2,4,6,8,10
or something like that,dosent really matter,the point is to have the even on the right side
ive tried messing around with my code but i just cant figure out how to do so,this is my current code;


Some help will be appreciated,and some genereal in dealing with arrays
 
Ranch Foreman
Posts: 49
1
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm only a beginner myself. But I will give it a try...

When the first element (i.e. at array[0]) of the array is even, you swap it with the last element of the array. Then you move on to the element in the next index (i.e. array[1]), etc.

What happens if the element you are taking from the back of the list (and you are bringing to the front) also happens to be even?
 
Sheriff
Posts: 13517
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about your algorithm for a minute. In particular, what happens when i reaches the part of the array has already been processed before? Your loop goes through the entire array -- ask yourself whether that's the correct logic. If not, when should you terminate the loop? (Hint: think about what the variable j represents)
 
Marshal
Posts: 6851
470
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi mike and welcome to the ranch!

First thing I suggest you to do, with all the projects you are going to work on - is to simplify input.

So instead of having an array of 10 numbers, start with an array of 2 numbers. Let's say 1 and 2. So having an even number on the left side, while odd number on the right. Like that: int[] A = {2, 1};

And your task is? Well, also, simplify your task to smaller tasks, so they are simpler to cope with.

Your task sounds "move all the even numbers to the right side of the array, and odd to the left."

So you can simplify your task to:
1. Figure out if number is even (or odd)
2. Swap numbers (maybe swap at indices you pass in as parameters - you'll see along the way what you need)

Get an idea?

Now, looking to your code, I don't see methods in it, apart from the mandatory main. Methods are meant to decompose bigger chunks of actions to singular actions. Look at the decomposed tasks above and think, what methods you may need.
 
Junilu Lacar
Sheriff
Posts: 13517
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, what does the System.out.println() statement on line 27 print? Hint: It's not what you probably think.
 
Liutauras Vilda
Marshal
Posts: 6851
470
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why your array is meant to store 11 numbers?
 
Liutauras Vilda
Marshal
Posts: 6851
470
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why did you declare temp variable outside for loop body? I don't see a need of it to use somewhere farther than for loop body.


So I'd start cleaning the house first, before I bring new furnitures in.
 
mike moka
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Umm..yeah ive run the debugger and noticed my logic for the whole drill is flawed,since the [indexs]i replace may be even number and once the loop gets to the [indexs]ive replaced it makes a whole mess out of everything.
And as far as Methods go,didnt learn it yet.
So im kinda stuck,logicly im thinking maybe replace the index with the next one,but that might cause errors in terms of  the number swaped is also even.Yep im defintly stuck
 
Junilu Lacar
Sheriff
Posts: 13517
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, what happens when array[j] is an even number? Should you swap that with array[i]? In your current logic, you swap even values of array[i] with array[j] even when array[j] is also even.
 
Junilu Lacar
Sheriff
Posts: 13517
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you walk through it manually first. Work out the algorithm with pen and paper so you can see, step-by-step, what the process should be. You should be clear on the process, the conditions that need to be checked, and the loop initialization and termination before you even attempt to tell the computer in a language that you're still unfamiliar with.

Also, don't throw randomness into the mix yet. Just start by trying to sort 1, 2, ..., 8, 9, 10.
 
Liutauras Vilda
Marshal
Posts: 6851
470
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

will show
1,3,5,7,9,2,4,6,8,10
or something like that,dosent really matter,the point is to have the even on the right side


Since you don't need (at least for now) to sort both parts (of even and odd numbers), the algorithm what Junilu suggested to come up with is really simple.

Think what is the least painful way you could go through an array once and accomplish that? Hint: Maybe at the same time, from start and end?
 
mike moka
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ive tried something els,but i encounter an error when it enters the if statment


 
Liutauras Vilda
Marshal
Posts: 6851
470
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Didn't look past the line where you created another array. Why do you need that one?

Tasks says: "Create an array with 10 numbers in it, then move...", Not insert.

You overthink this problem. And perhaps didn't follow an advice to use piece of paper and pencil.

Please switch off the computer, get the paper and pencil and think for half an hour. Then please write down in english what steps you'd take to accomplish task. Only then, when you are confident about algorithm you came up - you can start figure out how to achieve that in Java or any other language of choice.
 
Liutauras Vilda
Marshal
Posts: 6851
470
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Imagine there are 3 guys standing, one at the front of an array, another at the end of an array and the 3rd one is waiting for the instructions what to do.

First guy's task is to go from the start of an array and find an even number and when it finds - shout to the second guy to let him know he found the even number.
The second guy's task is to start looking for odd number(s) when he hears the shout from the first guy. Once the second guy finds the odd number, he shouts to third guy.
Third guy's task is to swap numbers at the positions where first and second guys found them. Once he done that, he shouts back to both guys and say: "carry on looking from where you left off.".

Your task is to finish story.
 
mike moka
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright,i figured out what needs to be done..(Kinda bumed i didnt see it )
But im still stuck
Once the program find an even number,and than an odd number and does the swap,it keeps looking for an odd number and keeps replacing it with the even number ive saved as temp.
not sure how to break the loop


 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
line 28:
break;
 
Liutauras Vilda
Marshal
Posts: 6851
470
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike, that algorithm is just one of several possibilities. Spend some more time thinking, you might come up with something better.

As said, create methods for small tasks.

isEven(N) or isOdd(N)
swapAtIndices(i, j)
...

You need to learn how to decompose problem. You can't solve problem easily having few nested loops at once, it is hard to understand.

So what this check does?

I'm sure you don't say, this check is applying mod 2 on array[i] and checking if equal to 0. Clearly it does something more meaningful for the human being. So what is that? Name it and create method for it.
 
mike moka
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh amm..forgot to say
We havent learend Methodes nor swap,so im not allowed to use it,hence why i think nested loop is the best solution
 
Liutauras Vilda
Marshal
Posts: 6851
470
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

mike moka wrote:We havent learend Methodes nor swap,so im not allowed to use it,hence why i think nested loop is the best solution


You are using methods already, as well as you are doing swaps already, this isn't something you need to be taught explicitly by your teacher. As well as you don't need to wait until somebody gives you permission how to better organise your code. Methods are common constructs in Java language. Read about them here and start using them as soon as possible, preferably in this project.

If you have 3 lines of code which do something:

You wrap them in a method and name it so it makes sense what it does:

So later you can call such method in your loop or if statement, example..
Instead this:
You can:

If you create yet another method from the if's statement check, you could end up with:

Do you see how it is easier to understand what is going on in your program and how easier might be to spot flaws (logical mistakes).

I'll leave you for a while to work on the problem and spend some more time, as this exercise is really for you to scratch the head and learn something


 
Junilu Lacar
Sheriff
Posts: 13517
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

mike moka wrote:We havent learend Methodes nor swap,so im not allowed to use it,hence why i think nested loop is the best solution


That is one of the most artificial and unnecessary constraints there is to learning. I guess it's not your fault that you're not "allowed" to do that though but I still think it's an impediment to learning that doesn't need to be. Why you wouldn't want to explore ways to make problem solving easier is just beyond me.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!