File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Very New To Java Question Help Please! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Very New To Java Question Help Please!" Watch "Very New To Java Question Help Please!" New topic
Author

Very New To Java Question Help Please!

jake apperley
Greenhorn

Joined: May 03, 2009
Posts: 7
Hey guys I am very new to Java and I dont even understand why it's a module on a WEB DESIGN course at University! Frustrates me why we have to learn it because I dont get it. Anyway I need some help with some assignment questions using the switch statement. I have done the switch statement part but just cant do the first bit.

Here is the question

This is a main/driver class.
Use a switch statement to pick a country by entering the correct character.
Allow the user to enter upper or lower case.

If the country is not on the list the program should output the exact string including a newline on the end:
That country is not on the list

Here is the country list and the example given

A Andorra
B Belgium
F Finland
M Malta
S Slovenia

Choose a country from menu above:
b - user types in
You chose "Belgium" - output


This isnt a hey do my homework question, its please can you help/explain how to do things and why we do certain things. I understand I have to use the charAt to pick up the character the user types in I think but just dont know how to implement it into the code.

Regards
Jake
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

I dont even understand why it's a module on a WEB DESIGN course at University!


Servlets and JSPs are very common ways to generate web pages -- and these technologies are based in Java.

Although less common, Applets are based on Java too.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

I have done the switch statement part but just cant do the first bit.


BTW, it would help us if you showed us what part you did. And elaborate what "first bit" you don't understand.

Henry
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2503
    
    8

Hello Jake welcome to the Ranch.

The switch statement takes in a int-compatible variable. So since you are using A-Z then characters can be that variable. After the switch there are "case" statements. Each scenario is a case. So A is a case, B is another case etc.

Now it depends on what kind of program your assignment is doing to decide how to determine those cases. Eg GUI program can take in integer or character for the switch statement; console program can take in character only (which is int-compatible).


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
jake apperley
Greenhorn

Joined: May 03, 2009
Posts: 7
Ok Henry, I was just abit embarrassed to post code as I really am poor at java lol

public static void main(String[] args) {


int location


Scanner scan = new Scanner (System.in);

System.out.println ("Choose a country from menu above:");
System.out.print ("You Chose ");

switch (location)
{
case A:
System.out.println(""Andorra"");
break;
case B:
System.out.println(""Belgium"");
break;
case F:
System.out.println(""Finland"");
break;
case M:
System.out.println(""Malta"");
break;
case S:
System.out.println(""Slovenia"");
break;
default:
System.out.println("That country is not on the list");

This is what I have done so far but am confused about getting the characters etc picked up
jake apperley
Greenhorn

Joined: May 03, 2009
Posts: 7
Also thanks for the warm welcome guys, I am a member of a few other poker forums and a loyaler at some. However I joined the sun java forum and they were like im not doing your homework do it yourself and were so unhelpful!

So thanks guys for actually trying to help me
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2503
    
    8

jake apperley wrote:This is what I have done so far but am confused about getting the characters etc picked up


Since you are using Scanner try inserting scan.next() like this:


The scan.next() is a String then the charAt(0) makes the variable a character (first character of the string input). Oh do watch out for upper and lower case in those case statements.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40


Take a look at the java.util.Scanner class -- particularly the hasNextXXX() ans nextXXX() methods. These methods will help get the user input for your location reference, which you can use in the switch.

Also, take a look at the "case" statements. Character literals need to be quoted (in single quotes).

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

However I joined the sun java forum and they were like im not doing your homework do it yourself and were so unhelpful!


Well, we may be a bit nicer here, but we won't do your homework either. Remember that homework is for learning -- you don't learn if you don't do it yourself.

Henry
jake apperley
Greenhorn

Joined: May 03, 2009
Posts: 7
I am getting can't find symbol on all of the case lines for some reason.

Also how do I get it so the user is able to enter both upper and lower case letters?
jake apperley
Greenhorn

Joined: May 03, 2009
Posts: 7
Henry Wong wrote:
However I joined the sun java forum and they were like im not doing your homework do it yourself and were so unhelpful!


Well, we may be a bit nicer here, but we won't do your homework either. Remember that homework is for learning -- you don't learn if you don't do it yourself.

Henry


yeah I know this but I learn better from people explaining than reading tbh. It doesnt help when I ask our lecturer for help and he says he doesnt know because he specialises in C++ which is absolutely ridiculous!!

I will have a look at the links thanks.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

I am getting can't find symbol on all of the case lines for some reason.


See my previous posts that mentions the "case" statements.

Henry
jake apperley
Greenhorn

Joined: May 03, 2009
Posts: 7
This is my code now, but is this correct for the question and how do I let the user enter both upper and lower case?

public static void main(String[] args) {

Scanner scan = new Scanner (System.in);
System.out.println ("Choose a country from menu above:");
System.out.print ("You Chose");
char location = scan.next().charAt(0);

switch (location)
{
case 'a':
System.out.println("Andorra");
break;
case 'b':
System.out.println("Belgium");
break;
case 'f':
System.out.println("Finland");
break;
case 'm':
System.out.println("Malta"");
break;
case 's':
System.out.println("Slovenia");
break;
default:
System.out.println("That country is not on the list");
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40


how do I let the user enter both upper and lower case?


Well, you have a few options. You can convert all the input to one case -- see the toLowerCase() or toUpperCase() methods of the java.lang.Character class. Or...

You can provide multiple case targets. If you don't use break, the code will fall through to the next case, like so...



Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

yeah I know this but I learn better from people explaining than reading


Unfortunately, "learning better" may not be better, as you are learning for the homework only. When you read...

1. You learn more than you need. Not just the stuff for you homework but the rest of the class or method options too.

2. You also need to learn how to apply the class or method, because you need to understand what is needed for what.

If you just get help for you specific example. You don't get either of this... The examples given to you already has what you need. You don't learn anything else, or even how it was applicable.

Henry
jake apperley
Greenhorn

Joined: May 03, 2009
Posts: 7
Ok cool thats awesome.
Why do I get an error message saying "unclosed string literal" on the bold line in my previous post?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Unclosed string literal usually means you haven't got the right number of " quotes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Very New To Java Question Help Please!