Beginner to the Java Lanuage

Joined
Jun 18, 2010
Messages
6
Reaction score
0
Hey guys my name is Tomas and i have just started programing in Java (currently studying Computer Studies at RMIT) and i need some help..

I have to create a console application with a list of options i have coded it as follows

Code:
 public static void main(String[] args) {
        // TODO code application logic here
        Scanner scanner = new Scanner(System.in);
        int selection;
        while(true){
            //Display the menu
            System.out.println("Make a selection");
            System.out.println("[1] Summery report for a sales employee");
            System.out.println("[2] Print a pattern");
            System.out.println("[3] Exit");
            System.out.println(" Select option: ");
           selection = scanner.nextInt();
           
           switch (selection){
               case 1:System.out.println("Loading report");
               break;
               case 2:System.out.println("Loading pattern");
               break;
               case 3:System.out.println("Exit Successful");
               System.exit(0);
               defult:System.out.println("Please enter a vaild selection:");
         }//Switch
      } //While
   } //Main Static

now all that works. But the last part does not show when the user places in the wrong number but it loads the menu again (which is what i want but i also want it to display defult).

The other issue i am having with this code is when the user inputs a letter (A-Z, a-z) the following error.

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:857)
at java.util.Scanner.next(Scanner.java:1478)
at java.util.Scanner.nextInt(Scanner.java:2108)
at java.util.Scanner.nextInt(Scanner.java:2067)
at assignmentone.Main.main(Main.java:22)
Java Result: 1

please help. I would much appreciate it.
 
Joined
Jun 14, 2010
Messages
10
Reaction score
0
you need to do Integer.parse and catch exception if it is not an integer
java.sun.com/j2se/1.5.0/docs/api/java/lang/Integer.html#parseInt%28java.lang.String%29
 
Joined
Jun 14, 2010
Messages
10
Reaction score
0
you may consider using string arg as parameters, e.g.,

myprogram print
myprogram symmary

... etc.
then you don't need to parse anything
 
Joined
Jun 18, 2010
Messages
6
Reaction score
0
Thanks

Thanks guys for the reply i have it all working now. But i still have another question.

I have to print a table (in console) to show information.

i tryed to do it with System.out.println("|_|"); but it will not keep the same size when the information is put inside the table.

Is there another way to draw the table

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top