Java String Examples

Check if string contains valid number example

Want to learn quickly?
Try one of the many quizzes. More than Java 400 questions with detailed answers.

8 Comments

  • my question is how to find out largest number in string input {10 20 30} o/p- is 30.
    please give me a examples on that

    • import java.io.*;
      import java.util.*;
      class largest
      {
      public static void main(String[] args)
      {
      int large=0,a;
      Scanner in=new Scanner(System.in);
      System.out.println(“Enter the input and type zero”0″ to end”);
      int a=in.nextInt();
      while(a!=0)
      {
      if(a>large)
      {large=a;}
      a=in.nextInt();
      }
      System.out.println(“The largest number is”+large);
      }}

  • 17. if( str[i].indexOf(“.”) > 0 )
    i have a doubt about this line of code.
    if i will take an string as “.20”, will it work ?. Because index of “.” here is equal to 0.
    So it will show the output as .20 is not a valid integer number.

  • Isn’t this a better version
    public class CheckNumber {

    public static void main(String[] args) throws IOException {
    System.out.println(“Enter a valid number:”);
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String input = br.readLine();
    try {
    Double numD = Double.parseDouble(input);
    System.out.println(numD + ” is a valid number.”);
    } catch (NumberFormatException nme) {
    // TODO Auto-generated catch block
    System.out.println(input + ” is not a valid number.”);
    }
    }
    }

Sponsors

Facebook Fans