[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 790
  • Last Modified:

Self Taught JAVA

I am using a textbook and trying to go through it to learn JAVA programming. I have a program with the following code and I really can't figure out where I went wrong.
/*
Chapter 4:      Understanding Data Types
Programmer:      Robert Ehinger
Date:            March 28, 2010
Filename:      MyType.java
Purpose:       This projects helps beginning programmers understand data types.

*/
import java.io.*;
import javax.swing.JOptionPane;

public class MyType
{
      public static void main(String[] args)
      {
            // declare and construct variables
            String strChoice, strTryString, strTryInt, strTryDouble;
            int choice, tryInt;
            double tryDouble;
            boolean done = false;
            //loop until cancel button is clicked

            while(!done)
            try
            {
                  choice = Integer.parseInt(strChoice);

                              // print prompts and get user input
                              System.out.println("\tWhat's my string type?");
                              System.out.println();
                              System.out.println();
                              System.out.println("\t1) String");
                              System.out.println("\t2) integer");
                              System.out.println("\t3) double");
                              System.out.println("\tQuit the program");

}

                              switch(choice)
                              {
                                    case 1:
                                          System.out.println("You are correct. Any input can be saved as a string.");
                                          break;

                                    case 2:
                                          choice = Integer.parseInt(tryInt);
                                          System.out.println("You are correct");
                                          break;

                                    case 3:
                                          choice = Integer.parseInt(tryDouble);
                                          System.out.println("You are correct");
                                          break;

                                    case 4:
                                    if      (done == true) finish();
                                          System.out.println("Goodbye!!");
                                          break;
                                    if (choice<1 || choice>4) throw new NumberFormatException();


                              catch(NumberFormatException e);
                              {
                              System.out.println("Invalid choice! Try again!");

}
}


      //The finish() method ends the program.
      public static void finish()
      {
            System.exit(0);
      }
}}

Here are the errors.

F:\CINS136\MyType.java:24: 'try' without 'catch' or 'finally'
            try
                ^
F:\CINS136\MyType.java:62: 'catch' without 'try'
                              catch(NumberFormatException e);
                                        ^
F:\CINS136\MyType.java:71: illegal start of expression
      public static void finish()
        ^
F:\CINS136\MyType.java:75: ';' expected
}}
^
4 errors

Tool completed with exit code 1

Please advise.

Thank you!!

Robert
0
Robert Ehinger
Asked:
Robert Ehinger
4 Solutions
 
VenabiliCommented:
The first two are connected - all you need is to close the try block before the catch. So replace
 catch(NumberFormatException e);
with
} catch(NumberFormatException e);

And you need to remove } just before the switch -- it is closing the try while you want to close it at the catch. This will also solve issue #3 (you have brackets in the wrong places.

And remove one of the } at the end of the code.

And I would use {} for the body of the while. :)
0
 
VenabiliCommented:
A few more things:
catch(NumberFormatException e);  -> remove the ;

choice = Integer.parseInt(tryInt); -> both are ints so you just need to do
choice = tryInt;

choice = Integer.parseInt(tryDouble); is a bit more tricky as tryDouble is double. So how do you plan on converiting this?

And why at all are you trying to do this inside? What are you trying to do with the change of choice inside?

strChoice is not inicialized...

Ok - what are you trying to do - let's try to fix this whole thing :)
0
 
VenabiliCommented:
Here are a few pointers for you on what needs fixing:
- Where do you read the answer of the user?
- I think I see now what you do with the choice but that's a bad idea-   you should use other variables and not the one that controls the switch. :)
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Robert EhingerIT specialistAuthor Commented:
The assignment is to write a program that helps beginning JAVA programmers understand data types. They should have 4 choices -1) String, 2) integer, 3) double and 4) Quit the program. The book gives the variables to use and assigns using a while(!done) loop to repeat as long as the user does not hit the cancel button. The assignment also says to use the switch(choice) and case statements and to give the user feedback depending on the choice. The choice=Integer.parseInt(strChoice) is listed in the assignment.
0
 
VenabiliCommented:
>The choice=Integer.parseInt(strChoice) is listed in the assignment.

OK. But where do you initialize strChoice (you need to somehow read what the user tells you so you can evaluate it:) Additionally - you want this to happen more than once - so this reading and then parsing should be inside of the cycle.

One more note:
if      (done == true) finish();
                                          System.out.println("Goodbye!!");
                                          break;
Where do you set done? Basically this will never happen with this code.

And you need to close the switch as well.

So do you want to start working through my notes above and we will start fixing the issues one by one?
0
 
SunScreenCertCommented:
I would say that the best thing is to first understand the language..what does it expect, like if you know java language(read head first java), then you need not any help for these errors..you will automatically understand what these errors are and how could you fix them...if you do such a way, you will end up becoming better and making no such errors in the first place..
0
 
VenabiliCommented:
That's true but sometimes you just need to start learning with the first programs you need to fix - and a lot of things start making sense only after you see them :) Reading a book won't make you a god developer (and if someone will read anything, the Sun tutorial is the better way to start than any book) and depending on how someone is used to learning languages and how much they know about programming generally,  it may not stop them from doing the same errors. :)

RobertEhinger,

How is your coding going?
0
 
Robert EhingerIT specialistAuthor Commented:
I am still stuck. There must be something simple I am missing. I really don't think reading another book is the answer. I tried the changes you mentioned and still no go. I will send the results and errors tomorrow after a good night's sleep.
0
 
Robert EhingerIT specialistAuthor Commented:
Here is the current code with the error codes -
import java.io.*;
import javax.swing.JOptionPane;

public class MyType
{
      public static void main(String[] args)
      {
            // declare and construct variables
            String strChoice, strTryString, strTryInt, strTryDouble;
            int choice, tryInt;
            double tryDouble;
            boolean done = false;
            //loop until cancel button is clicked

            while(!done)
            {
            try
            {
                  choice = Integer.parseInt(strChoice);

                              // print prompts and get user input
                              System.out.println("\tWhat's my string type?");
                              System.out.println();
                              System.out.println();
                              System.out.println("\t1) String");
                              System.out.println("\t2) integer");
                              System.out.println("\t3) double");
                              System.out.println("\tQuit the program");



                              switch(choice)
                              {
                                    case 1:
                                          System.out.println("You are correct. Any input can be saved as a string.");
                                          break;

                                    case 2:
                                          choice = tryInt;
                                          System.out.println("You are correct");
                                          break;

                                    case 3:
                                          choice = Integer.parseInt(tryDouble);
                                          System.out.println("You are correct");
                                          break;

                                    case 4:
                                    if      (done == true) finish();
                                          System.out.println("Goodbye!!");
                                          break;
                                    if (choice<1 || choice>4) throw new NumberFormatException();


                              }catch(NumberFormatException e)

                              {
                  }
                              System.out.println("Invalid choice! Try again!");

}
}


      //The finish() method ends the program.
      public static void finish()
      {
            System.exit(0);
      }
}


F:\CINS136\MyType.java:63: 'catch' without 'try'
                              }catch(NumberFormatException e)
                                         ^
F:\CINS136\MyType.java:25: 'try' without 'catch' or 'finally'
            try
                ^
F:\CINS136\MyType.java:74: illegal start of expression
      public static void finish()
        ^
F:\CINS136\MyType.java:78: ';' expected
}
^
F:\CINS136\MyType.java:78: '}' expected
}
 ^
5 errors

Tool completed with exit code 1
0
 
VenabiliCommented:
You still had not implemented any of my fixes from above. So again:

- Where is the code which reads strChoice?
- Fix the brackets - you should close the switch before the catch - move of of the brackets from after the catch to before it.
- Where do you change the value of done?

I suspect that
if      (done == true) finish();
 System.out.println("Goodbye!!");

Should be
if (done == true){
System.out.println("Goodbye!!");
finish();
}
Or something differen such as
done = true;
System.out.println("Goodbye!!");
finish();

But then you do not need the finish as this will exit from the code anyway as it will exit from the done.
0
 
Robert EhingerIT specialistAuthor Commented:
"- Where is the code which reads strChoice?"

I thought it was "choice = Integer.parseInt(strChoice);"

I am evidently not understanding what you are telling me. I am down to just 2 errors but still not a viable program. The errors are "F:\CINS136\MyType.java:25: 'try' without 'catch' or 'finally'
            try
                ^
F:\CINS136\MyType.java:65: 'catch' without 'try'
                              catch(NumberFormatException e)
                                        ^
2 errors

Tool completed with exit code 1"

*/
import java.io.*;
import javax.swing.JOptionPane;

public class MyType
{
      public static void main(String[] args)
      {
            // declare and construct variables
            String strChoice, strTryString, strTryInt, strTryDouble;
            int choice, tryInt;
            double tryDouble;
            boolean done = false;
            //loop until cancel button is clicked

            while(!done)
            {
            try
            {
                  choice = Integer.parseInt(strChoice);

                              // print prompts and get user input
                              System.out.println("\tWhat's my string type?");
                              System.out.println();
                              System.out.println();
                              System.out.println("\t1) String");
                              System.out.println("\t2) integer");
                              System.out.println("\t3) double");
                              System.out.println("\tQuit the program");

            }

                              switch(choice)
                              {
                                    case 1:
                                          System.out.println("You are correct. Any input can be saved as a string.");
                                          break;

                                    case 2:
                                          choice = tryInt;
                                          System.out.println("You are correct");
                                          break;

                                    case 3:
                                          choice = Integer.parseInt(tryDouble);
                                          System.out.println("You are correct");
                                          break;

                                    case 4:
                                    if      (done == true) finish();
                                          System.out.println("Goodbye!!");
                                          break;
                                    if (choice<1 || choice>4) throw new NumberFormatException();
                              }
                  }


                              catch(NumberFormatException e)
                              {

                              System.out.println("Invalid choice! Try again!");


                              }

      //The finish() method ends the program.
      public static void finish()
      {
            System.exit(0);
      }
      }
}
0
 
Robert EhingerIT specialistAuthor Commented:
I have been working more with this code and here is what I have now -

/*
Chapter 4:      Understanding Data Types
Programmer:      Robert Ehinger
Date:            March 28, 2010
Filename:      MyType.java
Purpose:       This projects helps beginning programmers understand data types.

*/
import java.io.*;
import javax.swing.JOptionPane;

public class MyType
{
      public static void main(String[] args)
      {
            // declare and construct variables
            String strChoice, strTryString, strTryInt, strTryDouble;
            int choice, tryInt;
            double tryDouble;
            boolean done = false;
            //loop until cancel button is clicked

            while(!done)
            {
            try{

            }
                                    catch(NumberFormatException e)
                              {
                              }
                  choice = Integer.parseInt(strChoice);

                              // print prompts and get user input
                              System.out.println("\tWhat's my string type?");
                              System.out.println();
                              System.out.println();
                              System.out.println("\t1) String");
                              System.out.println("\t2) integer");
                              System.out.println("\t3) double");
                              System.out.println("\tQuit the program");

            }

                              switch(choice)
                              {
                                    case 1:
                                          System.out.println("You are correct. Any input can be saved as a string.");
                                          break;

                                    case 2:
                                          choice = tryInt;
                                          System.out.println("You are correct");
                                          break;

                                    case 3:
                                          choice = Integer.parseInt(tryDouble);
                                          System.out.println("You are correct");
                                          break;

                                    case 4:
                                    if      (done == true) finish();
                                          System.out.println("Goodbye!!");
                                          break;
                                    if (choice<1 || choice>4) throw new NumberFormatException();
                              }


                              System.out.println("Invalid choice! Try again!");


                              }

      //The finish() method ends the program.
      public static void finish()
      {
            System.exit(0);
      }
      }

The erroe is -

F:\CINS136\MyType.java:56: cannot find symbol
symbol  : method parseInt(double)
location: class java.lang.Integer
                                          choice = Integer.parseInt(tryDouble);
                                                                        ^
1 error

Tool completed with exit code 1
0
 
Chandramouli kArchitectCommented:
am also learing Java and just managed to compile this by adding few more stuffs. I understand this code is just to follow instruction given in textbook but this code can be improved by many ways as suggested by other experts.

import java.io.*;
import javax.swing.JOptionPane;

public class MyType
{
      public static void main(String[] args)
      {
            // declare and construct variables
            String strChoice = null, strTryString, strTryInt, strTryDouble;
            int choice = 0, tryInt = 0;
            double tryDouble = 0;
            boolean done = false;
            //loop until cancel button is clicked
            while(!done){
                  try{

                  }
                  catch(NumberFormatException e){
                  }
                choice = Integer.parseInt(strChoice);
                // print prompts and get user input
                    System.out.println("\tWhat's my string type?");
                    System.out.println();
                    System.out.println();
                    System.out.println("\t1) String");
                    System.out.println("\t2) integer");
                    System.out.println("\t3) double");
                    System.out.println("\t4) Quit the program");

            }
            switch(choice)
            {
                                    case 1:
                                          System.out.println("You are correct. Any input can be saved as a string.");
                                          break;

                                    case 2:
                                          choice = tryInt;
                                          System.out.println("You are correct");
                                          break;

                                    case 3:
                                          choice = (int)tryDouble;
                                          System.out.println("You are correct");
                                          break;

                                    case 4:
                                    if (done == true) {
                                          finish();
                                          System.out.println("Goodbye!!");
                                        break;
                                    }                                        
                                    if (choice<1 || choice>4){
                                    }                                    
                                                
                              }


                              System.out.println("Invalid choice! Try again!");


                              }

      //The finish() method ends the program.
      public static void finish()
      {
            System.exit(0);
      }
      }
0
 
Robert EhingerIT specialistAuthor Commented:
Thank you, I will check it out.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now