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!
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]
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)
> 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 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. ;)
> ...
> 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.
; )
> 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
> > ...
> > 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. :)
> 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
Thanks a lot, I have it working perfectly now. :)
> Thanks a lot, I have it working perfectly now. :)You're welcome, and good to hear you've got things working!