Question

Numbers with embedded dashes play a huge role in our lives. Think of your phone number...

Numbers with embedded dashes play a huge role in our lives. Think of your phone number or Social Security number. Write a program that uses a Scanner to take a single line of user input. You may assume this input consists of any number of characters and contains zero, one or two dashes. Use only String's indexOf, lastIndexOf, and substring methods. Sample input might be

123-45-6789

or

919-555-1212

or

hare-brained

Your program should remove the dashes and print the digits. The output for the above input would be

123456789

and

9195551212

and

harebrained

Program must haves:

  • Program uses only indexOf, lastIndexOf and substring methods
  • Program works with any string containing at most two dashes
  • Program uses nextLine to read user input
  • Program prints the user input with dashes removed
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;
public class MyClass {
public static void main(String args[]) {
Scanner in=new Scanner(System.in);
String str;
System.out.println("User Input: ");
str=in.nextLine();
int indx1=str.indexOf("-");
int indx2=str.lastIndexOf("-");
String res="";
  
  
if(indx1==indx2)
{
res=str.substring(0, indx1)+str.substring(indx2+1);
}
else
{
res=str.substring(0, indx1)+str.substring(indx1+1, indx2)+str.substring(indx2+1);
}
  
System.out.println(res);
  
  
}
}

Add a comment
Know the answer?
Add Answer to:
Numbers with embedded dashes play a huge role in our lives. Think of your phone number...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • !!!!!!!Java!!!!! When you are confident that your methods work properly and that you can generate random...

    !!!!!!!Java!!!!! When you are confident that your methods work properly and that you can generate random text with my generateText method, you can move on to the second step. Create a third class called Generator within the cs1410 package. Make class. This class should have a main method that provides a user interface for random text generation. Your interface should work as follows: Main should bring up an input dialog with which the user can enter the desired analysis level...

  • Program Set 2 10 points) Credit card number validation Program Credit card numbers follow certain patterns:...

    Program Set 2 10 points) Credit card number validation Program Credit card numbers follow certain patterns: It must have between 13 and 16 digits, and the number must start with: 4 for Visa cards 5 for MasterCard credit cards 37 for American Express cards 6 for Discover cards In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly or whether a credit card...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT