Win a copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java this week in the Agile and Other Processes forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

How to compare multiple spaces with one space and still get it true?

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am reading a file.In some records it can have just spaces.It can be one space, two spaces or may be many more.I am trying to come up some kind of method where I will compare all of these spaces with just one sapce and I want this to be true.I mean for me one space or multiple space should mean the same thing.How can I do this?
trim() does not achieve what I want.
compare() also does not seem to work.
Any body has any idea?
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chris,
I'm not sure if I got it right.
The method could be:
1. public boolean compare(String s){
2. if(s.trim().equals(""))
3. return s.substring(0,1).equals(" ");
4. return false;
5. }
on line 2. you just make sure that the string you want to compare is in fact only composed of spaces, then on line 3. we take the first character (could have done it with charAt method) and compare it with the one-space-string.
is that what you wanted ?
Val
 
Chris Mathews
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's wring with this code?

<code>
public class Test
{ public static void main(String args[])
{
String abc = " ";
System.out.println(abc.trim());

if ((abc.trim()) != null)
{abc = abc.trim();
System.out.println("its at 1" + abc);}
else
{ abc = "01";
System.out.println("its at 2 " + abc);}


}
}
</code>
I thought this would print 'its at 2' but No...it is printing 'its at 1'.....Why ?
Any insight?
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because if the string abc is initialized (as in your code) invoking the trim method on it will not return a null even if your string does only own spaces, instead it will return "" (the null string) and thus the first test always succeeds and prints "its a one"...
 
Chris Mathews
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I make it return 1 or 2 depending upon what abc contains. If abc is just space(" ") or if abc is " 5 ".
What should I do?
 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WHat do you mean return 1 or 2?
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Chris,
I have made this program for you and it is all working fine as per your requirement.Now change the print statements as per your requirement and ENJOY...
class space
{
public static void main(String arg[])
{
String str=" ";
char c=' ';
boolean flag=false;
for(int i=0;i<str.length();i++)>
{
if(str.charAt(i)!=c)
{
flag=true;
break;
}
else
{
flag=false;
}
}
if(flag)
{
System.out.println("The string is containing chracters other than space");
}
else
{
System.out.println("The string is containing only spaces");
}
}
}
Regards
Gurpreet Sachdeva
For Mock Exams and some useful information about Bitshift operator, inner classes, garbage collection,etc please visit: http://www.go4java.20m.com
 
The problems of the world fade way as you eat a piece of pie. This tiny ad has never known problems:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!