I'm teaching myself java and I'm a few weeks in, decided to make a program with several options in it.
First part is to select between two animals, in this case a seal and hippo.
After that the part I'm having trouble with after selecting the seal is that I want options after selecting the seal, such as repeating the action, terminating it, or asking for proper input when user types something random.
Here's my code
import java.util.Scanner;
class animal {
//Animal variables
String name;
String type;
//Method or subroutine, something an object belonging to the class can do
void sound() {
System.out.println(name + " was petted! " + name + " went 'Gauuu!'");
}
}
public class Objectmethod {
public static void main(String[] args) {
Scanner scanner1 = new Scanner(System.in);
Scanner scanner2 = new Scanner(System.in);
//Creating an object under the class of Animal
animal animal1 = new animal();
animal1.name = "Baby Elephant Seal";
animal1.type = "Seal";
//Creating second animal
animal animal2 = new animal();
animal2.name = "Hippopotawhateveritis";
animal2.type = "Hippopotamus or however you spell it";
//Beginning prompt for user input
System.out.println("Would you like to pet the seal, or the hippo?");
//The code to recieve input of the user
String select = scanner1.nextLine();
//check user input to select correct object
while(true)
if(select.equals("seal")){
//Command that the animal sounds has.
animal1.sound();
//Prompt if the user would like to repeat
System.out.println("Would you like to pet the seal again?");
//second input recieving from user
String input = scanner2.nextLine();
//Checks user input for yes no or random crap
if(input.equals("yes")){
continue;
}
else if(input.equals("no")){
break;
}
else{
System.out.println("Answer yes or no you derpface.");
input = scanner2.nextLine();
}
}
else if(select.equals("hippo")){
animal2.sound();
break;
}
else{
System.out.println("You cray cray. Just pick one.");
select = scanner1.nextLine();
}
System.out.println("Thank you for participating.");
}
}
Typing seal works fine
then when input gibberish to get the else response of "Answer yes or no you derpface"
it works the first time, then the second time it goes back up to the response.
This is what happens
Would you like to pet the seal, or the hippo?
seal
Baby Elephant Seal was petted! Baby Elephant Seal went 'Gauuu!'
Would you like to pet the seal again?
"randominput"
Answer yes or no you derpface.
"secondrandominput"
Baby Elephant Seal was petted! Baby Elephant Seal went 'Gauuu!'
Would you like to pet the seal again?
no
Thank you for participating.
What do I have wrong that causes it to go to the "if" instead of the "else"? -Solved-
New problem. Here is what the last part of the script looks like after my attempt at fixing it.
I believe the issue is occurring at the chunk of code before termination, however I'm not sure what exactly it is as I've tried to apply the aforementioned fix and it didn't work. (The else statement just above the "Thank you for participating" code) Solved
(The issue was that having moved the first "while(true)" above everything else. I moved it back to where it was originally below "scanner1" and it is now functioning properly
-Note- This is after applying the first "while(true)" to robustly repeat petting of animals.
else{
while(!select.equals("seal") && !select.equals("hippo")){
System.out.println("You cray cray. Just pick one.");
select = scanner1.nextLine();
}
Example of the issue: (Quotes represent user input.)
Would you like to pet the seal, or the hippo?
"neither"
You cray cray. Just pick one.
"neither"
You cray cray. Just pick one.
"seal"
Thank you for participating.
Would you like to pet the seal, or the hippo?
The way I want it to occur: (Quotes represent user input.)
Would you like to pet the seal, or the hippo?
"neither"
You cray cray. Just pick one.
"neither"
You cray cray. Just pick one.
"seal"
Baby Elephant Seal was petted! Baby Elephant Seal went 'Gauuu!'
Would you like to pet the seal again?
...
...
//Rest of the program (Confirming repeated petting, or termination of program)
This should fix one of your problems. Let me know if this isn't what you wanted.
else{
while (!input.equals("yes") && !input.equals("no")){
System.out.println("Answer yes or no you derpface.");
input = scanner2.nextLine();
}
}
You have another issue though. You poll for the user response before the while(true) loop.
NOTE: After reviewing your code further, it seems like this functionality was intended. If you want to make your code more robust to continually pet all animals, see the code below.
You should edit your code to be like this:
while(true) {
System.out.println("Would you like to pet the seal, or the hippo?");
//The code to recieve input of the user
String select = scanner1.nextLine();
.
.
.
// the rest of your code
Per OP's request, here should be the fully edited code:
import java.util.Scanner;
class animal {
// Animal variables
String name;
String type;
// Method or subroutine, something an object belonging to the class can do
void sound() {
System.out.println(name + " was petted! " + name + " went 'Gauuu!'");
}
}
public class Objectmethod {
public static void main(String[] args) {
Scanner scanner1 = new Scanner(System.in);
Scanner scanner2 = new Scanner(System.in);
// Creating an object under the class of Animal
animal animal1 = new animal();
animal1.name = "Baby Elephant Seal";
animal1.type = "Seal";
// Creating second animal
animal animal2 = new animal();
animal2.name = "Hippopotawhateveritis";
animal2.type = "Hippopotamus or however you spell it";
// check user input to select correct object
while (true) {
// Beginning prompt for user input
System.out.println("Would you like to pet the seal, or the hippo?");
// The code to recieve input of the user
String select = scanner1.nextLine();
if (select.equals("seal")) {
// Command that the animal sounds has.
animal1.sound();
// Prompt if the user would like to repeat
System.out.println("Would you like another animal?");
// second input recieving from user
String input = scanner2.nextLine();
// Checks user input for yes no or random crap
if (input.equals("yes")) {
continue;
} else if (input.equals("no")) {
break;
} else {
while (!input.equals("yes") && !input.equals("no")) {
System.out.println("Answer yes or no you derpface.");
input = scanner2.nextLine();
}
}
} else if (select.equals("hippo")) {
animal2.sound();
break;
} else {
System.out.println("You cray cray. Just pick one.");
select = scanner1.nextLine();
}
}
System.out.println("Thank you for participating.");
}
}
Related
I am currently creating a program where the user enters a specific set of questions. And the program must go back to the menu after completely answering all questions. How should I do it?
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("""
\n \nAre you ready to take the quiz?
Enter "Y" to proceed or "N" to exit the program:""");
String TakeQuiz = input.nextLine();
if (TakeQuiz.equalsIgnoreCase("Y"))
do {
//blocks of code
}
}
}
System.out.println("Do you want to take the quiz again?");
String RetakeQuiz = input.nextLine();
while (RetakeQuiz.equalsIgnoreCase("Y")) ;
else {
System.out.println("We hope to see you again soon!");
System.exit(0);
}
}
}
There are many ways to achieve what you want, I would not clutter the main method and break the code to another function and loop there.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
for(;;)
takeQuiz();
}
public static void takeQuiz(){
Scanner input = new Scanner(System.in);
System.out.print("\n \nAre you ready to take the quiz?" +
"Enter \"Y\" to proceed or \"N\" to exit the program:");
String takeQuiz = input.nextLine();
if (takeQuiz.equalsIgnoreCase("Y")) {
System.out.println("Running code...");
System.out.println("Question 1");
System.out.println("Question 2");
System.out.println("Question 3");
}
// retake
if (takeQuiz.equalsIgnoreCase("R")){
takeQuiz();
}
if (takeQuiz.equalsIgnoreCase("N")){
System.out.println("We hope to see you again soon!");
System.exit(0);
}
}
}
Notice the escape character for quotes \" and the + for multiline Strings
Java 15 and beyond allows triple quotes as Java Text Blocks
so your String message should be valid
The basic structure is something like this:
boolean continueWithQuiz = true;
while (continueWithQuiz) {
// Put the code here for handling the quiz
...
// Should we keep going?
System.out.println("Do you want to take the quiz again?");
String retakeQuiz = input.nextLine();
continueWithQuiz = retakeQuiz == "Y";
}
One more comment. Please follow Java naming standards. Class names begin with an upper case letter. Constants should be ALL_CAPS. Everything else is in lower case.
I am literally know and get the hang of the java right now and I'm writing the program that helps to records patient'd ID in the Hospital, i'll show the whole code first,then, I will tell where you will, here is the code
package hospitalsrecord;
import java.util.*;
import java.io.*;
public class HospitalsRecord {
public static Scanner read = new Scanner(System.in);
public static ArrayList nameList = new ArrayList();
public static ArrayList patientAge = new ArrayList();
public static ArrayList Disease = new ArrayList();
public static ArrayList dateHospitalized = new ArrayList();
public static ArrayList roomNumber = new ArrayList();
//adding patient function
public static void AddNewPatient () {
//Ask patient's name
System.out.println("Please enter patient's name:");
String patientName = read.next();
//Ask Patient's age
System.out.println("Please enter patient's age:");
int age = read.nextInt();
//Ask patient's illness
System.out.println("Please enter patient's Disease name (also include accidents eg. Leg broke by Car Accident):");
String illness = read.next();
//Ask patient Hospitalized date
System.out.println("Please enter patient's Hospitalized date(Total days not included):");
String HPTLdate = read.next();
//Ask patient's room number
System.out.println("Please enter patient's hospitalize room number(3 degits):");
int HRN = read.nextInt();
//Confirmation
System.out.println("Doctor, would you like to confirm the following(y/n)?");
System.out.println("Name:" + patientName);
System.out.println("Age:" + age);
System.out.println("Disease:" + illness);
System.out.println("Date Hospitalized (HPTLD):" + HPTLdate);
System.out.println("Room Number:" + HRN);
String Confirm = read.next();
if (Confirm.equals("y")) {
nameList.add(patientName);
patientAge.add(age);
Disease.add(illness);
dateHospitalized.add(HPTLdate);
roomNumber.add(HRN);
} else {
AddNewPatient();
}
}
//Searching patient that listed
public static void searchPatient (){
}
//remove the patient function
public static void removePatient() {
}
//text printing function when strat the program
public static void selectorPage(){
System.out.println("Hello Doctor, welcome to Hospital Recorder v1.0.0");
System.out.println("If you want to add new patient into this recorder type: 'add' in the next blank line line");
System.out.println("If you want to search the patient list type: 'search' in the next blank line");
System.out.println("And, if you want to remove the patient that was out of hospitalizing type: 'remove' in the next blank line");
option = read.next();
}
//text printing simmilar to selecterPage function but perform after function
public static void selecterPageAfterAction() {
System.out.println("Your action has been performed, doctor");
System.out.println("Would you like to perform another action?(y/n)");
choiceSelection = read.next();
if (choiceSelection.equals("y")){
System.out.println("If you want to add new patient into this recorder type: 'add' in the next blank line line");
System.out.println("If you want to search the patient list type: 'search' in the next blank line");
System.out.println("And, if you want to remove the patient that was out of hospitalizing type: 'remove' in the next blank line");
option = read.next();
}
}
//Selection var
public static String option;
public static String choiceSelection;
//Main program
public static void main(String[] args) {
selectorPage();
switch (option) {
case("add"): {
AddNewPatient();
break;
}
case("search"):{
searchPatient();
break;
}
case("remove"):{
removePatient();
break;
}
case("end"):{
break;
}
default: {
System.out.println("Please enter the indentified option");
break;
}
}
if (option.equalsIgnoreCase("end")){
}
}
}
I hope you guys can read every line because it was so so so so complex, but for someone who can read all of it, i'll know that you'll say I still need more time for hard working, no worry i'll spend sometime to get most knowledge from you guys first, but still working hard for program to complete while waiting for answers! anyway the point that I want you guys to focus at this point:
if (option.equalsIgnoreCase("end")){
}
It maybe too blank because I've just newly add it while i'm working on it. So, what I want to know is at the if statement I type option.equalsIgnoreCase("end"), Am I explain the computer to do the following?
1.Compare the the String variable options with the String"end"?
2.Tell the computer to do the action inside if statement's when the String option wasn't the word end?
And please tell me how this method work, i don't clearly understand it. I understand like this "It compare two strings if it wasn't the same then it's result is true" I know my explanation is wrong so could you please help me? thanks again for helping if you can.
option.equalsIgnoreCase("end") - equalsIgnoreCase will ignore whether string is in lower case or uppercase.
So it will enter into if block only when option variable has either end or END.
Your first assumption is correct, you are asking to compare String whether it is equal to end. But Second one is wrong, from above code it will enter and execute statements present inside if only when option is end/END.
If you want to go inside If block when the option is not end then add a not like this if(!option.equalsIgnoreCase("end")).
I Hope this clears your doubt!
The class String has two methods to compare one String to another.
See the example below:
public static void main(String[] args) {
String str1 = "beer";
String str2 = "Beer";
System.out.println(str1.equals(str2));
System.out.println(str1.equalsIgnoreCase(str2));
}
The first method equals() compares str1and str2and takes the case into consideration. Hence, the first comparison results in false, meaning Beer is not equal to beer.
The second method equalsIgnoreCase()does the same, except that it is not case-sensitive. Result of this comparison is true, meaning "ignoring the case, Beer is the same string as beer".
Hope this helps.
I am taking the first Java class and working on my second project. The project is about creating an program as a network of rooms on a virtual three-dimensional work area. Each room provides a virtual environment that together can be assemble into a simulated or virtual world.
Basically, the beginning of the program, I used while loop, and at the end I want to ask user if he/she wants to quit the program, and print a thank you message. However, the while loop does not work. My program quit no matter I entered y or n. Below is my codes.
import java.util.Scanner;
public class Project
{
public static void main(String[] args)
{
Map map = new Map();
int floor = 0;
int row = 0;
int col = 0;
String input = " ";
Scanner scan = new Scanner(System.in);
// Begin user dialog. Welcome message
System.out.println("Welcome to the L.A Underground! (Verson 1.1)");
System.out.println();
String choice = "y";
while(!input.equalsIgnoreCase("quit"))
{
input = scan.nextLine().toLowerCase();
// My codes are here
if (input.equals("south")
{statement}
else
System.out.println("You can't go that way.");
else if (input.equals("quit"))
{ // See if user wants to continue
System.out.println("Do you wish to leave the Underground (Y/N)? >");
choice = scan.nextLine();
System.out.println();
}
// if user enters other words than quit
else
System.out.println("I don't recognize the word '" + input +"'");
}
System.out.println("Thank you for visiting L.A Underground.");
}
}
When I typed "quit" the console printed the message: "Do you wish to leave the Underground? (Y/N)? >". I tried Y/N (y/n) the program terminated. Any help is appreciated. Thank you.
Updated: Sorry for the confusion. What I wanted the program to run is when the user types "quit", the message will print out "Do you wish to leave the Underground (Y/N)?>?" , and if the user types "hello", the message will be "I don't understand the word 'hello'". And when the user type y, the program will quit, otherwise (type n), the program will start over again.
Ask for user input inside of your loop. If input.equalsIgnoreCase("quit"), then prompt the user an "are you sure" message. If the input.equalsIgnoreCase("y"), then break the loop, otherwise, keep going.
Scanner scan = new Scanner(System.in);
String input;
// Begin user dialog. Welcome message
System.out.println("Welcome to the L.A Underground! (Verson 1.1)");
System.out.println();
while (true) {
input = scan.nextLine();
if (input.equalsIgnoreCase("quit")) {
System.out.print("Do you wish to leave the Underground (Y/N)? >");
if (scan.nextLine().equals("y")) {
break;
}
}
// input wasn't "quit", so do other stuff here
}
System.out.println("Thank you for visiting L.A Underground.");
Your code loops until it gets "quit" ... then asks for "yes/no" ... then simply exits, regardless.
You need to change your loop, so that it includes BOTH "MY CODES HERE" AND the "quit y/n" check.
EXAMPLE:
...
boolean done = false;
while(!done) {
//MY CODES ARE HERE
if (input.equalsIgnoreCase("quit") && getYesNo ()) == 'y') {
done = true;
}
}
"getYesNo()" is a method you write. For example:
char getYesNo () {
System.out.print("Do you wish to leave the Underground (Y/N)? >");
String line = scan.nextLine();
return line.charAt(0);
}
In the code you've posted, your loop is being controlled by the condition !input.equalsIgnoreCase("quit"). That is, if input is "quit", the loop is terminated.
But the following block is executed only if input is "quit":
if (input.equals("quit"))
{
// See if user wants to continue
System.out.println("Do you wish to leave the Underground (Y/N)? >");
choice = scan.nextLine();
System.out.println();
}
So if this block is executed, !input.equalsIgnoreCase("quit") evaluates to false and the loop is terminated. And that's not what you want.
Now that you know what's wrong, fixing it is easy. Check the value of choice in the above if block: if choice is not yes, don't quit i.e. reset input to a default value.
I've pasted the working code here on pastebin.
Only my third week of class (new to programming).
I'm making a text-based story in Java, but I've come to a stump in the process. I have a static variable called "static String dogName;" that I'm trying to change the value of (only once). In the beginning of the game, the user has the option to name their dog. When I try to name the dog, the code skips the naming prompt because of the static String dogName.
I want to give the user the option to name their dog.
If there's a better way to do things in my code, please let me know.
Part of the code may not be complete like the decisions...
public static Scanner keyboard = new Scanner(System.in);
public static int choice;
// dogName is Dogs name forever a hundred times rick & morty
static String dogName;
public static void main(String[] args) {
int karma = 0;
// Dog stuff...
Dog Dandy;
Dandy = new Dog();
// Prologue
System.out.println("You're walking through an alley late at night "
+ ", you see a stray dog. What do you do? ");
System.out.println("[1] Approach");
System.out.println("[2] Attempt to touch");
System.out.println("[3] Give treat");
boolean running = true;
GAME:
while (running) {
choice = keyboard.nextInt();
switch (choice) {
case 1:
System.out.println("The dog became alarmed!");
Dandy.bark();
break;
case 2:
System.out.println("The dog becomes aggressive!");
Dandy.bite();
break;
case 3:
System.out.println("The dog comes in peace");
Dandy.sit();
break;
}
if (choice == 1) {
System.out.println("You stand back in caution. You cannot risk being bitten.");
}
if (choice == 2) {
System.out.print("");
karma--;
}
if (choice == 3) {
System.out.println("You give the dog a treat. It wags its tail in excitement");
karma++;
}
// Chapter 1.1 - Man's best friend
System.out.println("\nThe dog will live a harsh life in the outside world. What would you like to do? "
+ "\n[1] Adopt dog\n[2] Leave dog\n[3] Quit game! You're bored...");
choice = keyboard.nextInt();
switch (choice) {
case 1:
System.out.println("\nYou welcome your new companion");
System.out.println("\nWould you like to give him a name?\n[1] No\n[2] Yes");
choice = keyboard.nextInt();
switch (choice){
case 1:
System.out.println("You see a shiny object beneath his foot, it's a dog collar."
+ "\nYou pick up the dog collar and see the name Todd on it."
+ "\nYes, because you did not choose a name for your dog, we gave him the most basic name ever. "
+ "You're welcome.");
dogName = "Todd"; //RIP doge
karma--;
break;
case 2:
dogName = keyboard.nextLine();
karma++;
}
}
// Good guy player gives his dog a name
// Chapter 1.2 - Home sweet home
System.out.println("\n" + dogName + " crawls up to your leg and lets out a whimper.\n"
+ "Is " + dogName + " just afraid of the dark, or is he hungry?"
+ "\nYou don't know the last time he ate. What will you do?");
System.out.println("\n[1] Go home\n[2] Find a store\n[3] Search the area");
choice = keyboard.nextInt();
if (choice == 1){
System.out.println("\nYou head home with " + dogName + " as fast as you can.\n"
+"On the way back, " + dogName + " seems extremely happy to be with"
+ " his new owner.\nGoing out you had no idea you'd bring home a new friend.");
karma++;
}
if (choice == 2){
System.out.println("");
System.out.println("");
}
if (choice == 3){
}
}
// GAME ENDING
if (karma > 0) {
System.out.println("\nYou ended with " + karma + " karma. Good job!");
}
else if (karma == 0){
System.out.println("\nYou ended with " + karma + " karma. Neither good nor bad, a neutral state.");
}else{
System.out.println("\nYou ended with " + karma + " karma. Bad job!");
}
// CREDITS
System.out.println("\n\t# THANK YOU FOR PLAYING #");
System.out.println("\t# Game created by aliens from outer space #");
}
}
When I try to name the dog, the code skips the naming prompt because of the static String dogName.
No, the problem is unrelated to dogName being static. Instead, the problem is with the way you use the Scanner. When you do keyboard.nextInt() it reads just enough data to be able to return an int. So, if the user types 2 and Enter, the Scanner will read the 2 and return it as an int, leaving the newline character in the input buffer.
Then, when you go to read the dog's name with dogName = keyboard.nextLine(); the newline character that's already present causes it to return an empty string for the dog's name immediately, rather than wait for any user input.
You can fix this by doing another keyboard.nextLine() just before you ask for the dog's name:
case 2:
keyboard.nextLine();
dogName = keyboard.nextLine();
karma++;
The first nextLine() eats up the newline from the previous number that was typed, and the second nextLine() returns a line of text (sans newline) that can be assigned to dogName.
However, there are other problems you will run into with Scanner. If the player types anything other than a number, nextInt() will throw an InputMismatchException. It's possible to work around these problems, but they can end up giving you a headache.
You might be better off using keyboard.nextLine() every time to get a line from the player, and then checking to see if it contains a number and parsing that number.
Also, the convention in Java is to use lower case letters to begin variable names, so your variable Dandy should be named dandy. Others have given some sensible suggestions about splitting your program up into pieces rather than having one monolithic main method.
couple of things.
Separate your logic from your Main method class.
Create a POJO for Dog (I think you already have one) class that takes name in the constructor argument.
public class Dog {
private String dogName;
public Dog(String dogName)
this.dogName = dogName;
}
//getters setters..
}
Instead of putting everything in your main, you'll have to remove the static keyword, and make a new instance of the program, something like this:
public static void main(String[] args) {
Game game = new Game();
game.start();
}
Then the choice and dogName variable doesn't have to be static anymore.
For general remarks: start by splitting up your code into multiple methods, each grouped by functionality, for example, one method for handling the user input, one for printing the options, etc. That way your code will become less of a mess, and allows you to refactor later into different classes more easily.
#DavidConrad's answer covers your actual error, but I thought I'd add more on "what you could do better"
public static Scanner keyboard = new Scanner(System.in);
public static int choice;
// dogName is Dogs name forever a hundred times rick & morty
static String dogName;
Note how all these fields are static? This is only required because you are trying to use them from a static method (that method being main). With only the code you have posted, it would seem you could move those fields into the main method like where you create the Dog.
Also, a tiny pet-peeve of mine, but it's not actually a rule or anything, is "excessive" spacing - (like inbetween choice and dogName). One space is fine. Two is "too" many for my liking. ;)
public static void main(String[] args) {
int karma = 0;
// Dog stuff...
Dog Dandy;
Dandy = new Dog();
Grr, more spaces! But, more importantly is the line about Dog Dandy. Your variable name is capitalized, and it is best practice to name variables in lower-case (such as you did correctly for karma). You should also declare and initialize your Dog in the same line, like so: Dog dandy = new Dog();
With the prologue and chapters, you may consider separating these into a separate class. You may notice a pattern in each chapter.
some text is given
options are given
result of option is displayed
You could greatly improve the overall readability of your code by creating a class which could taken some introText, options, and then show an option depending on the choice made. If that seems over your head, then I wouldn't worry about it though - it's only your third week so I wouldn't expect you to have the differences between classes, methods, and fields down pat quite yet. If this is something you are seriously interested in, you could find all kind of tutorials covering those, it will be greatly beneficial when you truly understand what they can do and how they interact.
Hey everyone Someone might have asked this but Im trying to implement an ArrayList from a users answer I have done some research and it wont do. Here's my code < part of it
public class Breed
{
public static void cross_breed()
{
// Creates 3 strings named male_breed, female_breed, and cross_breed.
String male_breed;
String female_breed;
String cross_breed;
Cross_breed crossbreed;
List<String> clist = new ArrayList<String>();
while (true) // While the male_breed is a real breed continue on with the program
{
male_breed = JOptionPane.showInputDialog("What is the breed of the male?");
if (male_breed.length() == 0) break; // Makes it so if the male dog breed wasn't entered the program will exit
if (List_Breeds.blist.containsKey(male_breed)) //If the given input of the users is on the list of breeds continue with program
{
while (true) // Loops the female breed
{
female_breed = JOptionPane.showInputDialog("What is the breed of the Female");
if (List_Breeds.blist.containsKey(female_breed)) //
{
if (male_breed.equals(female_breed)) // if both breeds are the same
{
clist.add(JOptionPane.showMessageDialog(null, "Your dog is a Purebread " + male_breed));
break; //Ends the program
}
else
{
cross_breed = male_breed.substring(0, 3) + "a" + female_breed.substring(0, 3); //If both breeds arent the same take the
clist.add(JOptionPane.showMessageDialog(null, "Your dog breed is a " + cross_breed)); // first three letters of the male_breed and inserts
break; // Ends the program // an 'A' at the end.
}
Eclipse is giving me an error saying for both lines
clist.add(JOptionPane.showMessageDialog(null, "Your dog is a Purebread " + male_breed));
and
clist.add(JOptionPane.showMessageDialog(null, "Your dog breed is a " + cross_breed));
that states "the method add in the Type List is not applicable for the arguments (void)
if I'm doing this wrong please help... What I am trying to do is add the breed (or cross breed) name to an arraylist in which will be displayed in a second menu
"the method add in the Type List is not applicable for the arguments (void)" mean the method you're calling - JOptionPane.showMessageDialog(JComponent, String) does not return anything.
This is because showMessageDialog is for showing messages, not capturing input.
You need to take a look at the various flavours of JOptionPane.showInputDialog() in order to get input from the user.