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
Re: need help with phone number program for homework
Hello stevolabo!
Code Java:
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.
Re: need help with phone number program for homework
Please post what the program currently prints out
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
Re: need help with phone number program for homework
Do you have any specific java programming questions?
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?
Re: need help with phone number program for homework
Quote:
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.