Note that there are some explanatory texts on larger screens.

plurals
  1. POI do not know why I keep on getting strange feedback when I am trying to format phone numbers from a user in java.
    primarykey
    data
    text
    <p>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?</p> <p>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</p> <p>I would really appreciate some help. Thanks.</p> <pre><code> 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 + "?"); } </code></pre> <p>By the way. I know it is not formatted correctly but I am very confused with how stackoverflow allows you to add code. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload