• Post Reply Bookmark Topic Watch Topic
  • New Topic

Contains any char  RSS feed

 
Kristers Drozds
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings,
I need to check if atbilde contains any char(symbols) if it does, then -
System.out.println("You have entered symbol line");
System.out.println("Reverse" + teksts.reverse());

Could you help me with this please?

 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by symbols? Characters that aren't letters or numbers?
 
Ahmad Auada
Ranch Hand
Posts: 56
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Your program needs to check if the string contains an integer.
One way to do it is by trying to parse the string to an int. If this fails, it means that what ever was entered was not a number.
so you should add this:



By the way, good code should be easily readable by other programmers. atbilde and sc are not very clear. Try changing them.

Good luck!
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahmad Auada wrote:. . . One way to do it is by trying to parse the string to an int. . . .
I like the rest of your advice, but there are much easier ways to confirm that a String is in integer form. One is to use a regular expression, and you can read about that here. Another is to use a Scanner.Beware: that technique will read “123 Campbell” as a bona fide int. Also a regular expression matching [1-9][0-9]* will find bona fide integers which are too large to be an int, and only matches positive numbers.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!