-3

I want to know how to do I check my sub string if it is a numerical value or if it is just words.

For example, if my substring d=150.63

I have done some research and I've found Character.is Digit(), but it is not working. Below is the code I have made so far.

import java.util.*;
import java.io.*;

public class Prac34 {

    private Scanner x;

    public void openFile(){
      try{
        x=new Scanner(new File("sales.txt"));
      }
      catch (Exception e){
        System.out.println("could not find file");    
      }
    }

    public void readFile(){
      //int count=0;
      double total=0;
      while(x.hasNext()){

        String a=x.nextLine();
        int v=a.length();

        int b=a.indexOf(':');
        String c= a.substring(0,(b+1));

        String d=a.substring((b+1),v);
        double e= Double.parseDouble(d);

        if (Character.isDigit(d)) {
          total=total+e;
        }


        System.out.println(c);
        System.out.println(d);
}
}

    public void closeFile(){
      x.close();
    }
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
Geen.Kl
  • 3
  • 5
  • 9
    You should *really* look into good variable naming conventions. – Mike Elofson Nov 20 '15 at 20:48
  • 5
    Possible duplicate of [How to check if a String is a numeric type in Java](http://stackoverflow.com/questions/1102891/how-to-check-if-a-string-is-a-numeric-type-in-java) – Mike Elofson Nov 20 '15 at 20:53
  • Just use a [Apache Library](https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/math/NumberUtils.html) – StackFlowed Nov 20 '15 at 20:59
  • I'm not sure I understand the type of String expected as input. Which of the following are valid to your use-case? `String noNumbers = "To be or not to be"; String withNumbers = "2B||!2B"; String intNumber = "222"; String floatNumber = "22.2";` Or, what boolean would you expect back for each of them? – John Teixeira Nov 20 '15 at 21:07

2 Answers2

1

You could just try to parse the string:

try {
    double e = Double.parseDouble(d);
    total += e;
} catch (NumberFormatException ignore) {
    // Not a number, skipping
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • For exaple d may be equal to 100 in one case but may be equal to "No records found" , therefor i am just wondering how to make Character.isDigit to work in my program? Any ideas? Thanks ! – Geen.Kl Nov 20 '15 at 20:59
  • You don't need `Character.isDigit` - parsing `d` will throw a `NumberFormatException`, which you can catch and skip, as illustrated above. – Mureinik Nov 20 '15 at 21:01
0

Using a regex(regular expression):

String text="25.54";
     if(text.replace(".","").matches("[0-9]+")){ //or you can use also "\\d+"
         System.out.println("digit");
     }else{
      System.out.println("no digit");
     }

     System.out.println(text);

Also:

where (?) means none or one time

where (*) means none or more times

where (+) means one or more times

|||You can be a pro here

GOXR3PLUS
  • 6,877
  • 9
  • 44
  • 93
  • And will it work for a HexaDecimal representation of a number? Or a binary one? Or even with the `1_000` formatting? – Yassin Hajaj Nov 20 '15 at 21:12
  • You could use regex, but if you're going to parse it as a `double` anyway, you might as well just try to parse it like @Mureinik suggested, and catch the `NumberFormatException` to handle if it is not a `double`. – Evan LaHurd Nov 20 '15 at 21:13