BP Forums
Character ammount - Printable Version

+- BP Forums (https://bpforums.info)
+-- Forum: Programming Discussion (https://bpforums.info/forumdisplay.php?fid=34)
+--- Forum: Java (https://bpforums.info/forumdisplay.php?fid=41)
+--- Thread: Character ammount (/showthread.php?tid=912)

Character ammount - .righteous - 04-25-2016

I've just finished watching your tenth "Learning Java" part and I want to limit the character number to five digits only. 
I've tried this:
import java.util.*;
public class digitExtractorTester {
    static Scanner userInput = new Scanner(System.in);
    public static void main(String[] args){
        String rNumber = "12345";
        System.out.print("Enter a custom, five digit number: ");
        String nSequence = userInput.next();
        System.out.println(" ");
        if (nSequence.chars() != rNumber.chars()){
            System.out.println("Your input contains too many or too less digits.\nOnly five digit numbers are allowed.");
        digitExtractor byMath = new digitExtractor(Integer.valueOf(nSequence));
        System.out.println(" ");
        digitExtractor byChar = new digitExtractor(nSequence);
But it doesn't seem to be working. Any suggestions?

Never mind. I solved my own problem.
If anyone's wondering, I was using chars() when I was supposed to be using length(). (Source of information)

nSequence.length() != rNumber.length()

RE: Character ammount - brandonio21 - 04-28-2016

Hello .righteous and welcome to BP Forums! I'm glad to hear that you solved your issue. For your reference, most languages use the convention of "length()" to get the number of characters within a string.

(Ruby, Javascript, Rust, C#.net)