Trouble with the driver and classes that work with the driver.  
Author Message
Teresa





PostPosted: 10/1/2007 7:32:00 AM Top

java-programmer, Trouble with the driver and classes that work with the driver. this first part is the driver. need help with getting them both to work
with each other.

import java.util.Scanner;

public class AbbreviationsandStates

{

public static void main(String[] args)

{

String abbreIn;

String abbreOut;

String theResult;



EnterState es = new EnterState();


System.out.println("Enter Abbreviation the for the State : ");

Scanner in = new Scanner(System.in); //Instantiate Scanner

abbreIn = in.nextLine();


abbreOut = es.getAbbre(abbreIn);

theResult = es.getResult(abbreIn, abbreOut);


System.out.println(theResult);



}


}

this part is the class not sure what I am doing wrong but I do know that I
can not have an int for a string in the driver.

public class EnterState

{

public String getAbbre(String userInput)

{

userInput = userInput;


return userInput;

}

public int getResults(int abbre1, int abbre2)

{

String theResult;


switch (abbre1)

{

case 'A': case 'a':

switch (abbre2)

{

case 'L': case 'l':

System.out.print("Alabama");

break;

case 'K': case 'k':

System.out.print("Alaska");

break;

case 'Z': case 'z':

System.out.print("Arizona");

break;

case 'R': case 'r':

System.out.print("Arkansas");

break;

default:

System.out.print("Invalid");

break;

}

break;

case 'C': case 'c':

switch (abbre2)

{

case 'A': case 'a':

System.out.print("California");

break;

case 'O': case 'o':

System.out.print("Colorado");

break;

case 'T': case 't':

System.out.print("Connecticut");

break;

default:

System.out.print("Invalid");

break;

}

break;

case 'D': case 'd':

switch (abbre2)

{

case 'E': case 'e':

System.out.print("Delaware");

break;

default:

System.out.print("Invalid");

break;

}

break;

case 'F': case 'f':

switch (abbre2)

{

case 'L': case 'l':

System.out.print("Florida");

break;

default:

System.out.print("Invalid");

break;

}

break;

case 'G': case 'g':

switch (abbre2)

{

case 'A': case 'a':

System.out.print("Georgia");

break;

default:

System.out.print("Invalid");

break;

}

break;

case 'H': case 'h':

switch (abbre2)

{

case 'I': case 'i':

System.out.print("Hawaii");

break;

default:

System.out.print("Invalid");

break;

}

break;

case 'I': case 'i':

switch (abbre2)

{

case 'D': case'd':

System.out.print("Idaho");

break;

case 'L': case 'l':

System.out.print("Illinois");

break;

case 'N': case 'n':

System.out.print("Indiana");

break;

case 'A': case 'a':

System.out.print("Iowa");

break;

default:

System.out.print("Invalid");

break;

}

break;

case 'K': case 'k':

switch (abbre2)

{

case 'S': case 's':

System.out.print("Kansas");

break;

case 'Y': case 'y':

System.out.print("Kentucky");

break;

default:

System.out.print("Invalid");

break;

}

break;

case 'L': case 'l':

switch (abbre2)

{

case 'A': case 'a':

System.out.print("Louisiana");

break;

default:

System.out.print("Invalid");

break;

}

break;

case 'M':

switch (abbre2)

{

case 'E':

System.out.print("Maine");

break;

case 'D':

System.out.print("Maryland");

break;

case 'A':

System.out.print("Massachusetts");

break;

case 'I':

System.out.print("Michigan");

break;

case 'N':

System.out.print("Minnesota");

break;

case 'S':

System.out.print("Mississippi");

break;

case 'O':

System.out.print("Missouri");

break;

case 'T':

System.out.print("Montana");

break;

default:

System.out.print("Invalid");

break;

}

break;

case 'N':

switch (abbre2)

{

case 'E':

System.out.print ("Nebraska");

break;

case 'V':

System.out.print("Nevada");

break;

case 'H':

System.out.print("New Hampshire");

break;

case 'J':

System.out.print("New Jersey");

break;

case 'M':

System.out.print("New Mexico");

break;

case 'Y':

System.out.print("New York");

break;

case 'C':

System.out.print("North Carolina");

break;

case 'D':

System.out.print("North Dakota");

break;

default:

System.out.print("Invalid");

break;

}

break;

case 'O':

switch (abbre2)

{

case 'H':

System.out.print("Ohio");

break;

case 'K':

System.out.print("Oklahoma");

break;

case 'R':

System.out.print("Oregon");

break;

default:

System.out.print("Invalid");

break;

}

break;

case 'P':

switch (abbre2)

{

case 'A':

System.out.print("Pennsylvania");

break;

default:

System.out.print("Invalid");

break;

}

break;

case 'R':

switch (abbre2)

{

case 'I':

System.out.print("Rhode Island");

break;

default:

System.out.print("Invalid");

break;

}

break;

case 'S':

switch (abbre2)

{

case 'C':

System.out.print("South Carolina");

break;

case 'D':

System.out.print("South Dakota");

break;

default:

System.out.print("Invalid");

break;

}

break;

case 'T':

switch (abbre2)

{

case 'N':

System.out.print("Tennessee");

break;

case 'X':

System.out.print("Texas");

break;

default:

System.out.print("Invalid");

break;

}

break;

case 'U':

switch (abbre2)

{

case 'T':

System.out.print("Utah");

break;

default:

System.out.print("Invalid");

break;

}

break;

case 'V':

switch (abbre2)

{

case 'T':

System.out.print("Vermont");

break;

case 'A':

System.out.print("Virginia");

break;

default:

System.out.print("Invalid");

break;

}

break;

case 'W':

switch (abbre2)

{

case 'A':

System.out.print("Washington");

break;

case 'V':

System.out.print("West Virginia");

break;

case 'I':

System.out.print("Wisconsin");

break;

case 'Y':

System.out.print("Wyoming");

break;

default:

System.out.print("Invalid");

break;

}

break;

default:

System.out.print("Invalid");

break;

}



theResult = abbre1 + abbre2 + " The State is" ;


return theResult;

}

}


 
Joshua Cranmer





PostPosted: 10/1/2007 7:49:00 AM Top

java-programmer >> Trouble with the driver and classes that work with the driver. Teresa wrote:
> this first part is the driver. need help with getting them both to work
> with each other.
>
> [ Snip class 1 ]
>
> public class EnterState {
> public String getAbbre(String userInput)

Change this line to:

public static String getAbbre(String userInput)

and it will work. See http://java.sun.com/docs/books/tutorial/index.html
for more information.

> public int getResults(int abbre1, int abbre2) {
> String theResult;
> switch (abbre1) {
> case 'A': case 'a':
> switch (abbre2) {
> case 'L': case 'l':
> System.out.print("Alabama");
> break;
> [ snip everything else ]

[ NOTE: code reformatted to save screen space ]
Several nits:
1. Your second method does not seem to do quite what you want it to.
Characters should be passed as chars, not ints. Strings should be
returned as a String, not an int.
2. Try to be more descriptive than "they don't work."
3. Massive switch statements are not very ideal. Although the best was
would be to use a HashMap, since I assume that you are writing this as
part of an introductory class, that would probably be a bit beyond you
right now. At the very least, it is clearer to use code along the lines of:

// String abbreviation is passed in
abbreviation = abbreviation.toUpperCase();
if (abbreviation.equals("AL"))
return "Alabama";
else if (abbreviation.equals("AK"))
return "Alaska";
// etc.


--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
 
Teresa





PostPosted: 10/1/2007 7:57:00 AM Top

java-programmer >> Trouble with the driver and classes that work with the driver. Thanks for the help I will work on it tomorrow and let you know it works. I
was thinking of an hashtable but I can't use it.
"Joshua Cranmer" <email***@***.com> wrote in message
news:bCWLi.17628$Im1.10357@trnddc01...
> Teresa wrote:
> > this first part is the driver. need help with getting them both to work
> > with each other.
> >
> > [ Snip class 1 ]
> >
> > public class EnterState {
> > public String getAbbre(String userInput)
>
> Change this line to:
>
> public static String getAbbre(String userInput)
>
> and it will work. See http://java.sun.com/docs/books/tutorial/index.html
> for more information.
>
> > public int getResults(int abbre1, int abbre2) {
> > String theResult;
> > switch (abbre1) {
> > case 'A': case 'a':
> > switch (abbre2) {
> > case 'L': case 'l':
> > System.out.print("Alabama");
> > break;
> > [ snip everything else ]
>
> [ NOTE: code reformatted to save screen space ]
> Several nits:
> 1. Your second method does not seem to do quite what you want it to.
> Characters should be passed as chars, not ints. Strings should be
> returned as a String, not an int.
> 2. Try to be more descriptive than "they don't work."
> 3. Massive switch statements are not very ideal. Although the best was
> would be to use a HashMap, since I assume that you are writing this as
> part of an introductory class, that would probably be a bit beyond you
> right now. At the very least, it is clearer to use code along the lines
of:
>
> // String abbreviation is passed in
> abbreviation = abbreviation.toUpperCase();
> if (abbreviation.equals("AL"))
> return "Alabama";
> else if (abbreviation.equals("AK"))
> return "Alaska";
> // etc.
>
>
> --
> Beware of bugs in the above code; I have only proved it correct, not
> tried it. -- Donald E. Knuth


 
 
Roedy Green





PostPosted: 10/1/2007 11:42:00 AM Top

java-programmer >> Trouble with the driver and classes that work with the driver. On Sun, 30 Sep 2007 23:57:09 GMT, "Teresa" <email***@***.com> wrote,
quoted or indirectly quoted someone who said :

> I
>was thinking of an hashtable but I can't use it.

see http://mindprod.com/jgloss/hashtable.html
http://mindprod.com/jgloss/hashmap.html

There is sample code, and lots of explanation of how they work inside.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com