By using this site, you agree to our Privacy Policy and our Terms of Use. Close

Forums - General - Does anyone here know java?

I'm really stuck on this program and need help fast @_@



MaxwellGT2000 - "Does the amount of times you beat it count towards how hardcore you are?"

Wii Friend Code - 5882 9717 7391 0918 (PM me if you add me), PSN - MaxwellGT2000, XBL - BlkKniteCecil, MaxwellGT2000

Around the Network

I know Java. Depends on how advanced the program is you are trying to do

I have 2 years of it under my belt, now.



haha this is an intro class but I've got about 30 minutes to fix this code and I'm not sure why it's not compiling, I'll show you what he's asking for and then my code, if you could lead me in the right direction it'd help a lot XD

Overview: The purpose of this project is to give you experience in implementing selection and iteration structures. You will create a class named Phone which will encapsulate a phone number entered as characters and converted to digits.

Often businesses use phone numbers that are mnemonic (contain meaning) to make the number easier to remember such as 1-800-FLOWERS (1-800-356-9377), 1-800-CallAtt (1-800-225-5288), and 1-800-Discover (1-800-347-2683). Notice that some of the “words” are in mixed case and some are longer than 7 characters. The Phone class will contain 2 Strings: one that contains the characters typed in by the user after the 1-800- (in this case FLOWERS, CallAtt, or Discover) which are passed to the constructor as a String and one that contains the characters converted to a phone number also stored in a String (in this case 356-9377, 225-5288, 496-8294, or 347-2683). Notice that the converted numbers contain 7 digits even when the “word” contains more than 7 eligible characters.

Specifications: Code a public class named Phone as described below. As always you are not authorized to add, modify, or remove members of this class.

Data members:
A private String named word.
A private String named number.

Constructor:
A public constructor that takes one String parameter. It initializes word to the parameter. It then calls the setNumber method (see below).

Methods:
setNumber: a private support method that returns nothing and takes no parameters. It should first assign an empty String (“”) to number to avoid the word “null” appearing at the beginning of number. It converts the characters in word to digits and places the digits in number. The conversion is based on the way letters are assigned to the numbers on a telephone: 2 has ABC, 3 has DEF, 4 has GHI, 5 has JKL, 6 has MNO, 7 has PRS, 8 has TUV, and 9 has WXY. Note that Q and Z are not on any key for the purposes of this project. The conversion should not be case sensitive, in other words, the characters ‘a’ and ‘A’ both should be converted to ‘2’ etc. If a character in word is not a letter on a key it should be skipped and not be converted to a digit. number should contain the number of digits as there are eligible characters in word or 7, whichever is less. If number ends up containing more than 3 digits then a hyphen ( - ) should be placed between the third and fourth digits.

toString: a public method that returns number.


My code:

public class Phone
{

private String word;
private String number;

public Phone(String word)
{
setNumber();
}

private int setNumber();
{
return null;
number = String("");

if(word == A, B, C, a, b, or c)
{
number = number + "2";
}
else(word.next == D, E, F, d, e, or f)
{
number = number + "3";
}
else(word.next == G, H, I, g, h, or i)
{
number = number + "4";
}
else(word.next == J, K, L, j, k, or l)
{
number = number + "5";
}
else(word.next == M, N, O, m, n, or o)
{
number = number + "6";
}
else(word.next == P, R, S, p, r, or s)
{
number = number + "7";
}
else(word.next == T, U, V, t, u, or v)
{
number = number + "8";
}
else(word.next == W, X, Y, w, x, or y)
{
number = number + "9";
}

}

public String toString()
{
return number;
}
}



MaxwellGT2000 - "Does the amount of times you beat it count towards how hardcore you are?"

Wii Friend Code - 5882 9717 7391 0918 (PM me if you add me), PSN - MaxwellGT2000, XBL - BlkKniteCecil, MaxwellGT2000

It's giving me an ")" expected error when I try to compile :\



MaxwellGT2000 - "Does the amount of times you beat it count towards how hardcore you are?"

Wii Friend Code - 5882 9717 7391 0918 (PM me if you add me), PSN - MaxwellGT2000, XBL - BlkKniteCecil, MaxwellGT2000

Well, for starters you don't have to use the method heading of private "int" and then have a return null; line.

Make that method like this:

private static void setNumber(){

}

Then, in the IF statements, you can't use commas to separate things like that. Really the best way to do this would be a loop and a switch. The current way you have it setup, it is taking the entire String "word" and comparing it to just one letter. You need to setup a loop structure like the following:

for(int i = 0; i<7; i++){
if(word.charAt(i) == 'A' || word.charAt(i) == 'B' || word.charAt(i) == 'C' .... etc.){

//You need one of each option if you are using if statements, separated by two vertical bars (the shift+black-slash). That's why a switch would be
//easier but this is just to show you how to alter your code the way you have it started.
//Also, since this is comparing a char and not a string, your == A has to be put in apostrophes: 'A'

number = number + "2";
}
else if(word.charAt(i) == 'E' || ....){

number = number + "3";
}
}

That loop checks every character up to the 7th one and compares it to each possible character.  All you would need to do is create the additional IF statements to check for the other letter/number combinations.


I hope this isn't using Java that you haven't learned yet. If this is too advanced for the stage of your class you are in, let me know and I'll try to think of a more basic way to do this.



Around the Network

Oh and if you are going to be running this class without a driver class (another class that creates instances of Phone and runs the program), you will need a main method at the top:

public static void main(String[] args){
Phone test1 = new Phone("Flowers");
test1.setNumber();
System.out.println(test1.number); //may have to change the String number variable to public rather than private.
//This is just to test and make sure it got the correct result by printing it to the console.

}

Oh and if you need a very good Java development environment, download Eclipse:
http://www.eclipse.org/downloads/download.php?file=/eclipse/downloads/drops/R-3.5.1-200909170800/eclipse-SDK-3.5.1-win32.zip



I know enough Java to know I hate the language

Anyways, you don't need your giant IF section if you simplify. If you can convert your Word into an array of characters, then you can do hex manipulation and convert back into a string. (look up a ascii table if you really need to figure out how this works)

You then need to loop through this for each index in the phone number.

//char is the current index in the phone number
if(char >= 0x61) //adjust for lowercase letters
{
char -= 0x20;
}
char -= 0x40;
if(char >= 0x13) //key 7 has 4 characters, so we have to shift everything down 1
{
char -= 1;
}
char = (char-1)/3 + 2; //make sure this is integer math or things will get messed up
if(char == 10) //z is on the 9th key, and the only 1 that would show up on 10.
{
char -= 1;
}
number += char.ToString();

this could be simplified down a tiny bit more but isn't really needed. Also note that I wrote this as a C# based code and I wouldn't know the exact syntax for Java.

anyways, your IF statments are broken now anyways, so might as well replace them. Your current setup will cause you to check 1 number only. You really need a for loop to index through all the characters in the phone number.




If you drop a PS3 right on top of a Wii, it would definitely defeat it. Not so sure about the Xbox360. - mancandy
In the past we played games. In the future we watch games. - Forest-Spirit
11/03/09 Desposit: Mod Bribery (RolStoppable)  vg$ 500.00
06/03/09 Purchase: Moderator Privilege  vg$ -50,000.00

Nordlead Jr. Photo/Video Gallery!!! (Video Added 4/19/10)

^I beat you to it, and with actual Java!



nightsurge said:
^I beat you to it, and with actual Java!

mine is more efficient and less lines of code, yours uses tons of if statements. Mine also works with Q and Z even though they aren't used in this program, and a simple "if" statement could be inserted to skip them during the for loop.

Obviously I left out a bit of code for looping, max numbers, adding a '-' and other minor details.




If you drop a PS3 right on top of a Wii, it would definitely defeat it. Not so sure about the Xbox360. - mancandy
In the past we played games. In the future we watch games. - Forest-Spirit
11/03/09 Desposit: Mod Bribery (RolStoppable)  vg$ 500.00
06/03/09 Purchase: Moderator Privilege  vg$ -50,000.00

Nordlead Jr. Photo/Video Gallery!!! (Video Added 4/19/10)

I'm not sure why you are getting your exception though. You should get a line number for the exception and look around there. 30 minutes isn't enough time to fix this unless you really know what you are doing.




If you drop a PS3 right on top of a Wii, it would definitely defeat it. Not so sure about the Xbox360. - mancandy
In the past we played games. In the future we watch games. - Forest-Spirit
11/03/09 Desposit: Mod Bribery (RolStoppable)  vg$ 500.00
06/03/09 Purchase: Moderator Privilege  vg$ -50,000.00

Nordlead Jr. Photo/Video Gallery!!! (Video Added 4/19/10)