• Post Reply Bookmark Topic Watch Topic
  • New Topic

Compose 2 given Char arrays into 1 new char array  RSS feed

 
Petar Stoyanov
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Soo I got this problem.. I have 2 char arrays



I looked around and found how to combine int arrays but I'm not sure how to apply that to char arrays as the merge line and such seem to not work with chars
so whats the easiest way to combine 2 char arrays into a new one?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Show us what you tried that didn't work.
 
Petar Stoyanov
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh... i already deleted it but it was along the lines of
char [] newArray = merge(word1,word2) or something like that
which gave me an error whatever i tried
worked with int arrays for the guy i had found at the time, but just changing it from int to char doesnt do the trick
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It really depends on what code was in that merge() method. Such a method could work given the right code. Perhaps it would be more educational for you to try to write the code that would work.
 
salvin francis
Bartender
Posts: 1662
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there is an existing method to copy an array content from one to another, Maybe you could use that to achieve your task ?

System.arraycopy
 
salvin francis
Bartender
Posts: 1662
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Petar Stoyanov wrote:char [] newArray = merge(word1,word2) or something like that

Hi Petar,
Lets go ahead with your approach of defining a method,

 
Petar Stoyanov
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:Hi Petar,
Lets go ahead with your approach of defining a method,


The whole idea is to just merge the 2 arrays in one and save it in a new array with a for-loop that i suppose would look something like

that's all for now i suppose, as i go forward with other tasks i will probably expand onto this code
 
salvin francis
Bartender
Posts: 1662
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Petar Stoyanov wrote:



Good to know that you are using a loop. It's "for" and not "For". Java is case sensitive. Your code would not compile.
Before you start working on a loop, do you know how to define an array whose size is equal to the combined length of the two arrays ?

I also wanted to point out that your sysout would be outside your loop unless you want to iteratively print the new array each time you run the loop. I doubt thats what you want.
A useful method provided by java is Arrays.toString(). It will convert your array to a comma separated string enclosed in brackets. This means you see your final combined array as :

 
Petar Stoyanov
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:Good to know that you are using a loop. It's "for" and not "For". Java is case sensitive. Your code would not compile.
Before you start working on a loop, do you know how to define an array whose size is equal to the combined length of the two arrays ?

I guess that would be char [] = new char[10]; ?
thats for the 10 letters in hello world i would need to store in it
 
salvin francis
Bartender
Posts: 1662
37
Eclipse IDE Google Web Toolkit Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Petar Stoyanov wrote:
I guess that would be char [] = new char[10]; ?

Awesome, but here is something I would suggest:

This will ensure that combined is the length of word1 and word2 and you don't need to hard code 10 into it.

The advantage is that someone can change your code to

and you dont have to make code changes to make that work
 
Petar Stoyanov
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:
Petar Stoyanov wrote:
I guess that would be char [] = new char[10]; ?

Awesome, but here is something I would suggest:

This will ensure that combined is the length of word1 and word2 and you don't need to hard code 10 into it.

The advantage is that someone can change your code to

and you dont have to make code changes to make that work


yeah that makes perfect sense
and how do i copy both arrays into the new one without hard coding it in
 
salvin francis
Bartender
Posts: 1662
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving further, what would be your approach to fill this "combined" array with contents of word1 first and then the contents of word2 ? Hint: you can loop through each of them one by one.
 
Petar Stoyanov
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:Moving further, what would be your approach to fill this "combined" array with contents of word1 first and then the contents of word2 ? Hint: you can loop through each of them one by one.


is what i can think of at the moment, problem is i get only squares as a result in Eclipse , i guess this doesnt exactly copy the chars so i'm lost here ;/
 
Henry Wong
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
The straightforward way to "copy" something is with the assignment operator. Have you learned that yet?

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html

Henry

 
Petar Stoyanov
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:The straightforward way to "copy" something is with the assignment operator. Have you learned that yet?

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html

Henry


I believe that's what i used for the for loop, am i wrong?
having the "i" go throught the whole length and print out its value at each letter
but the result i get is squares only instead of the letters themselves.
 
Henry Wong
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
Petar Stoyanov wrote:
I believe that's what i used for the for loop, am i wrong?
having the "i" go throught the whole length and print out its value at each letter


So ... with the assignment operator, your were able to put values into the index variable "i".  Now, can you do the same for the elements of the new array (that you created)? After all, aren't you supposed to put values into that?

Henry
 
Petar Stoyanov
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Petar Stoyanov wrote:
I believe that's what i used for the for loop, am i wrong?
having the "i" go throught the whole length and print out its value at each letter


So ... with the assignment operator, your were able to put values into the index variable "i".  Now, can you do the same for the elements of the new array (that you created)? After all, aren't you supposed to put values into that?

Henry

Okay im starting to see... a bit...


More like this i guess? have the i go through the length of the new array but assign it the values from word1 and 2 however i don't know how to code it so that after it finishes word1 it goes through word2
 
salvin francis
Bartender
Posts: 1662
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Henry pointed out, you can use an assignment operator to assign value to your variables,

A pseudo code to do your task would be something like:

1. Initialize array word1
2. Initialize array word2
3. Create an empty array "combined" whose size is that of word1+word2
5. Initialize "walker" to 0
4. Loop through all the contents of word1[n] (where n starts from 0 to word1.length-1)
  4.1 Assign (as Henry pointed out) combined[walker] to word1[n]
  4.2 Set walker to walker + 1
5. Loop through all the contents of word2[m] (where m starts from 0 to word2.length-1)
  5.1 Assign (as Henry pointed out) combined[walker] to word2[m]
  5.2 Set walker to walker + 1
6. Print combined array

 
Petar Stoyanov
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:As Henry pointed out, you can use an assignment operator to assign value to your variables,

A pseudo code to do your task would be something like:

1. Initialize array word1
2. Initialize array word2
3. Create an empty array "combined" whose size is that of word1+word2
5. Initialize "walker" to 0
4. Loop through all the contents of word1[n] (where n starts from 0 to word1.length-1)
  4.1 Assign (as Henry pointed out) combined[walker] to word1[n]
  4.2 Set walker to walker + 1
5. Loop through all the contents of word2[m] (where m starts from 0 to word2.length-1)
  5.1 Assign (as Henry pointed out) combined[walker] to word2[m]
  5.2 Set walker to walker + 1
6. Print combined array



What the heck is a "walker" havent seen it used as a term until now
 
salvin francis
Bartender
Posts: 1662
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Petar Stoyanov wrote:What the heck is a "walker" havent seen it used as a term until now

it's just a variable name I used, it's similar to your 'i'.
or

or some other variable name as per your choice.
 
Henry Wong
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
Petar Stoyanov wrote:
What the heck is a "walker" havent seen it used as a term until now


From reading the pseudo code, you should be able to guess that it is an index variable -- for whatever loop to walk through the arrays.


Anyway... I kinda disagree with Salvin here. I don't think starting from this pseudo code is a good idea. The issue here is logic. The OP needs to understand the logic. And that is probably best done with a pencil and some paper. Once the logic is all mapped out, then that should get to the pseudo code, and hence, to Java code. Without an understanding of the logic, all the OP will be doing is "paint by numbers" -- which may or may not be done correctly (and learn little).

Henry
 
Petar Stoyanov
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:
Petar Stoyanov wrote:What the heck is a "walker" havent seen it used as a term until now

it's just a variable name I used, it's similar to your 'i'.
or

or some other variable name as per your choice.

how do i set the word length to be word.length-1 <- obviously this did not work
 
salvin francis
Bartender
Posts: 1662
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you want to do that ?
 
Petar Stoyanov
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:Why do you want to do that ?

4. Loop through all the contents of word1[n] (where n starts from 0 to word1.length-1) <==
  4.1 Assign (as Henry pointed out) combined[walker] to word1[n]
  4.2 Set walker to walker + 1
5. Loop through all the contents of word2[m] (where m starts from 0 to word2.length-1) <==
  5.1 Assign (as Henry pointed out) combined[walker] to word2[m]

have i misunderstood that ?
 
salvin francis
Bartender
Posts: 1662
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you have misunderstood what I meant, but lets start over. What would be your approach to merge the two ?
How do you think we should loop through the elements in the array, don't worry about right and wrong. We all learn at some point of time.
 
Petar Stoyanov
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:Yes, you have misunderstood what I meant, but lets start over. What would be your approach to merge the two ?
How do you think we should loop through the elements in the array, don't worry about right and wrong. We all learn at some point of time.


ive only ever used

so yeah... once again this way
 
Henry Wong
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
Petar Stoyanov wrote:
ive only ever used

so yeah... once again this way


I think Salvin was referring to logic -- and not code. Meaning (with a pencil and paper) how do the two array look like (draw it)? How does the target result look like (draw it)? And what assignments go where to get from the starting point to the result point. Obviously, this third part is going to be a bunch of arrows (in the drawing) -- so, you may need to elaborate the drawing a bit in a follow up (which can be further discussed here).

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