I am having trouble printing out the first and last name scanned in (fname1, lname1). I have to create 6 objects and these are two that I can't seem to even start with. Also, if I enter anything except "yes" or "y", it will not loop back to the radiobuttons I inserted above the snippet. How do I fix this?
This is what prints out in the output window:
[,0,0,0x0,invalid,layout=java.awt.FlowLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=java.awt.Dimension[width=350,height=200]]
public class Cabin_Selector
{
public static void main(String[] args)
{
JFrame cabin_selection = new JFrame("Select Your Cabin"); //Prompts the user
cabin_selection.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Close the Frame when exiting
Project2_JoshuaLucas selection = new Project2_JoshuaLucas("", "", "", "", "", "", "", 0.00); //Call the constructor for the Project2_JoshuaLucas class
cabin_selection.getContentPane().add(selection); //put the object in the current pane of the jframe
cabin_selection.pack(); //size the frame
cabin_selection.setVisible(true); //make the frame visible
} //end main method
} //end class
if (source == cabin1)
{
cabin1.setBackground(Color.darkGray);
cabin2.setBackground(Color.gray);
cabin3.setBackground(Color.gray);
cabin4.setBackground(Color.gray);
cabin5.setBackground(Color.gray);
cabin6.setBackground(Color.gray);
cabin7.setBackground(Color.gray);
cabin8.setBackground(Color.gray);
cabin9.setBackground(Color.gray);
cabin10.setBackground(Color.gray);
suite1.setBackground(Color.red);
suite2.setBackground(Color.red);
System.out.println("Your choice is Cabin 11-1, would you like to designate this as your room?");
info1 = scan_in.nextLine();
info1 = info1.toLowerCase();
if ( info1.equals ("yes") || info1.equals ("y"))
{
continues=true;
System.out.println("Please enter the number of people in your cabin (*Maximum number of people is 2*)");
cabin_people = scan_in.nextInt();
scan_in.nextLine();
while(continues)
{
switch (cabin_people)
{
case 1:
System.out.println("There is one passenger within the cabin. (You will pay an EXTRA 45% because of the empty passenger slot)");
continues=false;
onepassenger=true;
break;
case 2:
System.out.println("There are two passenger within this cabin.");
continues=false;
twopassenger=true;
break;
default:
System.out.println("Please try again. Remember, the maximum amount of passengers allowed is 2.");
System.out.println("How many passengers are staying within this cabin?");
cabin_people=scan_in.nextInt();
scan_in.nextLine();
continues=true;
}//Closes the Switch
}//Closes the while(continues) loop
while(onepassenger)
{
System.out.println("Please state your FIRST name: ");
fname1=scan_in.nextLine();
System.out.println();
System.out.println("Please state your LAST name: ");
lname1=scan_in.nextLine();
onepassenger=false;
Project2_JoshuaLucas passenger1 = new Project2_JoshuaLucas (fname1, lname1, "", "", "", "", "", 0.00);
System.out.println(passenger1);
} //Closes while(1passenger)
while(twopassenger)
{
System.out.println("Please state your FIRST name: ");
fname1=scan_in.nextLine();
System.out.println("Please state your LAST name: ");
lname1=scan_in.nextLine();
System.out.println("Please enter the second passenger's FIRST name: ");
fname2=scan_in.nextLine();
System.out.println("Please enter the second passenger's LAST name: ");
lname2=scan_in.nextLine();
System.out.println("Please enter the city you live in: ");
cob=scan_in.nextLine();
twopassenger=false;
} //Closes while(2passenger)
} //Closes yes | y
else
System.out.println("Please select another cabin");
continues=false;
} //Closes source==cabin1
Your displaying the toString() method returned by one of your Swing components -- likely a JPanel since it uses FlowLayout, and the solution is:
Don't pass a Swing component into a System.out.println(...) call,
Instead print out the state of your key non-GUI objects, called "model" objects because they model the behavior and state of your program.
Make sure that these classes have a decent public String toString() method override, so what they print out makes sense.
You ask:
What is a model object?
If you are creating an airplane GUI for instance, you'll likely need to create several classes, some of the GUI component classes, others non. The model classes could include:
Passenger
Airplane
AirplaneSeat
....
These model classes will contain information about themselves, such as a Passenger will have a name, perhaps an age, a passengerNumber id number... The Airplane will have a collection of first class AirplaneSeats and likewise for coach. AirplaneSeat will have a boolean occupied field, and perhaps a Passenger field to tell who occupies what seat. But none of these classes will contain GUI component code, none will extend JPanel or JFrame or such.
Then you'll likely have view classes that do either extend GUI components or contain them, and they will display the state of the model classes above. You tried to print out the toString() of a view class, one that does not override the toString() method.
A side recommendation: you appear to be trying to mix a Swing GUI program with a console program, and you're not going to want to do this. Instead you should have all user interaction and output in the GUI, not in console, except perhaps for simple debugging purposes, but nothing more.
Related
Basically i want to enter the names as long as i don't cancel the InputMessageDialog, then i want to asign my names to variable created before and print them out at the end in the MessageDialog. I was trying some stuff outside the loop but got the notificacion that "value 'names' is always 'null'"
String names;
while (true) {
names = JOptionPane.showInputDialog(null, "ENTER THE NAMES");
if (names == null) {
JOptionPane.showMessageDialog(null, "ENTRY CANCELED!");
break;
} else {
}
}
JOptionPane.showMessageDialog(null, "YOUR NAMES: " + names);
Your code is pretty close to what you describe as your goal – the primary thing missing is that you need to keep track of the various values along the way, and print them at the end.
The code you posted will loop again and again asking for a single value (which you are storing into String names - a little confusing choice for variable name, since it contains only one name input). As you found, when the user hits the cancel button (to end the loop), it sets names to null. The final step shows a dialog box with the last value for names (which is always null).
Here's a program that:
loops until the user hits the "cancel" button (which would set input to be null), or if they enter a blank value – this allows the user to exit by simply hitting return without typing anything
adds all non-empty input values to a java.util.Set – this is an arbitrary choice, use whatever data structure is appropriate for your program
shows a final dialog with the contents of the set
import javax.swing.*;
import java.util.HashSet;
import java.util.Set;
public class t {
public static void main(String[] args) {
Set<String> values = new HashSet<>();
while (true) {
String input = JOptionPane.showInputDialog(null, "enter something");
if (input != null && !input.isEmpty()) {
values.add(input);
} else {
break;
}
}
JOptionPane.showMessageDialog(null, "all values: " + values);
}
}
If I run with the following input:
one
two two
three three three
<blank>
Then the final dialog message is:
all values: [one, two two, three three three]
Note that a java.util.Set doesn't necessarily return items in any specific order, it just happens to have worked out that way in this example.
I'm trying to prevent user to add 2 attendee with the same passport I tried many things as making Boolean variable to check if attendee was added before or no but I have failed all of my attempts can't really think of a way to make this
public class Test {
public static void main(String[] args) {
try {
Workshop Available_Work_Shops = new Workshop();
Scanner sc = new Scanner(System.in);
String passportNumber, workshop;
boolean isAttendeeMatch;
ArrayList<Attendee> attendeeList = new ArrayList<>();
int choice;
do {
System.out.println("1. Add New Attendee");
System.out.println("2. Add Existing Attendee to Workshop");
System.out.println("3. Remove Attendee from Workshop");
System.out.println("4. Print WorkShop List");
System.out.println("5. Print All Attendees");
System.out.println("0. Close Program");
System.out.println("Please Enter a number to choose");
choice = sc.nextInt();
sc.nextLine();
switch (choice) {
case 1 -> {
System.out.println();
System.out.println("Adding New Attendee");
System.out.println("____________________");
System.out.print("Please Enter Attendee Name: ");
String Attendee_name = sc.nextLine();
System.out.print("Please Enter Attendee Passport Number: ");
String Attendee_passport = sc.nextLine();
System.out.print("Please Enter Attendee Age: ");
String Attendee_Age = sc.nextLine();
System.out.print("Please Enter Attendee Phone Number: ");
String phone = sc.nextLine();
attendeeList.add(new Attendee(Attendee_name, Attendee_Age, Attendee_passport, phone));
for (Attendee attendee : attendeeList) {
if (attendeeList.contains(attendee.PassportNumber)) {
System.out.println("Existing User Found please enter '3' to remove the duplicate user");
}
}
}
The Issue
Let's focus on these two lines
...
for (Attendee attendee : attendeeList) {
if (attendeeList.contains(attendee.PassportNumber)) {
...
Since you've defined the attendeeList to be an ArrayList holding Attendee objects, you should be passing Attendee objects to attendeeList.contains(). While the contains() function does accept any Object, it will only return true if one of the List's elements .equals() the object you pass in.
The Solution
I'd recommend changing this block of code...
attendeeList.add(new Attendee(Attendee_name, Attendee_Age, Attendee_passport, phone));
for (Attendee attendee : attendeeList) {
if (attendeeList.contains(attendee.PassportNumber)) {
System.out.println("Existing User Found please enter '3' to remove the duplicate user");
}
}
... to something like this
Attendee potentialAttendee = new Attendee(Attendee_name, Attendee_Age, Attendee_passport, phone)
// Check if the potentialAttendee's passport number is already used by someone in the list.
boolean passportRegistered = false;
for (Attendee attendee : attendeeList) {
if (attendee.PassportNumber.equals(potentialAttendee.PassportNumber) {
System.out.println("That user passport number has already been registered. It can't be added again.");
passportRegistered = true;
}
}
// Only add the potentialAttendee if his/her passport number wasn't already used by someone in the list.
if (!passportRegistered) {
attendeeList.add(potentialAttendee);
}
The key difference is that you shouldn't add the potentialAttendee to the list until you've first validated that he/she meets the requirements to be added to that list.
Potential Enhancements
Store the Attendees in a HashMap instead of an ArrayList. If you key off of the PassportNumber, then by definition you can't have two participants with the same PassportNumber in the collection of attendees.
Java has a nice Streaming API that could make the validation even closer to a one liner.
Right off the bat, I see a few issues that would break the code:
You're adding the attendee to the list and then checking if it exists in the list afterwards (which it always will after adding it if checking correctly).
if (attendeeList.contains(attendee.PassportNumber)) will never return true because you're checking if the entire List has an object that is equal to the PassportNumber in it (as opposed to the PassportNumber in the object). It should be if(attendee.getPassportNumber().equals(Attendee_passport)).
Not sure how much of a beginner you are but it might also be useful to look into things like naming conventions (in general Java variables should be named with a lowercase first letter and each new word has an uppercase letter), using List<Attendee> as the declared variable type instead of ArrayList, and things like filtering lists using Java Streams to see if something exists already. Java also has Set as an alternative to List for when you only want to store unique values (there's other differences between List and Set so make sure you look into it if you want to use it).
So, as you will see I have a menu system. The bit of code you're about to see shows how I intend for the user's input to determine what method/or user option in the program will be presented to them. To do this, I've tried an if statement within my menu system method which I'd like to call to the particular method which is assigned to whatever option the user has picked - to then start that option's purpose/or method. I've typed in the method I'd like option '1' to correspond to within the if statement... but it presents to me an error. Any help on how I fix this or how to go about this... would be truly appreciated. P.s. I've only been learning Java for a couple weeks, and it's my first language.
The error prints this message:
error: non-static method keepCounting() cannot be referenced from a static context
keepCounting();
^
1 error
compiler exit status 1
import java.util.Scanner;
class Main {
// instance fields
public static final Scanner userInput = new Scanner(System.in);
// constructor method (defunct)
// 0. menu system method
public static void menuSystem(){
int usersNumberChoice;
System.out.println("P4CS Mini Applications");
System.out.println("----------------------");
System.out.println("Please select an option:");
System.out.println("1. Keep Counting Game");
System.out.println("2. Number Conversion Tool");
System.out.println("3. UPC Calculator");
System.out.println("4. UPC Checker");
System.out.println("9. Quit");
System.out.println("Please enter an option:");
usersNumberChoice = userInput.nextInt();
if (usersNumberChoice == 1){
keepCounting();
}
}
// 1. keep counting game method
public void keepCounting(){
System.out.println("hello world");
}
Mark your keepCounting() as static because static method can not be called from non-static method.
public static void keepCounting(){
System.out.println("hello world");
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
For my programming class, I have the following assignment:
In this assignment you will write a program that will model a pet
store. The program will have a Pet class to model individual pets and
the Assignment5 class will contain the main and act as the pet store.
Users will be able to view the pets, make them age a year at a time,
add a new pet, and adopt any of the pets.
Create a private static String method which prints out the main menu
of the program. It then accepts a String from the user and returns
their choice. The commands to list are as follows. a. List the pets in
the store. b. Age up the pets. c. Add a new pet. d. Adopt a pet. e.
Quit. i. Your method must verify that the user typed in a valid input
before returning the input.
I've got my code so that it:
Prints the menu
Scans for input
Converts that input to uppercase
Checks if this value is "A","B","C","D", or "E"
If not, it asks for another value
If so, a section of code is executed
What I'm having the most difficulty with is where I should be placing this method, the scanner, and the rest of my code. An explanation of where I should put each and why would be extremely helpful.
Below is some of my code:
import java.util.Scanner;
public class Assignment5{
// Creates a new scanner
Scanner scan = new Scanner(System.in);
private static String mainMenu(Scanner scan){
// Print menu and ask user for input
System.out.println("A. List the pets in the store");
System.out.println("B. Age up the pets");
System.out.println("C. Add a new pet");
System.out.println("D. Adopt a pet");
System.out.println("E. Quit");
System.out.print("Type a letter to make your selection: ");
// Scan for input. Convert to uppercase
String letter = scan.next().toUpperCase();
// Check if letter is valid. Return true or false
public static boolean isValidInput(String letter) {
return (letter == "A" || letter == "B" || letter == "C" || letter == "D" || letter == "E");
}
// If isValidInput is false, ask the user to input another letter.
while (!isValidInput(letter){
System.out.println("That is not one of the options. Input another letter.");
letter = scan.next().toUpperCase();
}
return letter;
}
public static void main(String[] args){
// Create two pets
Pet one = new Pet("Spot", 3);
Pet two = new Pet("Fluffy", 24);
// Initially, pet #3 has no values attached to it
Pet three = null;
// Initial greeting
System.out.println("Welcome to the pet store!");
mainMenu(scan);
// A: List the pets
if (mainMenu(scan) == "A"){
System.out.println("Listing pets...");
System.out.println(one.getName() + " is " + one.getAge() + "-years-old and is currently " + one.getStatus());
System.out.println(two.getName() + " is " + two.getAge() + "-years-old and is currently " + two.getStatus());
// Check if there is a third pet before printing its information
if (three != null){
System.out.println(three.getName() + " is " + three.getAge() + "-years-old and is currently " + three.getStatus());
}
mainMenu(scan);
}
there is more code afterward, but my teacher doesn't like us to post our full code because others might find it and copy it.
Thanks!
I would say the placement of your method and main method looks good. Generally the placement of those will depend with who you work with and coding styles that people around you use, but plain methods before the main method the way you have it works well
As for the scanner. Having it at the top like you have it, so it can be reused for both your main and your main menu method is good. I would include an Access Modifier (public, protected, or private) on it.
For a rough estimate though all your initializations will go first though (such as how you initialized your scanner) then your methods (some people order these by the order in which they are used and some people order these by the access modifier tied to them) then your main method coming last is pretty standard.
Not entirely sure this is what you were asking, but I believe it was something similar to this.
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.