cosmicXD
cosmicXD

Reputation: 23

How do you identify if a char entry is a two or more digit number?

What I mean to say is, if I have an array which is delimited by spaces how can I distinguish if two consecutive chars are two or more digit numbers instead? Bear with me I'm still pretty new to programming in general.

this is what I have so far:

import java.util.*;
    public class calc 
    {
        public static String itemList;
        public static String str;
        public static void main(String[] args) 
        {
            Scanner sc = new Scanner(System.in);
            str = sc.nextLine();
            delimitThis();
            sc.close();
        }
        public static void delimitThis()// Delimiter to treat variable and                 numbers as seperate
        {
            List<String> items = Arrays.asList(str.split("\\s+"));
            System.out.println(items);

            for (int i = 0; i < str.length(); i++) 
             {
                    itemList = items.get(i);
                    category();

             }  
        }
        public static void category()////For Filtering between vars and constants and functions
        {
            for (int x = 0; x < itemList.length(); x++)
            {
               char ite = itemList.charAt(x);
        if(Character.isDigit(ite))
        {
            System.out.println(ite + "is numeric"); //Will be replaced by setting of value in a 2 dimensional list
            }
        }
    }

Upvotes: 2

Views: 90

Answers (2)

Nin
Nin

Reputation: 407

First of all, I want to fix your mistakes:

Mistake 1:

// bad
for (int i = 0; i < str.length(); i++) 
{
    itemList = items.get(i);
    category();
}  

You are traversing through List<String> items, but str.length is being used. It is wrong. To {print the item then do category()} for every item in items, the code should be:

// fixed
for (int i = 0; i < items.size(); i++)
{
    itemList = items.get(i);
    category();
}

Mistake 2:

for (int x = 0; x < itemList.length(); x++)
{
   System.out.println(itemList);
}

I'm not sure what you wanted to do here. It's just that your code does not make sense to me. I assume you wanted to print line every character from itemList, the code should look like this:

for (int x = 0; x < itemList.length(); x++)
{
   System.out.println(itemList.charAt(x));
}

Done with the mistakes. Now checking an a string whether it contains 2 digit numbers or more, we can use String.matches() with regular expression:

if(itemList.matches("\\d\\d+")){
    System.out.println(itemList + " is a two or more digit number");
}else{
    System.out.println(itemList + " is NOT a two or more digit number");
}

The code looks like this in the end:

import java.util.*;
public class Calc
{
    public static String itemList;
    public static String str;
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        str = sc.nextLine();
        delimitThis();
        sc.close();
    }
    public static void delimitThis()// Delimiter to treat variable and numbers as seperate
    {
        List<String> items = Arrays.asList(str.split("\\s+"));
        System.out.println(items);

        for (int i = 0; i < items.size(); i++)
        {
            itemList = items.get(i);
            category();
        }
    }
    public static void category()////For Filtering between vars and constants and functions
    {
        for (int x = 0; x < itemList.length(); x++)
        {
            System.out.println(itemList.charAt(x));
        }

        // is 2 digit number or not?
        if(itemList.matches("\\d\\d+")){
            System.out.println(itemList + " is a two or more digit number");
        }else{
            System.out.println(itemList + " is NOT a two or more digit number");
        }
    }
}

Upvotes: 1

Sweeper
Sweeper

Reputation: 272750

To check whether a string is at least a 2 digit number, use this regex: \d{2,}.

public static boolean is2OrMoreDigits(String s) {
    Pattern p = Pattern.compile("\\d{2,}");
    return o.matcher(s).matches();
}

Upvotes: 0

Related Questions