Results 1 to 9 of 9
  1. #1
    Lander is offline Member
    Join Date
    Jan 2014
    Posts
    5
    Rep Power
    0

    Question Need to count Strings that how many inputs from user were Numbers ...

    import java.util.Scanner;
    public class CountStringNumbers { // show the number of Strings that were numbers . . ..
    public static void main(String []args){

    int count = 0;
    int countNumbers=0;

    Scanner input=new Scanner(System.in);
    System.out.println("Type Something , , , and to end the the termination type STOP");
    String word=input.nextLine();
    if(word.equals("stop"))
    {
    System.out.println("You didnt input anything");
    }
    while(!word.equalsIgnoreCase("stop")){
    if (word.equals("0")||word.equals("1")||word.equals(" 2") ||word.equals("3")||word.equals("4")||word.equals( "5")||word.equals("6")||word.equals("7")||word.equ als("8")||word.equals("9"))
    {
    : (-:// You see ! ! ! I can count the numbers from 0-9 if the user puted but if it is 10 it doesnt count it , and i need to write all the numbers to have that option. . . . Is there an easy way that includes all the numbers? like if(word.equals(number)){<<<???? (numbers) i know htat doesnt work but is there anything similar ?? ?? Thanks
    countNumbers++;
    }

    System.out.println("Type Something , , , and to end the the termination type STOP");
    word=input.nextLine();
    count++;
    }
    System.out.println("You wrote "+count+" lines wich from those lines "+countNumbers+" were numbers and the programe terminated ");

    }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default Re: Need to count Strings that how many inputs from user were Numbers ...

    Why not turn it into a nice little method? The following skeleton will do fine:

    Java Code:
    boolean isNumber(String word) {
       if (word == null) return false; // definitely not a number
       ... // you do the other check(s) ...
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Need to count Strings that how many inputs from user were Numbers ...

    Hi,

    you can use Integer.parseInt(String) to convert the input. If the input is not an int it will throw an Exception.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,157
    Rep Power
    6

    Default Re: Need to count Strings that how many inputs from user were Numbers ...

    Sometimes you want a situation where you try to parse a number and if it doesn't succeed, you set a default in stead of having an exception thrown. I like to do this:

    Java Code:
    public static Integer parseInt(String input, Integer def){
      if(input == null){
        return def;
      }
    
      try{
        return Integer.parseInt(input);
      } catch(Throwable t){
         return def;
      }
    }
    I used Integer in stead of int, because then you have the freedom of returning null.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Lander is offline Member
    Join Date
    Jan 2014
    Posts
    5
    Rep Power
    0

    Default Re: Need to count Strings that how many inputs from user were Numbers ...

    Thank you All , and sorry for this but im really newbie so hope you dont get bothered when i make other questions lol
    I really appreciate your help and hope it comes a day when i can help you too . . .

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,157
    Rep Power
    6

    Default Re: Need to count Strings that how many inputs from user were Numbers ...

    Quote Originally Posted by Lander View Post
    I really appreciate your help and hope it comes a day when i can help you too . . .
    My house can use some painting.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    Lander is offline Member
    Join Date
    Jan 2014
    Posts
    5
    Rep Power
    0

    Default Re: Need to count Strings that how many inputs from user were Numbers ...

    Really ? cool i love painting especially with close eyes

  8. #8
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Need to count Strings that how many inputs from user were Numbers ...

    My cars are dirty...
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  9. #9
    Lander is offline Member
    Join Date
    Jan 2014
    Posts
    5
    Rep Power
    0

    Default Re: Need to count Strings that how many inputs from user were Numbers ...

    I can send those to wash companies :) Just let the keys under the Welcome carpet at your door :D

Similar Threads

  1. Replies: 1
    Last Post: 10-24-2012, 01:21 PM
  2. Question on user inputs and strings
    By steve0827 in forum New To Java
    Replies: 7
    Last Post: 10-04-2012, 03:49 PM
  3. Controlling User Inputs
    By time-space in forum New To Java
    Replies: 5
    Last Post: 05-30-2011, 03:50 PM
  4. Sum positive numbers using 10 inputs from user
    By pvictory1 in forum New To Java
    Replies: 15
    Last Post: 10-10-2010, 02:30 AM
  5. Replies: 1
    Last Post: 09-24-2010, 08:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •