-
Vector and switch problems
Hello, this is my program so far for making a zoo of animals. I've been thinking about this and staring at the screen for so long already. My comp has been weird with Textpad lately. The Zoo() method gets "class or interface expected" error when compling, but it didn't before and now it does. Am I thinking wrong with the coding for trying to display the all the breeds of animals and youngest animal from the inputted animals. Is using the math.min the wrong way to go and am I trying to access the wrong string from the vector animals? I'm just so tired of being close, but not done with this. If someone can help to explain what I am programming wrong, it would benefit me, so I can understand what I can't figure out. Also, do you think that using the switch is the right move for my program or could I incorporate if? Thanks.
Code:
import java.util.Vector;
import javax.swing.JOptionPane;
public class Zoo {
public class Animal {
// data fields
public String breed;
public String name;
public String color;
public String gender;
public int age;
public Animal(String b, String n, String c, String g, int a)
{
breed = b;
name = n;
color = c;
gender = g;
age = a;
}
}
public String toString()
{
System.out.Println("Breed" + " Name" + " Age");
System.out.Println(breed + " " + name + " " + age);
}
} // end class Animal
// new Zoo method
public Zoo() {
String theBreed;
String theName;
String theColor;
String theGender;
int theAge;
String[] animalMenu = {
"1) Add an animal to zoo",
"2) Display all breeds of animals",
"3) Display all information on animals",
"4) Display number of animals",
"5) Display age of the youngest animal",
"6) Exit"
};
int menuSelection = 0;
StringBuffer output = new StringBuffer(800);
Vector animals = new Vector();
do
{
menuSelection = JOptionPane.showOptionDialog(null,
"Please select an option:", "Menu",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE, null,
animalMenu, animalMenu[0]) + 1;
switch (menuSelection)
{
case 1:
theBreed = JOptionPane.showInputDialog("Enter the breed:");
theName = JOptionPane.showInputDialog("Enter the name:");
theColor = JOptionPane.showInputDialog("Enter the color:");
theGender = JOptionPane.showInputDialog("Enter the gender:");
theAge = 0;
do
{
try
{
String inputAge = JOptionPane.showInputDialog("Enter the age:");
theAge = Integer.parseInt(inputAge);
break;
}
catch(NumberFormatException e){
JOptionPane.showMessageDialog( null,
"Please input int values only",
"ERROR", JOptionPane.ERROR_MESSAGE);
}
} while (true);
animals.add((Object) new Animal(theBreed, theName, theColor, theGender, theAge));
break;
case 2:
JOptionPane.showMessageDialog(null, animals.theBreed, "All Breeds");
break;
case 3:
for(int i = 0; i < animals.size(); i++)
{
output.append(animals.get(i).toString() + "\n");
}
JOptionPane.showMessageDialog(null,
output.toString(),
"Zoo Animals", JOptionPane.PLAIN_MESSAGE);
case 4:
JOptionPane.showMessageDialog(null, "Number of animals: ");
JOptionPane.showMessageDialog(null, animals.size(i));
break;
case 5:
JOptionPane.showMessageDialog(null, "Age of youngest animal: ");
JOptionPane.showMessageDialog(null, Math.Min(i);
break;
case 6:
break;
default:
}
} while (menuSelection != 6);
}
public static void main(String[] args) {
Zoo app = new Zoo();
}
}
-
>"class or interface expected" error
That sounds like a mismatching of () or {}
Pair them off. Make sure there are no extra ones or that one is not missing.
For example:
}
} <<<<<<<< Where is this ones mate?
public String toString()
-
Looks to me like Norm has caught the probable bug ... this is the kind of error which "pops up" during code editing when you've been cutting and pasting code.
Are you using special formatting for your java code in TextPad (tabs, colors, etc) ?
-
compilable code
Problem was at line 23, apart from logical mistakes. See the lines which I commented. This is compilable code and a swing window is getting launched. But I am sure there are some logical mistakes. That you have to fix to make the program work properly.
Code:
/* aggarwal dot mohit at gmail dot com */
import java.util.Vector;
import javax.swing.JOptionPane;
public class Zoo {
public class Animal {
// data fields
public String breed;
public String name;
public String color;
public String gender;
public int age;
public Animal(String b, String n, String c, String g, int a)
{
breed = b;
name = n;
color = c;
gender = g;
age = a;
}
public String toString()
{
System.out.println("Breed" + " Name" + " Age");
return (breed + " " + name + " " + age);
}
} // end class Animal
// new Zoo method
public Zoo() {
String theBreed;
String theName;
String theColor;
String theGender;
int theAge;
String[] animalMenu = {
"1) Add an animal to zoo",
"2) Display all breeds of animals",
"3) Display all information on animals",
"4) Display number of animals",
"5) Display age of the youngest animal",
"6) Exit"
};
int menuSelection = 0;
StringBuffer output = new StringBuffer(800);
Vector animals = new Vector();
do
{
menuSelection = JOptionPane.showOptionDialog(null,
"Please select an option:", "Menu",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE, null,
animalMenu, animalMenu[0]) + 1;
switch (menuSelection)
{
case 1:
theBreed = JOptionPane.showInputDialog("Enter the breed:");
theName = JOptionPane.showInputDialog("Enter the name:");
theColor = JOptionPane.showInputDialog("Enter the color:");
theGender = JOptionPane.showInputDialog("Enter the gender:");
theAge = 0;
do
{
try
{
String inputAge = JOptionPane.showInputDialog("Enter the age:");
theAge = Integer.parseInt(inputAge);
break;
}
catch(NumberFormatException e){
JOptionPane.showMessageDialog( null,
"Please input int values only",
"ERROR", JOptionPane.ERROR_MESSAGE);
}
} while (true);
animals.add((Object) new Animal(theBreed, theName, theColor, theGender, theAge));
break;
case 2:
// JOptionPane.showMessageDialog(null, animals.theBreed, "All Breeds");
break;
case 3:
for(int i = 0; i < animals.size(); i++)
{
output.append(animals.get(i).toString() + "\n");
}
JOptionPane.showMessageDialog(null,
output.toString(),
"Zoo Animals", JOptionPane.PLAIN_MESSAGE);
case 4:
JOptionPane.showMessageDialog(null, "Number of animals: ");
//JOptionPane.showMessageDialog(null, animals.size(i));
break;
case 5:
JOptionPane.showMessageDialog(null, "Age of youngest animal: ");
//JOptionPane.showMessageDialog(null, Math.Min(i) );
break;
case 6:
break;
default:
}
} while (menuSelection != 6);
}
public static void main(String[] args) {
Zoo app = new Zoo();
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks