• Post Reply Bookmark Topic Watch Topic
  • New Topic

How would I change all numbers in even index locations to 3?  RSS feed

 
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, so what I am trying to do is, as the title says, replace all numbers in even numbered index locations with the integer 3. I am new to arrays and I am getting the errors with this. I'm not even sure if it will replace the numbers like I want. Here is the code that I have:

 
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please cut-n-paste the error message in a post.
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
method replaceAll in class ArrayTester cannot be applied to given types. Required: int[], int; found: int[]; reason: actaul and formal argument lists differ in length. However, I dont think that even if this error is solved, it will replace all even index locations with the number 3, like I want. I am not sure if my logic is solid.
 
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
David Vach wrote:method replaceAll in class ArrayTester cannot be applied to given types. Required: int[], int; found: int[]; reason: actaul and formal argument lists differ in length.


Well, basically, the error message is pretty clear. You defined the replaceAll() method to take two parameters -- a int array, and a int. And later, when you try to use it, you are only calling it with an int array.

David Vach wrote:However, I dont think that even if this error is solved, it will replace all even index locations with the number 3, like I want. I am not sure if my logic is solid.


Well, to be blunt ... Yes. Your code doesn't replace anything. It just prints the number 3 sometimes.

Henry
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would I have to define the int in my main class? Also, how would I fix it so it replaces with 3?
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And when I say replace, I mean the method should replace all occurrences of num that are found in even numbered index positions with the value of 3. For example, if the value num is stored in number[4]. then it must be replaced with the value 3 since 4 is even. If the num is found in numbers [5], is should not replace since 5 is odd.
 
Greenhorn
Posts: 1
C++ Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My friend, what I understood is that you should replace all the even positions of the array with the number 3, if this is so, I have developed the following code that I think may be useful:



Best regards.
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although initially your code was not what I was looking for completely, with some slight manipulation, I was able to solve my problem using your solution as a basis. Thanks!
 
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Marcelo Canales.

Thank you for your contribution.  However, here in the Beginning Java forum, we don't provided complete solutions until the OP has supplied their own.  We want to help with the learning process and not take that away by providing a solution; rather give hints and clues -- things that will make the OP think about and come to their own conclusion.  Code snippets are okay.
 
Greenhorn
Posts: 11
Eclipse IDE Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Vach wrote:Although initially your code was not what I was looking for completely, with some slight manipulation, I was able to solve my problem using your solution as a basis. Thanks!



Could you please share your final code and where you solved your issue, please?

 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my full code just in case you were curious with what I was messing around with:




The issue is fixed in lines 53-56. I stuck with my originally intended method and used his example for reference.
 
Marshal
Posts: 56605
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Vach wrote:. . . The issue is fixed in lines 53-56. I stuck with my originally intended method and used his example for reference.
I am not convinced. Why are you iterating one at a time and then testing whether the index divides by 2? Why do you have all those loops printing commas? There are at least two much better ways to get rid of the commas. One is to use a StringJoiner object. The other is simply to print a backspace character.
Only use the literal \n if somebody says they want an LF character. Don't print a new line. Use println.
Why are you using ! followed by ==? What is wrong with !=?
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its the way the AP board wants it according to my teacher. She doesn't want us using things like StringJoiner to make our lives easier. Its all supposed to be logic based so we show the college board on the AP exam that we know how to get the computer to do what we want. Although I will take into consideration they \n thing you mentioned, I hadn't known before as this is my first computer science class. Also I had completely forgotten about !=, so I will change that. However, the commas are being printed because our teacher wanted commas for some reason.
 
Campbell Ritchie
Marshal
Posts: 56605
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use a StringJoiner to put commas between successive “words”. You can print "\b" after the last comma, but that will probably be regarded as a naughty cheat and will lose you marks.
But why are you using ++ in the loop which changes all the numbers to 3? You don't need the test with %. I would mark you down for that. I would also mark you down for not writing a blank line between successive methods and for not putting spaces round operators like = and +=. I would also complain about the names of the methods which don't reflect what the methods do.
Are you supposed to do all the replacing operations on the same array? I would take copies of the arrays and make the changes in each copy.
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We are supposed to do all the replacing operations in the same array fro some reason. We haven't been taught any other way.
 
Campbell Ritchie
Marshal
Posts: 56605
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is one way to copy an array; that way you can do all the replacements with a fresh copy. If you have been told to do all the replacements in series on the same array, then that is what you will have to do.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!