473,408 Members | 2,027 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,408 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 10648
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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...
0
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.