Need help for the method to remove whitespaces between words in a sentence

I wrote this program to get rid of the whitespaces. I created a loop to go to each character of the sentence, if it's whitespace get rid of, if not accumulate. I don't know how to accumulate. Can someone please help?.

import java.util.*;

publicclass DoCompress

{

publicstaticvoid main(String args[])

{

String s;

Scanner sc =new Scanner(System.in);

for(;;)

{

String E ="E";

System.out.print("Type your Palindrome or E to Exit:");

s = sc.nextLine();

String scompress = compressIt(s);

System.out.println(scompress);

if(s.equalsIgnoreCase(E))break;

}

}

privatestatic String compressIt(String c)

{

for(int i=0 ; i < c.length() ; i++)

{

char str = c.charAt(i);

if(! Character.isWhitespace(str));

}

//Accumulate the non whitespace.

}

}

[1784 byte] By [aivon1sta] at [2007-11-27 8:49:47]
# 1
Try c.replaceAll("\\s","");
SidGatea at 2007-7-12 20:59:36 > top of Java-index,Java Essentials,Java Programming...
# 2
send the complete program
harish.suna at 2007-7-12 20:59:36 > top of Java-index,Java Essentials,Java Programming...
# 3
> Try > > c.replaceAll("\\s","");Instead for writing different method compresIt String scompress = compressIt(s);useString scompress = s.replaceAll("\\s","");
SidGatea at 2007-7-12 20:59:36 > top of Java-index,Java Essentials,Java Programming...
# 4
How can I delete my reply?Message was edited by: Statik
Statika at 2007-7-12 20:59:36 > top of Java-index,Java Essentials,Java Programming...
# 5
> How can I delete my reply?> > Message was edited by: > StatikYou can't delete it. And now I have replied to your post, you can't even edit it.
prometheuzza at 2007-7-12 20:59:36 > top of Java-index,Java Essentials,Java Programming...
# 6
Oh, sorry. That was my first post. My bad, didn't know.
Statika at 2007-7-12 20:59:36 > top of Java-index,Java Essentials,Java Programming...
# 7
> Oh, sorry. That was my first post. My bad, didn't> know.No problem.
prometheuzza at 2007-7-12 20:59:36 > top of Java-index,Java Essentials,Java Programming...
# 8
can't use 'replaceall, because the teacher has not covered that in the class. We're supposed to write our own method for this.
aivon1sta at 2007-7-12 20:59:36 > top of Java-index,Java Essentials,Java Programming...
# 9

> can't use 'replaceall, because the teacher has not

> covered that in the class. We're supposed to write

> our own method for this.

Here's a hint:

import java.util.*;

public class DoCompress

{

public static void main(String args[])

{

Scanner sc = new Scanner(System.in);

while(true)

{

System.out.print("Type your Palindrome or E to Exit: ");

String in = sc.nextLine();

if(in.equalsIgnoreCase("E")) break;

in = compressIt(in);

String rev = reverse(in);

// Compare 'in' and 'rev', if they're the same: it's a palindrome.

}

}

private static String compressIt(String s)

{

StringBuffer buffer = new StringBuffer();

// LOOP 'i' from 0 to 's'.length-1

//IF the char at posistion 'i' out of string 's' is not a space

//append that char at the end of the buffer

//END IF

// END LOOP

return buffer.toString();

}

private static String reverse(String s)

{

return s; // replace this with your code, of course!

}

}

prometheuzza at 2007-7-12 20:59:36 > top of Java-index,Java Essentials,Java Programming...
# 10

You want to place the accumulate section within the for loop in compressIt. You'll also need to declare a local String variable in the method and add each letter to that using the += operator.

private static String compressIt(String c)

{

String retVal = "";

...

//Accumulate the non whitespace.

retVal += <convert str to a String and place here>;

}

C.R.Crena at 2007-7-12 20:59:36 > top of Java-index,Java Essentials,Java Programming...
# 11

> can't use 'replaceall, because the teacher has not covered that in the class. We're supposed to write our own method for this.

Take your slingshot to your next class with this purported "teacher" and shoot out the lights. When questioned state that you couldn't possibly use the electric light bulb because the foresaid he/she/it who would be "teacher" has not had to foresight to teach you how to use the forementioned light bulbs.

Is it just me, or are "teachers" actually getting dumber over the years?

Message was edited by: corlettk

corlettka at 2007-7-12 20:59:36 > top of Java-index,Java Essentials,Java Programming...
# 12

IMHO it's a good thing that students (myself included) learn how to do things without using all those fancy methods/classes from the standard API. It learns them something on how to tackle a problem: which elementary steps are involved to solve a specific problem. Thinking "algorithmically" will train students to tackle larger problems.

prometheuzza at 2007-7-12 20:59:36 > top of Java-index,Java Essentials,Java Programming...
# 13
can't use 'StringBuffer' either, same story. :(
aivon1sta at 2007-7-12 20:59:36 > top of Java-index,Java Essentials,Java Programming...
# 14

> can't use 'StringBuffer' either, same story. :(

The first step should be to work out on paper how you would do this manually. If you were a computer, what steps would you take. Imagine a bunch of letters lined up on the table in front of you, how would you process them to take the spaces out?

Once you have the process worked out in your head, start applying java to it. Take each discrete step and write code that does that.

hunter9000a at 2007-7-12 20:59:36 > top of Java-index,Java Essentials,Java Programming...
# 15

> can't use 'StringBuffer' either, same story. :(

I really doubt it. Please clarify with your teacher why you cannot make use of the StringBuffer's functionality of buffering characters. I can imagine you can't use it's reverse() method, but simply using it's append(...) method should be fine.

Or else, just concatenate it like this:

private static String compressIt(String s)

{

String b = "";

// LOOP 'i' from 0 to 's'.length-1

//IF the char at position 'i' out of string 's' is not a space

//concatenate the char after 'b'

//END IF

// END LOOP

return b;

}

prometheuzza at 2007-7-21 22:48:26 > top of Java-index,Java Essentials,Java Programming...
# 16
If this is ur hw, wats so wrong with doing it the had way long way, but reading each character in the string and if doesn't equal ' ' then return it?
blackmagea at 2007-7-21 22:48:26 > top of Java-index,Java Essentials,Java Programming...
# 17
> If this is ur hw, wats so wrong with doing it the had> way long way, but reading each character in the> string and if doesn't equal ' ' then return it??Try spelling words out: you really make no sense!
prometheuzza at 2007-7-21 22:48:26 > top of Java-index,Java Essentials,Java Programming...
# 18
Newb,You can simply just read each character in the string, and if equals ' ', then you don't return that character. You do know how to read characters, don't you?
blackmagea at 2007-7-21 22:48:26 > top of Java-index,Java Essentials,Java Programming...
# 19
> Newb,Funniest opening line I have seen on these forums in awhile.
Djaunla at 2007-7-21 22:48:26 > top of Java-index,Java Essentials,Java Programming...
# 20

This should do it

public void RemoveSpaces(String s)

{

for (int i=0;i<=s.length()-1;i++)

{

if (s.charAt(i)!=' ')

System.out.print(s.charAt(i));

}

}

(This is coded in bluej , the code in actual java may be just a little bit different, but to my knowledge it will stay the same)

The loop runs from 0 to the length of the string -1, because the counting for the strings length starts from 1, whereas the s.charAt sees the first character at 0.

So the i in the for loop goes to each character of the string. The if loop then checks that if the character which i is at is not a space, then it prints the character, and else it doesnt.

Hope this helped

Overkilla at 2007-7-21 22:48:26 > top of Java-index,Java Essentials,Java Programming...
# 21

> This should do it

>

>...

Well, technically, that doesn't remove any spaces: it just displays all characters from a given String that are not equal to a white space.

But please let the OP try to do this him/herself. This way s/he learns the most. Just like you did in your own thread.

Thanks.

prometheuzza at 2007-7-21 22:48:26 > top of Java-index,Java Essentials,Java Programming...
# 22
An answer on a platter, complete with dipping sauce.I wish the answers to my questions were that complete.
blackmagea at 2007-7-21 22:48:27 > top of Java-index,Java Essentials,Java Programming...
# 23
what is OP?
aivon1sta at 2007-7-21 22:48:27 > top of Java-index,Java Essentials,Java Programming...
# 24
> what is OP?original poster. So, that would be you.
prometheuzza at 2007-7-21 22:48:27 > top of Java-index,Java Essentials,Java Programming...
# 25

> Is it just me, or are "teachers" actually getting

> dumber over the years?

Not necessarily (Can't rule it out entirely though) it is often a case of having a curriculum that they have no choice but to follow. I would also offer that despite the fact that there are better ways to do it. Learning how to write a method to do it the hard way has value in terms of things like teaching the student how to move through the String and find characters in the String etc. I wouldn't have framed the assignment that way, but I can see what they were driving at.

PS.

puckstopper31a at 2007-7-21 22:48:27 > top of Java-index,Java Essentials,Java Programming...
# 26
Thanks everyone!. You guys are very helpful.
aivon1sta at 2007-7-21 22:48:27 > top of Java-index,Java Essentials,Java Programming...
# 27

Ah k got ya prometheuss - i guess youre right, in my own threads even though i asked a lotta questions i didnt get the answer on a platter, and it helped me 2 develop my logic.

Actually, i dont know why, im addicted to programming now. In my free time, i do programs or read a java book to learn more instead of playing games or watching tv or surfin the net like i usually do :O(im 15 yrs old)

Overkilla at 2007-7-21 22:48:27 > top of Java-index,Java Essentials,Java Programming...
# 28
What will be the best book for Java (beginner)?. I currently used the book by Tony Gaddis and also have the one by Deitel & Deitel.
aivon1sta at 2007-7-21 22:48:27 > top of Java-index,Java Essentials,Java Programming...
# 29
There are a great quantity of good books. I'm partial to the O'Reilly books m'self.PS.
puckstopper31a at 2007-7-21 22:48:27 > top of Java-index,Java Essentials,Java Programming...
# 30

> IMHO it's a good thing that students (myself

> included) learn how to do things without using all

> those fancy methods/classes from the standard API. It

> learns them something on how to tackle a problem:

> which elementary steps are involved to solve a

> specific problem. Thinking "algorithmically" will

> train students to tackle larger problems.

I would agree, especially if the intention of the class is to teach programming not necessarily Java. And, the manner in which the OP asked the question would make me think that this is an introduction to programming with Java being the vehicle for learning.

jbisha at 2007-7-21 22:48:32 > top of Java-index,Java Essentials,Java Programming...
# 31

> What will be the best book for Java (beginner)?. I

> currently used the book by Tony Gaddis and also have

> the one by Deitel & Deitel.

I'm a huge fan of the Deitel brothers' work. O'Reilly is also good and more specific per book. Deitel is kind of a "kill-every-bird-with-one-stone" deal. Go around reading back covers and skimming the pages. That's what I did.

-C. R.

C.R.Crena at 2007-7-21 22:48:32 > top of Java-index,Java Essentials,Java Programming...