This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
hey all, having a bit of trouble, got a 3 character string, just wanna be able to remove the middle one and return a string something like public static void main(String args[]) { String phrase = new String ("CAT"); if (phrase.length()==3) { char temp = phrase.charAt(3); char temp1= phrase.charAt(1);
String temp2 = new String ("temp"); temp1.concat(temp); System.out.println(temp1); } } Any suggestions? i've cant concat, i cant put a char straight into a string, im all out T
This sounds like a homework problem, so I don't want to give the code right to you. Let me give you a couple of hints. There are a couple of different ways to approach the "problem". Keep in mind that at their heart, Stings are really char arrays (char[]). Take a look at the String API and see if with that thought in mind you might find a different way of approaching the problem. You can also take a look at the StringBuffer API for some alternatives.
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
posted
0
Tim, Note that the class documentation that Mark mentioned is available at http://java.sun.com/j2se/1.4.2/docs/api/ Take a look at the documentation of the methods available to Strings. Perhaps you'll notice a few things that you'll be able to make use of. Also, note that the index of the first character in a String is 0, not 1. Don't hesitate to ask for further nudging in the right direction.
Tim, public static void main(String args[]) { String phrase = new String ("CAT"); if (phrase.length()==3) { char temp = phrase.charAt(3); //is your indexes correct? char temp1= phrase.charAt(1); String temp2 = new String ("temp"); //is this going to be output? temp1.concat(temp); //is this correct System.out.println(temp1); //what will be output here if it did work? } } in your code, look at your indexes, are they correct?? is you concat() method correct? also check the API to see if you can find a better solution. to your charAt() methods. I am sure you can see what mistakes you have done, I was going to give the answer until i saw others thought it was a homework question good luck, but post back with any amendments you have Davy [ February 28, 2004: Message edited by: Davy Kelly ]
How simple does it have to be???
Tim Tock
Greenhorn
Joined: Jan 15, 2004
Posts: 15
posted
0
edited again, oops, fantastic, just got to grips with stringbuffers, heres wat i got: public class JdbcExample2 { public static void main(String args[]) { String phrase = new String ("ABC"); String x; if (phrase.length()==3) { char temp = phrase.charAt(0); char temp1= phrase.charAt(2); x = new StringBuffer().append(temp).append(temp1).toString(); System.out.println(x); } } } Thanks for the help, just have to add, I cannot thank javaranch enough for all the help and advice i've recieved since being on here (I just wish I could put something back into the board) big THANKYOU to everyone ps. it wasnt a homework assignmet, lol, it was just something i wanted to do for a little project of mine, but thanks all the same!!! [ February 29, 2004: Message edited by: Tim Tock ]
sever oon
Ranch Hand
Joined: Feb 08, 2004
Posts: 268
posted
0
Here's what I'd write:
sev
Tony Morris
Ranch Hand
Joined: Sep 24, 2003
Posts: 1608
posted
0
Stings [sic] are really char arrays (char[]).
This methodology of thinking is prone to problems. Most VMs implement a String using a char[]. However, a char[] is a mutable type, where a String is not (intended to be anyway - this is another academic argument). Example: char[] a = new char[]{'a', 'b', 'c'}; a[0] = 'd'; Now let's see the String equivalent: String a = "abc"; // What now ? It can't be done (well it can, but like I said, that's another story).