Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Phone Number Problem

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Phone Number Problem

    In this program we are asked to request the user to enter a phone number in the following format:

    (0xx)xxxxxxxxx

    The first number must be 0 and there can be up to 4 digits inside the brackets (inculding the 0). The remainder is is a number between 5-7 digits long.

    For example, lets assume that the user enters (055)123456

    We are then trying to convert the number into the following format +353-55-123456 as an output

    Where I am stuck at the moment is that I am trying to get the position index of where the character ")" is located so that I can split the number and allow me to produce the required output. However when I try and get the position of ")" i receive and error in the compiler saying "method Integer.parseInt(String is not applicable"
    Can someone have a look an see if they can point me in the right direction?

    Thanks

    import javax.swing.JOptionPane;
    public class Week8Q1
    {
    	public static void main(String [] agrs)
    	{
    		String x = ")";
    		String num2;
    		String num3;
    		String num1 = "";
    		String pattern = "\\(0[0-9]{1,3}\\)[0-9]{5,7}";
    		String num4 = "+353-";
    		String number = JOptionPane.showInputDialog(null, "Please enter a number using the following format \n(xxxx)xxxxxxx");
    		int length = number.length();
    		if (number.equals(""))
    		JOptionPane.showMessageDialog(null, "You have not entered a valid number");
    		else
    		if (!(number.matches(pattern)))
    		JOptionPane.showMessageDialog(null, "You have not entered a number in a valid format");
    		else
    		num1 = Integer.parseInt(x.indexOf(number));
    		num2 = number.substring(2,(num1+1));
    		num3 = number.substring((num1+1),length);
    		JOptionPane.showMessageDialog(null, "Your number in international format is :\n " + num4 + num2 + num3);;
    	}
    }
    Last edited by digitalsystems; November 5th, 2013 at 10:41 AM. Reason: incorrect code


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Phone Number Problem

    Look at the API doc for what arg is required for the parseInt() method. Are you passing what it requires?
    What does the indexOf() method return?
    What value do you want to assign to num1? Does the parseInt() method return that type of data?

    The API doc is here: Java Platform SE 7


    You should take some time to read the API doc for the methods you are trying to use. Their usage in the code does not make sense.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. phone number generator
    By yuli in forum What's Wrong With My Code?
    Replies: 18
    Last Post: November 4th, 2013, 05:36 PM
  2. phone number generator1
    By yuli in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 4th, 2013, 04:37 PM
  3. Call to any phone number from a webpage
    By yaatri in forum Web Frameworks
    Replies: 0
    Last Post: April 10th, 2012, 12:41 PM
  4. need help with phone number program for homework
    By stevolabo in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 23rd, 2012, 07:34 PM