Need a little bit of help with substring...

Im very new at java programming, and need a bit of help with a problem:

Here is what I have:

System.out.print("Enter a string : ");

Scanner scan = new Scanner (System.in);

stringy = scan.nextLine();

Now I want to split the string "stringy" like this: h:hi:hip:hipp:hippo

I know this uses substring, but I can't figure out how to do it.

Any help would be great, thanks!

[418 byte] By [fatratstewa] at [2007-11-26 21:09:18]
# 1
It's explained in the API docs in great detail:[url= http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#substring(int,%20int)] http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#substring(int,%20int)[/url]
prometheuzza at 2007-7-10 2:45:13 > top of Java-index,Java Essentials,Java Programming...
# 2
I have read that, but it only seems to explain how to use substring with a string of a certain length.Because the user could enter a string of any length, it wouldnt work by using numbers eg. (1,4)
fatratstewa at 2007-7-10 2:45:14 > top of Java-index,Java Essentials,Java Programming...
# 3

> I have read that, but it only seems to explain how to

> use substring with a string of a certain length.

>

> Because the user could enter a string of any length,

> it wouldnt work by using numbers eg. (1,4)

Have a look at the lentgh() method from the String class.

prometheuzza at 2007-7-10 2:45:14 > top of Java-index,Java Essentials,Java Programming...
# 4

> > I have read that, but it only seems to explain how

> to

> > use substring with a string of a certain length.

> >

> > Because the user could enter a string of any

> length,

> > it wouldnt work by using numbers eg. (1,4)

>

> Have a look at the lentgh() method from the

> String class.

I know about the length method, what I dont know is how to use the length and substring methods together to solve the problem i mentioned initially. Remember, Im very new. ;)

fatratstewa at 2007-7-10 2:45:14 > top of Java-index,Java Essentials,Java Programming...
# 5
Use "split"
abanka at 2007-7-10 2:45:14 > top of Java-index,Java Essentials,Java Programming...
# 6

> ...

> I know about the length method, what I dont know is

> how to use the length and substring methods together

> to solve the problem i mentioned initially.

There are three ingredients to perform this task:

- String.length()

- String.substring(int start, int end)

- for-statement: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/for.html

Pseudo code:IN <- input String from user

LOOP FROM 0 -> IN.length()

print IN.substring(?, ?)

print ":"

END LOOP

> Remember, Im very new. ;)

Remember that by just handing you the solution, you will learn far less than finding things out by yourself.

; )

prometheuzza at 2007-7-10 2:45:14 > top of Java-index,Java Essentials,Java Programming...
# 7
> Use "split"Although in the original post the OP uses the word split, I don't think that's the goal of this assignment.I believ if the user inputs the String Java, he needs to output:J:Ja:Jav:Java
prometheuzza at 2007-7-10 2:45:14 > top of Java-index,Java Essentials,Java Programming...
# 8

> > ...

> > I know about the length method, what I dont know

> is

> > how to use the length and substring methods

> together

> > to solve the problem i mentioned initially.

>

> There are three ingredients to perform this task:

> - String.length()

> - String.substring(int start, int end)

> - for-statement:

> http://java.sun.com/docs/books/tutorial/java/nutsandbo

> lts/for.html

>

> Pseudo code:IN <- input String from user

> LOOP FROM 0 -> IN.length()

>print IN.substring(?, ?)

> print ":"

> END LOOP

>

>

> > Remember, Im very new. ;)

>

> Remember that by just handing you the solution, you

> will learn far less than finding things out by

> yourself.

> ; )

Thanks a lot, i should be able to figure it out froom the pseudo code. :)

fatratstewa at 2007-7-10 2:45:14 > top of Java-index,Java Essentials,Java Programming...
# 9
> Thanks a lot, i should be able to figure it out froom> the pseudo code. :)You're welcome. When you get stuck, post again here.When posting code, please use code tags: http://forum.java.sun.com/help.jspa?sec=formatting
prometheuzza at 2007-7-10 2:45:14 > top of Java-index,Java Essentials,Java Programming...
# 10
Thanks a lot, I have it working perfectly now. :)
fatratstewa at 2007-7-10 2:45:14 > top of Java-index,Java Essentials,Java Programming...
# 11
> Thanks a lot, I have it working perfectly now. :)You're welcome, and good to hear you've got things working!
prometheuzza at 2007-7-10 2:45:14 > top of Java-index,Java Essentials,Java Programming...