Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6637535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:19:38+00:00 2026-05-25T23:19:38+00:00

Whenever I type in a phone number, this program below that I wrote to

  • 0

Whenever I type in a phone number, this program below that I wrote to format phone numbers from the user gives me back weird numbers that I did not even enter at all. Can someone please explain to me why I am getting such weird errors?

I want it so when someone enters just 12345678978 it will format to 1-234-567-8978
If they enter 2345678978 it will format to 234-567-8978
And if they enter 5678978 it will change to 567-8978.
I always get weird numbers that sometimes aren’t even what I entered like
12345678978 I get 144-34–567-
2345678978 I get 153-567-8978
5678978 I get 162-8978

I would really appreciate some help. Thanks.

    import java.util.Scanner;

    public class Test3 {
public static void main(String[] args) {
    Scanner y = new Scanner(System.in);
    String phoneNumber;
    int phoneNumberLength;
    System.out.print
        ("Please enter your phone number WITHOUT spaces or dashes: ");
    phoneNumber = y.nextLine();
    phoneNumberLength = phoneNumber.length();
    if (phoneNumberLength == 11) {
        phoneNumber = phoneNumber.charAt(0) + "-" + phoneNumber.charAt(1)
            + phoneNumber.charAt(2)
            + phoneNumber.charAt(3)
            + "-" + phoneNumber.charAt(4) + phoneNumber.charAt(5)
            + phoneNumber.charAt(6)
            + "-" + phoneNumber.charAt(7) + phoneNumber.charAt(8)
            + phoneNumber.charAt(9)
            + phoneNumber.charAt(10);
    }
    if (phoneNumberLength == 7) {
        phoneNumber = phoneNumber.charAt(0) + phoneNumber.charAt(1)
            + phoneNumber.charAt(2)
            + "-" + phoneNumber.charAt(3) + phoneNumber.charAt(4)
            + phoneNumber.charAt(5) + phoneNumber.charAt(6);
    }
    else {
        phoneNumber = phoneNumber.charAt(0) + phoneNumber.charAt(1)
            + phoneNumber.charAt(2)
            + "-" + phoneNumber.charAt(3) + phoneNumber.charAt(4)
            + phoneNumber.charAt(5)
            + "-" + phoneNumber.charAt(6) + phoneNumber.charAt(7)
            + phoneNumber.charAt(8)
            + phoneNumber.charAt(9);
    }
    System.out.println("So your phone number is " + phoneNumber + "?");
}

By the way. I know it is not formatted correctly but I am very confused with how stackoverflow allows you to add code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:19:39+00:00Added an answer on May 25, 2026 at 11:19 pm

    Any string that starts like this:

    number = number.charAt(0) + number.charAt(1) + ...
    

    will cause the problem, because you are adding two char types together. This is treated as integer arithmetic, not string concatenation. It would be a lot better to add substrings together, so that the operator is string concatenation, instead of integer addition.

    number = number.substring(0, 3) + '-' + number.substring(3, 6) + ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

whenever I run this below it gives the output but it also gives an
So I'm making this android application, that needs to read data from user-provided CSV
$(#epFilter).keypress(function(){ query = $(#epFilter).val(); alert(query); }); In this code, whenever I type in my
I've been getting this error in emacs whenever I type anything in certain buffers:
Whenever I type two curly braces ( {{ ), I get something like that:
The C# compiler requires that whenever a custom type defines operator == , it
Whenever I build+run my program, it displays all this extra stuff: GNU gdb 6.3.50-20050815
I have a showInputDialog. Whenever I type something to that field, I want it
I have a HTML Text Box, Whenever I type text to this text box,
I've gotten used to , when i press M-x that whenever i type a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.