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.
6 10642
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. - boolean needNumber = true;
-
while (needNumber)
-
needNumber = false;
-
// your main code.
-
// input string.
-
// parse ...
-
...
-
else if (!Character.isDigit (pole[i]))
-
{
-
Out.print("No number entered, enter number");
-
needNumber = true; // reset so we have to enter again.
-
}
-
}// end for
-
}// end while
-
//convert and output number.
2. break method.
Put a while around the same part, but declare i earlier.: - int i = 0;
-
while (i<pole.length){
-
...
-
...
-
else if (!Character.isDigit (pole[i]))
-
{
-
Out.print("No number entered, enter number");
-
break; // break for loop
-
}
-
}//end for
-
}//end while
If we break unnaturally, then i<pole.length is still true.
Are you trying to add all the digits?
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.
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);
}
}
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.
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?
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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")...
|
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
|
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...
|
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...
|
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...
|
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...
|
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:...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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
|
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...
|
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...
| |