473,324 Members | 2,511 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

If input is/is not a number

5
Hello,
I've started learning java this semester in school and my level is still quite low.
Now i found a problem with my program:

I want the program to recognise whether the imput is a number, then it is saved into int variable. This part works correctly. I made the second part in quite the same way It is supposed to repeat the input again if it contains any non-digit characters, until it consists of digits only. This is how it works(or rather doesnt):

class test{

public static void main (String[] args){

String text = In.readLine();
char [] pole = text.toCharArray();
String number = "";
int cycle =0;



for (int i=0;i<pole.length;i++)
{
if (Character.isDigit (pole[i]))
{
number= number + pole[i];
}
else if (!Character.isDigit (pole[i]))
{
Out.print("No number entered, enter number");

}

}
int a = new Integer(number).intValue();
Out.print (a);

}
}

Any ideas how to make it work? I would prefere a way, that doesnt change it completelly, but there is no other, or reasonable way, it doesnt matter. Thank you for help.
Apr 20 '10 #1
6 10642
jkmyoung
2,057 Expert 2GB
So you want the first 8 characters of the line to be digits? There's a couple methods involving putting a loop around most of your code.


1. Flag method: Put a while loop around most of your function.
Expand|Select|Wrap|Line Numbers
  1. boolean needNumber = true;
  2. while (needNumber)
  3. needNumber = false;
  4. // your main code.
  5. // input string.
  6. // parse ...
  7.  ...
  8.   else if (!Character.isDigit (pole[i]))
  9.   {
  10.     Out.print("No number entered, enter number");
  11.     needNumber = true; // reset so we have to enter again.
  12.   }
  13.  }// end for
  14. }// end while
  15. //convert and output number.
2. break method.
Put a while around the same part, but declare i earlier.:
Expand|Select|Wrap|Line Numbers
  1. int i = 0;
  2. while (i<pole.length){
  3. ...
  4. ...
  5.   else if (!Character.isDigit (pole[i]))
  6.   {
  7.     Out.print("No number entered, enter number");
  8.     break; // break for loop
  9.   }
  10.  }//end for
  11. }//end while
If we break unnaturally, then i<pole.length is still true.

Are you trying to add all the digits?
Apr 20 '10 #2
Hnizdo
5
I want to use it to determine whether the input is a number (all the chars are actually digits) or if it contains any/or only letters, like 8saf or asdf. In that case it
continue by making the new input as long as the user doesnt write digit only string.
Apr 20 '10 #3
Hnizdo
5
I did it like this, so far it seems to work, but I appreciate any ideas how to make it easier, shorter, better, anything helps.

class test{

public static void main (String[] args){


int i=0;
String text;
char [] pole;
String number = "";

while (i<1)
{ i=1;
text = In.readLine();
pole = text.toCharArray();
number = "";

for ( i=0;i<pole.length;i++)
{
if (Character.isDigit (pole[i]))
{
number= number + pole[i];
}
else if (!Character.isDigit (pole[i]))
{
Out.print("No number entered, enter number");
i=0;
break;
}

}
}
int a = new Integer(number).intValue();
Out.print (a);

}
}
Apr 20 '10 #4
jkmyoung
2,057 Expert 2GB
It's a little dangerous flagging i in that way, but it will work.
The i=1; is meaningless, as it promptly gets reset to i=0 at the for loop.
Apr 20 '10 #5
Hnizdo
5
I transformed the code a bit to be able to set certain borders, now I cannot think about any errors, that could happen. It looks like this:

class test{

public static void main (String[] args){


int i=0,a=1;
String text;
char [] pole;
String number = "";

while (i<1)
{ text = In.readLine();
pole = text.toCharArray();
number = "";
for ( i=0;i<pole.length;i++)
{
if (Character.isDigit (pole[i]))
{
number= number + pole[i];
a = new Integer(number).intValue();
if (a<1 || a>5000)
{
Out.print ("Out of range, enter number between 1 and 5000");
i=0;
break;
}
}
else if (!Character.isDigit (pole[i]))
{
Out.print("No number entered, enter number");
i=0;
break;
}
}
}


Out.print (a);

}
}


What was the danger you were talking about?
Apr 20 '10 #6
Hnizdo
5
and one more question: if I wanted to use it in a program, i suppose I should put it into a method, but I'm not really sure how it should look like and how should I call it. I have some experience with methods, not much though, but this one is weird since it transforms int and string and does not really have any variable which I should for example like

public static int condition (int num)

when I tried to do it like, and called it like number = condition(num) it didnt work.

Really thanks for your help
Apr 20 '10 #7

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

Similar topics

6
by: LRW | last post by:
I have an automated process which uploads a comma separated spreadsheet (csv) and inserts it into a database: $sql = "LOAD DATA INFILE '".$uploadfile."' INTO TABLE `tbl_tracking` FIELDS...
12
by: zhi | last post by:
Really confused, when I use keyword style argument as following: >>> input(prompt="hello") Traceback (most recent call last): File "<pyshell#52>", line 1, in -toplevel- input(prompt="hello")...
3
by: david | last post by:
HI! Im trying to make "HTML form" into automatic. 1. If I get 18 numbers like: A B C D E F . . . . 2. How can I put those 18 numbers automatically into 6 numbers format like: A B C D E F
37
by: Jason Heyes | last post by:
A pythagorean triple is a triple <a,b,c> whose components are positive integers satisfying a*a + b*b = c*c. An example is <3,4,5> since 3*3 + 4*4 = 9 + 16 = 25 = 5*5. I want to write a function...
10
by: jeff regoord | last post by:
A user inputs a float value. The scanf() function gets the value. However, I need to create an error handler with an if else statement saying invalid input if the input is not a number. Does...
9
by: kernelxu | last post by:
hi,everybody. I calling function setbuf() to change the characteristic of standsrd input buffer. some fragment of the progrem is: (DEV-C++2.9.9.2) #include <stdio.h> #include <stdlib.h> int...
8
by: lisaj | last post by:
I'm having huge difficulties producing a script for this: Write a javascript programme that will prompt for, and accept from the user, an input string which contains at least 8 characters. It...
2
by: whojustdenyme | last post by:
Hey guys, I'm new to C and programming..so I need some help. I need a way to round an INPUT number to an INPUT value so for ex: enter a decimal: 3.1459 enter the number to round: 3 Answer:...
9
by: nick048 | last post by:
Hi to all, If in input I enter an int, I need to create a function that verify if my int is in a list of this value: 1 4 7 10 13 16 19 22 25 28 31 34. How can resolve this question. I hope in...
3
by: td0g03 | last post by:
Like the titles says I'm suppose to generate a random number then divide that by a number inputed by a user. The random number can range from 2-8. I tried to do the code, but I get some weird result...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.