Re: print problem using isDigit

Discussion in 'Java' started by Donkey Hottie, Sep 29, 2008.

  1. Sabine Dinis Blochberger <> wrote in
    news::

    > remeday86 wrote:
    >
    >> oh nvm. I got it :p
    >>

    >
    > I mind. What was the solution?


    quite obvious to me. He prints the whole "problem" string on every round
    of the loop of one character of it.


    >
    > Also, please do not top-post.
    >>
    >>
    >>
    >> remeday86 wrote:
    >> >Hi,
    >> >
    >> >I am still an amateur in java. Can someone please fix my program.
    >> >Everytime I input "3*4" or any of that format, it prints twice. Here
    >> >is my code:
    >> >
    >> > public static void main(String[] args) {
    >> >
    >> > //Input a math problem
    >> > String problem;
    >> > int numberOfCharacters;
    >> >
    >> > problem = JOptionPane.showInputDialog(null, "Input a math
    >> > problem:"); numberOfCharacters = problem.length();
    >> >
    >> > //Determine whether input digit are digits.
    >> > for (int i = 0; i <numberOfCharacters; i++)
    >> > {
    >> > if (Character.isDigit(problem.charAt(i))) {
    >> > System.out.print(problem);
    >> > }
    >> > else{
    >> > System.exit(1);
    >> > }
    >> > }
    >> > }
    >> >
    >> >All I'm trying to do is print if the problem if its "4 * 5" or "5 +
    >> >7" etc. but if it isn't (ie letters), the program will exits. :( I
    >> >dont understand why it prints twice if it is a digit.

    >>

    >
    >
     
    Donkey Hottie, Sep 29, 2008
    #1
    1. Advertising

  2. Donkey Hottie wrote:
    > Sabine Dinis Blochberger <> wrote in
    > news::
    >
    >> remeday86 wrote:
    >>
    >>> oh nvm. I got it :p
    >>>

    >> I mind. What was the solution?

    >
    > quite obvious to me. He prints the whole "problem" string on every round
    > of the loop of one character of it.


    I thought that, and then I saw that he exits at the first non-digit
    character. So for "4 * 5" it would go
    "4" is a digit - print "4 x 5"
    " " is not so exit.

    Wouldn't it?

    >
    >> Also, please do not top-post.
    >>>
    >>>
    >>> remeday86 wrote:
    >>>> Hi,
    >>>>
    >>>> I am still an amateur in java. Can someone please fix my program.
    >>>> Everytime I input "3*4" or any of that format, it prints twice. Here
    >>>> is my code:
    >>>>
    >>>> public static void main(String[] args) {
    >>>>
    >>>> //Input a math problem
    >>>> String problem;
    >>>> int numberOfCharacters;
    >>>>
    >>>> problem = JOptionPane.showInputDialog(null, "Input a math
    >>>> problem:"); numberOfCharacters = problem.length();
    >>>>
    >>>> //Determine whether input digit are digits.
    >>>> for (int i = 0; i <numberOfCharacters; i++)
    >>>> {
    >>>> if (Character.isDigit(problem.charAt(i))) {
    >>>> System.out.print(problem);
    >>>> }
    >>>> else{
    >>>> System.exit(1);
    >>>> }
    >>>> }
    >>>> }
    >>>>
    >>>> All I'm trying to do is print if the problem if its "4 * 5" or "5 +
    >>>> 7" etc. but if it isn't (ie letters), the program will exits. :( I
    >>>> dont understand why it prints twice if it is a digit.

    >>

    >



    --
    RGB
     
    RedGrittyBrick, Sep 29, 2008
    #2
    1. Advertising

  3. Donkey Hottie

    Bo Vance Guest

    RedGrittyBrick wrote:

    >
    > I thought that, and then I saw that he exits at the first non-digit
    > character. So for "4 * 5" it would go
    > "4" is a digit - print "4 x 5"
    > " " is not so exit.
    >

    12 * 1
    "1" is a digit - print "12 * 1"
    "2" is a digit - print "12 * 1"
    ....

    John B. Matthews posted a nice expression parser around here recently.
     
    Bo Vance, Sep 29, 2008
    #3
  4. Donkey Hottie

    Bo Vance Guest

    RedGrittyBrick wrote:

    >
    > I thought that, and then I saw that he exits at the first non-digit
    > character. So for "4 * 5" it would go
    > "4" is a digit - print "4 x 5"
    > " " is not so exit.
    >
    > Wouldn't it?
    >


    Yep.
     
    Bo Vance, Sep 29, 2008
    #4
  5. Bo Vance wrote:
    > RedGrittyBrick wrote:
    >
    >>
    >> I thought that, and then I saw that he exits at the first non-digit
    >> character. So for "4 * 5" it would go
    >> "4" is a digit - print "4 x 5"
    >> " " is not so exit.
    >>

    > 12 * 1
    > "1" is a digit - print "12 * 1"
    > "2" is a digit - print "12 * 1"
    > ...


    Yes, I guess the OP misstated the input that led to the stated output.


    --
    RGB
     
    RedGrittyBrick, Sep 29, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alf P. Steinbach

    isdigit() for characters greater than 127

    Alf P. Steinbach, Oct 9, 2004, in forum: C++
    Replies:
    4
    Views:
    3,231
    James Gregory
    Oct 9, 2004
  2. Replies:
    9
    Views:
    560
    Andreas Leitgeb
    Feb 9, 2007
  3. Carramba

    passing array to isdigit()

    Carramba, Feb 9, 2005, in forum: C Programming
    Replies:
    15
    Views:
    794
    Michael Mair
    Feb 21, 2005
  4. mdh

    isdigit

    mdh, Oct 6, 2006, in forum: C Programming
    Replies:
    10
    Views:
    815
  5. MooMaster
    Replies:
    2
    Views:
    393
    Dan Bishop
    May 1, 2008
Loading...

Share This Page