473,491 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

loop through a string in java

1 New Member
Hi,

I am trying to check whether a user inputs a number in a string with letters but every time i check this it only says there is a number if it is at the beginning of the string, otherwise it says there is no number and I was wondering if anyone could help. My code for this is below:

public void print()
{
if(number == false)
{
System.out.print("there is no number");
}
else if (number == true)
{
System.out.print("number");
}
}

boolean number = false;

public boolean number(String user)
{
for (int i=0; i<user.length();i++)
{
if ((user.charAt(i))>=45 && (user.charAt(i))<=57)
{
number = true;
return true;
}
else if(!((user.charAt(i))>=45 && (user.charAt(i))<=57))
{
i++;
//number = true;
//return true;
}
}
number=false;
return false;
}
Mar 3 '07 #1
2 17878
DeMan
1,806 Top Contributor
Not quite sure about this code (see attached comments)
Expand|Select|Wrap|Line Numbers
  1. public boolean number(String user)
  2. {
  3.   number =false;  /* Let's initialise to the failure case */
  4.   for (int i=0; i<user.length();i++)
  5.   { 
  6.     if ((user.charAt(i))>=45 && (user.charAt(i))<=57) /* I assume you want basic cal fun as well*/
  7.     {
  8.       number = true;
  9.       return true; /* Get rid of this retunr I think, although you could break if you
  10.                        just need to know whether there was a number, but aren't 
  11.                         interested in how many  *./
  12.     }
  13.     else if(!((user.charAt(i))>=45 && (user.charAt(i))<=57)) /* You could use a simple else since the condition is the total opposite of befroe (that uis the if here is redundant) 
  14.   You may bnot need these else clause anyway*/
  15.     {
  16.       i++; /* I'm not sure why you increment this here */
  17.      //number = true;
  18.     //return true;
  19. }
  20. }
  21. //number=false; /* Set this to false at the beginning, and it will only be true if it found a number */
  22. return number;
  23. }
  24.  
  25.  
Mar 4 '07 #2
Eric Geiger
1 New Member
public static boolean number(String user)
{
boolean number = false;/*you can get rid of this*/

for (int i=0; i<user.length();i++)
{
if (user.charAt(i)>=45 && user.charAt(i)<=57)
{
number = true;
return true;/*2 choices you can either a or 1 do return number or b or 2 just get rid of above statement "number = true"*/
}
else if(!(user.charAt(i)>=45 && user.charAt(i)<=57))
{
}
}
return false;
}
Mar 17 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

354
15468
by: Montrose... | last post by:
After working in c# for a year, the only conclusion I can come to is that I wish I knew c. All I need is Linux, the gnu c compiler and I can do anything. Web services are just open sockets...
3
1737
by: rmijares | last post by:
I am sure you guys have seen this before. I am working on the famous Employee Payroll program for a class assignment. In any case, the while loop at the end of the Employee class just keeps looping;...
5
2111
by: oaklander | last post by:
I have this JSP where I have alot of fields with conditions. I would like to make it more efficient and use a for loop. Here is an example (showing 2 fields for example only): <%@ page...
4
1615
by: chellemybelle | last post by:
Hello, I basically have made a little cheezy slideshow and would like to add captions to each pic as it loops through. I've tried everything I can think of. I'm assuming I might need to use an...
1
1887
by: saytri | last post by:
I have a problem with this code. I'm, doing a quiz, and the questions are stored in a binary file. My problem is how am i going to compare the answers entered by the user to the correct answers. The...
5
1701
by: jure87 | last post by:
hello everyone im new to this forum and need some help...i have a java code called flight.java and another code called checkflights.java, the purpose of this is to readin a text that is called...
12
3201
by: beatjunkie27 | last post by:
I am working on a class assignment called Pennies for Pay the object of the program is to receive input for number of days worked. This should be > 0 and <= 40. An example output is below and...
1
4565
by: robotlizz | last post by:
Hello - I am a brand new at Java and I am having a hard time with a program I have to turn in tomorrow. I can not get the 'Q' option to work and the loop goes on forever. I've tried to go over the...
1
3024
by: JavaJon | last post by:
Hello, I'm Jon. I've recently picked up Java after using a "gimmick" programming language called GML ( Game Maker Language ). I've read a lot of tutorials and even a Java for Dummies *.pdf book....
4
2529
by: crochunter | last post by:
Hi, I want to read values from a text files from specified fields and use them as values to fill my methods inside the paintComponent() method. I am using for loop to do that but not able to do it...
0
6980
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7192
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6862
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7364
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3087
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1397
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
282
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.