In java programming write the main and run it for the following method: public s
ID: 3938507 • Letter: I
Question
In java programming write the main and run it for the following method:
public static boolean digitSequence (String s)
Returns whether s is a valid digitSequence that looks like an int.
It return true if and only if s is a sequence of >0 chars, and every char is a digit in ['0','9'] OR
s is a hyphen followed by a sequence of >0 digits. e.g.,
"0" true(simplest case)
"" false(empty String doesn't qualify)
"0123456789" true
long strings are fine:
"-999999999999999999" true
"+1234" false(the only sign allowed is a single hyphen)
"-0" true (another name for 0)
" 8888888888 " false(white space not ok)
"00000000000000012" true (leading 0's are fine)
Explanation / Answer
class Check
{
public static boolean isdigitSequence(String str) {
boolean flag = false;
if (str.length<0)
flag = false;
for (char c : str.toCharArray())
if ('0'=< C<='9' || str.charAt(0)=='-')
{ flag = true;
}
return flag;
}
public static void main(String args[])
{
boolean check = false;
System.out.println("Please enter the string");
Scanner s = new Scanner(System.in)
str = s.nextLine();
check = isdigitSequence(str);
if(check == true)
{System.out.println("The entered string is completley int);
}
else
{System.out.println("Entered string does not contain only int);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.