• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sorting contents of strings

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Tim Tock
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's what I'd write:

sev
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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).
 
reply
    Bookmark Topic Watch Topic
  • New Topic