2

I have this short snippet of code where I have to check a string to see if it contains integers and possibly a decimal. The string is an amount of money (12.34) so as well it can not go past the fourth index.

My question is I'm being told to use charAt() (and in my code I used matches() and contains() which is wrong) to check for integers and decimals so that this routine will return a boolean that is true if the string works with those parameters, however I'm confused at how to go about converting this to use charAt() instead of matches() and contains().

As well, I'm sorry if I formatted this wrong, or worded something wrong, or the code looks awful, I'm in my first semester of Java and it's my very first programming class I've ever taken so I'm a bit rough.

import java.util.Scanner;

public class Auction
{
    public static void main(String[] args)
    {
      Scanner keyboard = new Scanner(System.in);
      String price;
      String quantity;

      System.out.print("How much money are you willing to bet on this item?: $");
      price = keyboard.next();


      if(price.matches("[0-9]*") && price.length() <= 5)
      {
         Float f = Float.parseFloat(price);
         System.out.printf("$%5.2f", f);
         System.out.println();
      }

      else if(price.matches("[0-9]*") && price.length() <= 5 && price.contains("."))
      {
         Float f = Float.parseFloat(price);
         System.out.printf("$%5.2f", f);
         System.out.println();
      }

      else
      {
         System.out.println("Invalid input");
      }

      System.out.print("What quantity do you want to bid on?: ");
      quantity = keyboard.next();
      if(quantity.contains("."))
      {
         System.out.println("Invalid input");
      }
    }
}
Ramanlfc
  • 8,283
  • 1
  • 18
  • 24
TGen
  • 21
  • 1
  • You should maybe just split it by `array = price.split(".")` and check if the `array[1].length == 2` So you know then that it is valid. – 3kings Nov 15 '15 at 05:02
  • Are there requirements for charAt or is that just a suggestion? There are so many other ways I would do this, just wondering if the chars are part of the lesson... – Michael Nov 15 '15 at 05:05
  • What does "can not go past the fourth index" mean? Did you mean that there can be at most 4 digits after the decimal point? --- Is it correct to assume that value cannot be negative, since you didn't mention it? – Andreas Nov 15 '15 at 05:43
  • 1
    Looking at your code, you seem to allow numbers between `0` and `99999`, but also `.0000` to `.9999`, and anything in between such as `9.999`, `99.99`, and `9.999`. That doesn't seem right. Is it what you intended? Or did you mean to say a number between `0` and `99.99` with at most 2 digits after the decimal point? – Andreas Nov 15 '15 at 05:55
  • Can you comment if any of the solution worked? – Mahendran Ayyarsamy Kandiar Nov 17 '15 at 18:32

2 Answers2

1

I am typing this from a phone. So excuse the mistakes please. have u been asked by your professor to use charAt instead of regex and matches?

if (inpString!= null && !inpString.isEmpty () && inpString.length() <= 5){
   int periodCount = 0;
   for (int i=0; i  < inpString.length (); i++){  
      char c = inpString.charAt (i);
      if (c == '.'){
         periodCount++;
      }else if (c >= '0' && c  <= '9'){


      }else {
              System.out.println("invalid output");
              break;
      }
      if(periodCount > 1){
          System.out.println("too may periods. Invalid output"); 
          break;
      }


   }
}else { 
       System.out.println ("invalid input");
    }

Can you comment if u need to check that there are no thousandth digit i.e 1.234? If yes make sure

          inpString.substring       
              (inpString.lastIndexOf 
                 (".")).length < 3 

with all the null and indexOutOfBounds checks

0

How about this way?

import java.util.Scanner;

public class Auction {

private static final String numberRegex = "^\\d*(\\.\\d+)?$";
private static final String integerNumber = "^\\d*$";
private static final int MAX_LENGTH = 5;

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    String price;
    String quantity;

    System.out.print("How much money are you willing to bet on this item?: $");
    price = keyboard.next();

    if (price.length() <= MAX_LENGTH && price.matches(numberRegex)) {
        Float f = Float.parseFloat(price);
        System.out.printf("$%5.2f\n", f);
    } else {
        System.out.println("Invalid input");
        return;
    }

    System.out.print("What quantity do you want to bid on?: ");

    quantity = keyboard.next();
    if (!quantity.matches(integerNumber)) {
        System.out.println("Invalid input");
    }
}

}

shakhawat
  • 2,639
  • 1
  • 20
  • 36