Robert Ehinger
asked on
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("Goodby e!!");
break;
if (choice<1 || choice>4) throw new NumberFormatException();
catch(NumberFormatExceptio n e);
{
System.out.println("Invali d 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(NumberFormatExceptio n 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
/*
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
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
}
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("Goodby
break;
if (choice<1 || choice>4) throw new NumberFormatException();
catch(NumberFormatExceptio
{
System.out.println("Invali
}
}
//The finish() method ends the program.
public static void finish()
{
System.exit(0);
}
}}
Here are the errors.
F:\CINS136\MyType.java:24:
try
^
F:\CINS136\MyType.java:62:
catch(NumberFormatExceptio
^
F:\CINS136\MyType.java:71:
public static void finish()
^
F:\CINS136\MyType.java:75:
}}
^
4 errors
Tool completed with exit code 1
Please advise.
Thank you!!
Robert
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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(st rChoice) is listed in the assignment.
>The choice=Integer.parseInt(st rChoice) 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("Goodby e!!");
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?
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("Goodby
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?
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..
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?
RobertEhinger,
How is your coding going?
ASKER
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.
ASKER
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("Goodby e!!");
break;
if (choice<1 || choice>4) throw new NumberFormatException();
}catch(NumberFormatExcepti on e)
{
}
System.out.println("Invali d 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(NumberFormatExcepti on 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
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
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
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("Goodby
break;
if (choice<1 || choice>4) throw new NumberFormatException();
}catch(NumberFormatExcepti
{
}
System.out.println("Invali
}
}
//The finish() method ends the program.
public static void finish()
{
System.exit(0);
}
}
F:\CINS136\MyType.java:63:
}catch(NumberFormatExcepti
^
F:\CINS136\MyType.java:25:
try
^
F:\CINS136\MyType.java:74:
public static void finish()
^
F:\CINS136\MyType.java:78:
}
^
F:\CINS136\MyType.java:78:
}
^
5 errors
Tool completed with exit code 1
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
"- 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(NumberFormatExceptio n 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("Goodby e!!");
break;
if (choice<1 || choice>4) throw new NumberFormatException();
}
}
catch(NumberFormatExceptio n e)
{
System.out.println("Invali d choice! Try again!");
}
//The finish() method ends the program.
public static void finish()
{
System.exit(0);
}
}
}
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
^
F:\CINS136\MyType.java:65:
catch(NumberFormatExceptio
^
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
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
}
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("Goodby
break;
if (choice<1 || choice>4) throw new NumberFormatException();
}
}
catch(NumberFormatExceptio
{
System.out.println("Invali
}
//The finish() method ends the program.
public static void finish()
{
System.exit(0);
}
}
}
ASKER
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(NumberFormatExceptio n 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("Goodby e!!");
break;
if (choice<1 || choice>4) throw new NumberFormatException();
}
System.out.println("Invali d 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
/*
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(NumberFormatExceptio
{
}
choice = Integer.parseInt(strChoice
// print prompts and get user input
System.out.println("\tWhat
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
}
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("Goodby
break;
if (choice<1 || choice>4) throw new NumberFormatException();
}
System.out.println("Invali
}
//The finish() method ends the program.
public static void finish()
{
System.exit(0);
}
}
The erroe is -
F:\CINS136\MyType.java:56:
symbol : method parseInt(double)
location: class java.lang.Integer
choice = Integer.parseInt(tryDouble
^
1 error
Tool completed with exit code 1
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you, I will check it out.
- 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. :)