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 7 of 7

Thread: need help with phone number program for homework

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default need help with phone number program for homework

    New to Java programming and need help with my program. The program should output

    1:919:882:5000
    5000
    1
    882
    919

    driver program
    public class SSProg1
    public static void main(String[] args)
    {
    SSDissector phone = new SSDissector("1:919:882:5000");
    System.out.println(phone.getPhoneNumber());
    System.out.println(phone.getPhoneNumber(4));
    System.out.println(phone.getPhoneNumber(1));
    System.out.println(phone.getPhoneNumber(3));
    System.out.println(phone.getPhoneNumber(2));
    }
    class file
    public class SSDissector
    {
    private String colonSeparated; // Full phone number with colon's
    private String countryCode; // Country Code of the phone number only
    private int areaCode; // Area code of the phone number only
    private int prefix; // Prefix of the phone number only
    private int number; // Last four digits of the phone number only
    //************************************************** ******************************
    public SSDissector(String colonSeparated)
    {
    this.colonSeparated = colonSeparated;
    int index1 = colonSeparated.indexOf(":");
    this.countryCode = colonSeparated.substring(index1 - 1, 1);
    //System.out.println(countryCode);
    int index2 = colonSeparated.indexOf(':');
    String areaCode = colonSeparated.substring(index2 + 1, 5);
    //System.out.println(areaCode);
    //this.prefix = prefix;
    int index3 = colonSeparated.lastIndexOf(':');
    String prefix = colonSeparated.substring(index3 - 3, 9);
    //System.out.println(prefix);
    int index4 = colonSeparated.lastIndexOf(':');
    String number = colonSeparated.substring(index4 + 1);
    //System.out.println(number);
    }
    //************************************************** ********************************
    // This method returns the string colonSeparated
    public String getPhoneNumber()
    {
    return this.colonSeparated;
    }// End getPhoneNumber()
    //************************************************** ********************************
    // This Method returns the corrisponding section of the number
    public int getPhoneNumber(int x)
    {
    if (x==1)
    {
    int countryCodeInt = Integer.parseInt(countryCode);
    return countryCodeInt;
    }
    if (x==2)
    {
    return areaCode;
    }
    if (x==3)
    {
    return prefix;
    }
    if (x==4)
    {
    return number;
    }
    else
    {
    return number;
    }
    }// End getPhoneNumber(int)
    }// End MFDissector


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: need help with phone number program for homework

    Hello stevolabo!

    String areaCode = colonSeparated.substring(index2 + 1, 5);
    Can you explain what are you trying to do here (in the left side of the assignment)? If you are trying to cast your class member areaCode from int to String, firstly this is not the appropriate way and secondly an int cannot be casted to String.
    I think that it would help if you made all of your class's members Strings, instead of having some of them ints and others Strings.
    And please wrap your code with the highlight tags to improve its readability.
    Hope it helps.

  3. #3
    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: need help with phone number program for homework

    Please post what the program currently prints out
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help with phone number program for homework

    This is what I'm trying to do


    YourInitialsDissector.java
    Implement a YourInitialsDissector.java class that stores the phone number as a colon separated string of numbers and as four separate pieces described below.
    You must implement all of the following:

    colonSeparated – a colon separated String. Example value: "1:919:882:5000"
    countryCode – stored as a String as it could hold 001
    areaCode, prefix, number – stored as int variables

    This constructor receives one parameter, a colon-separated string. You may assume that the parameter’s value is valid (i.e., no error checking required). The constructor initializes the instance variables with appropriate values. There are many ways to solve the problem of extracting the sections from the given colon separated string. You are required to use String methods to extract the individual sections as strings, and then use parseInt method calls to convert the strings to int’s for those that need it. Do NOT use split as it has not been covered yet.

    This is a standard accessor method that simply returns the colonSeparated instance variable’s value.

    This method receives the position of one of the section (1, 2, 3, or 4) and returns the section that’s at that position. Area Code is 1, etc.


    Sample Session:

    When using the main method specified above, your output should be:
    1:919:882:5000
    5000
    1
    882
    919

  5. #5
    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: need help with phone number program for homework

    Do you have any specific java programming questions?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help with phone number program for homework

    Sorry, I have took java 1 a while ago, and now am taking java 2. This is my first program back, and struggling. the driver class was given to us from our teacher. So I have to create the dissector program. The bottom part of the program isn't working. I'm wondering how to write the last part of the program?

  7. #7
    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: need help with phone number program for homework

    how to write the last part of the program
    Use an editor to write the code for the last of the program.
    If you have any specific java programming questions, ask them.
    How to write the code is not a specific question.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. HELP!! Phone Directory Program
    By lahegemon in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 6th, 2011, 09:27 AM
  2. The Rational Class Program [HOMEWORK HELP]
    By FCarlton24 in forum Object Oriented Programming
    Replies: 1
    Last Post: October 18th, 2011, 12:31 PM
  3. i like to play this game with my phone and i wanted to make a program to help me
    By Imreallyawesome in forum What's Wrong With My Code?
    Replies: 25
    Last Post: August 12th, 2011, 07:34 PM
  4. HELP! Server side validation for phone number
    By LexenZ in forum JavaServer Pages: JSP & JSTL
    Replies: 5
    Last Post: June 19th, 2011, 10:29 AM
  5. need help with homework program...thanks
    By robertsbd in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 5th, 2010, 03:12 PM