0

I'd like to be able to force a 10 character input using StringBuilder. I know it can be set to a max char limit but can I make it exactly 10?

Maybe something like this:

import java.util.Scanner;
public class Phonenumber
{
public static void main(String[] args)
{
  Scanner input = new Scanner(System.in);
  StringBuilder num = new StringBuilder(System.in);

  System.out.println("What is your 10 digit phone number?");
  num = input.nextStringBuilder();
  while(!(num =(10 characters)) // I don't know how to phrase this.
  {
     System.out.println("Sorry, you entered the wrong ammount of digits");
  }
  if(num =(10 characters)
  {
     System.out.println("Your Phone number is " + num); 
  }     
  }
  }

4 Answers4

1
<input type="text" id ="idOfValueToTest" />
<input type="submit" id ="btnValidate" value="Submit" onclick="return Validate()" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">     </script>

<script type="text/javascript">
function Validate() {
    var validation = document.getElementById("idOfValueToTest").value;
    var pattern = /^\d{10}$/;
    if (pattern.test(validation)) {
        return true;
    }
    return false;
}
</script>

Try this!

I'm sorry i misread java to javascript. The regex should work though. Try:

public static boolean isValid(String input)
{
   String regex = "^\d{10}$";
   if(input.matches(regex))
   {
     return true;
   }
   return false;
 }

Hi, i see what you're saying. Well as other have said you should not use stringbuilder to do that. If you're looking for resources then i would suggest reading the class description here: https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html . Basically as the name says, a stringbuilder builds strings! It might be a weird way to actually do what you want using append but that would be really weird and really bad programming. I will add an example of what you should NOT do!

String text = "abcdefghijklmnopq";
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 10; i++)
{
  builder.append(Character.toString(text.charAt(i)));
}
 String result = builder.toString();
 System.out.println(result);

See what other people posted if you're looking a most commonly used way to do this. If regex seems complicates then i would suggest going with .length as that is pretty straight forward, but do some research on regex, is a very good tool!

Stelios
  • 41
  • 5
  • While that may get the desired result, thank you, I was actually asking if StringBuilder can be used for this. I'm trying to learn about the capabilities of string builder. I will play with that code but I think it may be more advanced than I can handle at this time. – Ryan Martin Aug 20 '16 at 11:35
1

Best alternate is to use String instead of StringBuilder and use String.length() method to validate input from user and your code also needs some correction..

import java.util.Scanner;
public class Phonenumber
{
public static void main(String[] args)
{
  Scanner input = new Scanner(System.in);
  //StringBuilder num = new StringBuilder(System.in); As we are using String,StringBUilder is not needed

  System.out.println("What is your 10 digit phone number?");
  String num = input.nextLine();
  if(!(num.length()==10)) // Correction: If you use while here and user enters input not having 10 digits it will go to endless loop
  {
     System.out.println("Sorry, you entered the wrong ammount of digits");
  }
  if(num.length()==10) //Here you can use else to make your code more appropriate in case of time complexity(Here Time complexity won't matter but as you seem to be new to java, I think you should check this property)
  {
     System.out.println("Your Phone number is " + num); 
  }     
  }
  }
Sanket Makani
  • 2,432
  • 2
  • 13
  • 23
1

Try this:

System.out.println("What is your 10 digit phone number?");
String num = input.nextLine();
while (!num.matches("\\d{10}")) {
    System.out.println("Sorry, you did not enter exactly 10 digits. Try again");
    num = input.nextLine();
}

This checks that the length is exactly 10 and that all characters are digits.

But you could go one step better and remove non-digits in case the user entered formatting characters (like brackets, dashed and spaces), yet still accept the input, by changing the reads to:

input.nextLine().replaceAll("\\D", "");

FYI \D means "non digit". This kind of input cleansing is what every input field should have in the real world, but all to often does not.

Bohemian
  • 365,064
  • 84
  • 522
  • 658
0

My main problem was that I was trying to have user input and limit it to 10 characters. I recently found out that it is impossible to fill a StringBuilder from user input. I can amend it once it's been converted to a string but I can't use String Builder to directly limit it.

  • and StringBuilder has no limit, it only has an initial size of the buffer, but it extends automatically. – eckes Aug 20 '16 at 14:40