Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I've recently started to learn Java using a website called codehs but I'm stuck on this problem. It wants me to create a method called isInteger to determine if a string is an integer or not and return true if it is or false if it's not. Any idea how I can do this? I've tried a lot of stuff but nothing seems to work and I can't just do

System.out.println(Character.isDigit('1'));

because it wants me to create a method called isInteger to perform this task, any ideas?

What I have tried:

Java
public class Scratchpad extends ConsoleProgram
{
    public void run()
    {
        // Add your own tests here
        System.out.println(isInteger(str));
    }

    // Copy and paste your Unit Test method here
    public boolean isInteger(String str)
    {
    
        if(Character.isDigit('1'))
        {
            return true;
        }
        else
        {
            return false;   
        }
    }

}
Posted
Updated 9-Mar-23 8:45am
v2
Comments
wseng 5-Jun-17 22:55pm    
In System.out.println(isInteger(str)); Where you define str ?
MSUSER30 6-Jun-17 0:50am    
Thanks, the code bellow worked.I also realized instead of putting str in the print output I had to add a character in single quotes.
wseng 6-Jun-17 11:34am    
I'm glad it helped.

To check value is Integer or not

public boolean isInteger(String number ){
    try{
        Integer.parseInt(number);
    }catch(Exception e ){
        return false;
    }
    return true;
}
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 6-Jun-17 0:01am    
5ed.
wseng 6-Jun-17 11:35am    
Big thanks !
I ran a code that worked for all but one of their situations. It turns out that one of the inputs they test is typed incorrectly. One of them should be "1O1" (the ones are numbers and the other is a capital O) but they typed it up as "101" where they are all zeros and it doesn't pass their grading window. You can see the difference in the window at how they are typed and know what it should be.
My code that worked for everything else is:

public boolean isInteger(String str) //char was String
{
for(int i = 0; i < str.length(); i++)
{
if(Character.isDigit(str.charAt(i)))
return true;
}
return false;
}

A little late for this individual but may help others as they come across it.
 
Share this answer
 
Your code works correctly. Is codeHS checkcode function not letting you pass? If so, are their any specifications they request for in the description?
 
Share this answer
 
public boolean isInteger(String str)
{

if (str.equals(""))
{
return false;
}

for(int i=0; i< str.length();i++)
{

char ch = str.charAt(i);
if(!Character.isDigit(ch))
{
return false;
}

}
return true;
}
 
Share this answer
 
public boolean isInteger(String str)
{
    int length = str.length(); //String length

    boolean isNum;
    boolean integer; //<--- final var used for return

    int yay = 0; // 1 added for every number
    int na = 0; // 1 added for every string
    
// In case the string is ""
    if(length == 0)
    {
        integer = false;
        return integer;
    }
    
//The real deal. . .
    for (int i = 0; i < length; i++)
    {
        char digit = str.charAt(i); //Receives 1 char at a time
        isNum = Character.isDigit(digit); //Finds out if char is a digit
        
        if(isNum == true)
        {
            yay++; //Adds one for every integer
        }
        else
        {
            na++; //Adds one for every non-integer
        }
    }
    
    if (na > 0)
    {
        integer = false; //That means all of the digits were true for isNum variable
    }
    else
    {
        integer = true; //That means a/many digits were false for isNum variable
    }
    
    return integer;
}
 
Share this answer
 
Comments
Richard Deeming 10-Mar-23 4:28am    
Really bad code; once you've found a non-numeric character, why keep checking the other characters? You already know what the function needs to return.
CHill60 10-Mar-23 4:42am    
Reasons for my downvote:
- This is far too long-winded. Java provides a function Integer.ParseInt - see Solution 1
- Even if that were not the case, this is inefficient - once you have found a non integer character simply drop out of the loop - see Solution 3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900