declare

Java Tutorial - 16 - Read Characters from a String into a Char Array



Sharing buttons:

hello welcome to this lesson of

mastering Java where we're going to

continue working with our skills with

arrays and also with strings and

characters giving you knowledge of these

little functions and methods that can

kind of help you along the way in your

studies of Java and in your practical

programming so what we're gonna do in

this lesson is we're going to learn how

to copy the contents of a string the

characters from a string and put it into

a character array as you know in Java

strings are really objects and arrays

are really objects too but whenever you

start to look at other programming

languages such as C strings and arrays

are not really objects in those regards

so for instance an array in or I should

I should say a string and the language C

is just an array of characters so even

though you can kind of think of them

that way in Java as well really

everything is implemented as objects so

there's many cases especially if you're

going back and forth between different

programming languages or you might need

to take the contents of a string and put

them into an array of characters there's

just there's just lots of opportunities

when you might want everything implement

it as an array and Java lets you do that

so first let's create a string and I'm

gonna call this guy STR right and inside

of this I'm gonna put my name Jason

Gibson that's the string okay I'm gonna

put that guy like that and then I'm

gonna go and I'm gonna create an array

I'm gonna create an array of characters

and so the way you do that is you

specify what is the oregano store that's

called a character type and then I'm

gonna name it I'm gonna say array put my

double brackets indicating that it is an

array and I'm not gonna populate

anything inside of this yet I'm just

gonna say new care bracket and how many

characters do I have here one two three

four five six seven eight nine ten

eleven twelve so there's 12 so just to

be safe I'll reserve 15 memory spaces in

my character array so all I've really

done right now is I've created a string

called Jason Gibson which remember is an

object in Java and I've created a

character array which is discrete memory

locations each one of these memory

locations I've set aside 15 of

can hold a character data type alright

so it would be nice to be able to take

the characters that are in my string and

put them into the memory locations for

my array now the reasons why you would

do that they come later as you as you

get familiar with programming I'm giving

you one little motivation letting you

know that a language such as C strings

and C are not implemented as objects

they're they're implemented as arrays of

characters so you might need to do this

for instance if you're interfacing with

C code or maybe if you're just more

comfortable doing it that way

there could be many algorithms that

you're using that would that would need

to have the string implemented as an

array of characters so the way you

actually do that is you would use the

method attached to the string object and

the name of that method is called get

cares and you might think of it as

getting literally getting the characters

from your string and putting them

somewhere now you can see here that it's

trying to give you some help it's trying

to tell you the method is get cares it's

an integer requiring where the source

begins integer where the source ends

this is a character array destination

that's where we're gonna store it what

array we're going to store it and within

an integer of where the destination

begins so basically what that's telling

you is it requires four things to do

this the first thing it requires is that

the the index of the source so the

source it already knows that this is STR

that's the string here STR that it's

referencing here so when I put a 0 here

what it's telling me is begin copying

from the 0th index from the meaning the

first letter of this string the next

thing it is asking is how many

characters do I want to copy so let's

just say for instance I want to copy

just my first name that's one two three

four five characters let me put five

characters the next thing it's asking me

for is the destination well I want to

put it into this array that I have

called array all right and then the

final thing is it's asking me where do I

store it in the array I can I can put it

anywhere I want but I'm gonna go ahead

and put it at the beginning of the array

starting with element zero and then I'll

put a semicolon there so you need to

know how this is written but again if

you forget you can always just

type STR dot get cares and it's trying

to tell you in fact if you go here you

can start reading about what this guy is

basically you need source begin source

end character destination air raid array

destination we're copying too and where

am i copying within that array so I'm

starting from element 0 of my string of

copying five elements from my string I'm

storing it in the array called a rake

and I'm storing it an element 0 which is

very beginning of that array so then if

I do system dot out dot print Ln and

inside of here at this point after this

execute this line here then the the

words the letters JSON should be stored

in this array beginning at element 0 so

array element 0 like this and then if I

terminate that and then if ever done

everything correctly I should see a J

print to the screen because that is what

is stored right there and I get a J so

let me go ahead and copy this and I'll

paste a few more down here let me print

out element 1 element 2 or index I

should say index index 3 index 4 so I'm

just printing out the first 5 elements

from counting room 0 to 4 of the array

and hopefully if I've done it all right

JSON so this is just verifying what this

methods really doing its verifying that

I've started at index 0 from my string

I've copied 5 elements which is this guy

I've put them in my character array and

I've started at element 0 in the array

in the destination array so if I want to

change this for instance instead of 5

what if I want to do copy 1 2 3 4 5 6 7

8 9 10 11 12 so if I put a 12 here then

what's going to happen is I'm going to

begin copying from element 0 I'm gonna

copy 12 elements from the string I'm

going to put it in the destination array

beginning an element 0 alright and then

if I want to print everything out let me

go ahead and copy and it will just paste

a much times and let's just say this

would be

five six seven eight nine ten eleven

twelve ten eleven so if we've done

everything correctly we'll run and what

we get is Jason this is each element of

this character Ray is printing out JSON

the space is also an element and then GI

BSO n has also printed out and I'm doing

that using the accessing each individual

element of the right now of course I

have a few empty spaces left over in

this array and the reason I'm done I've

done that is just to give myself a

little bit of pad so basically at this

point I think I've gotten across the

main the crux of what I'm really trying

to show you here and that is that you

can take any string you want and you can

copy the individual characters into an

array of characters and you do that what

they get cares method which is a part of

every string it's a it's a method

associated with every string I can

specify what part of the string I'm

copying because I can start I can

specify the starting point in terms of

its index in how many characters I wish

to copy I can specify the destination

array and I can also specify where

inside of the destination to write a

paste thing so if I change this to one

or two then that means the pasting will

begin the pasting will begin after the

array has started so for instance if I

screw up here and put ten this is an

accident

then I'm gonna try to copy and paste

twelve characters into this array

starting at location ten but my my array

isn't really that long so I should get

an error if I do that and I get an

exception because when it tries to

execute the get Kerr's function is

trying to start tasting everything in at

location ten of this array it's trying

to to paste twelve items in and when you

start doing that eventually you're gonna

get larger than the size of the array I

have so typically what you're doing when

you're doing this is you're copying the

entire string and you're copying into

location zero one two three and so on in

terms of the array so make sure you

understand how to do this I've got kind

of a neat exercise to show you and give

you practice with doing this and give

you kind of a practical reason of when

you might actually use this so I suggest

you off to the exercise right now

practice it yourself make sure you

understand it again it's not something

you use every single day but it's

something that is very useful

you're dealing with strings and arrays

in Java