Hello sorry for this bold question but im not sure how to solve my needs on line 24 and 30 in the code. Also im getting error messages on line 33 and 22 so it would be awesome if you could solve those.
import java.util.Scanner;
public class CheckOrder {
public static Scanner userInput = new Scanner(System.in);
public static Scanner userInputStarters = new Scanner(System.in);
public static String menu;
public static String check;
public static String TheRestroom = "The Restroom";
public static String Restroom = "Restroom";
public static String Eat = "Eat";
public static String ToEat = "To Eat";
public static String restOrEat;
public static void main(String[] args) {
System.out.print("Hello and Welcome to the SOMETHING restaurant. Would you like to eat or use the restroom?");
public static restOrEat = userInput.NextInt();
if (restOrEat.equalsIgnoreCase(TheRestroom || Restroom)) {
System.out.println("Please go ahead to the restroom, it's over there.");
System.exit(1);
}
if (restOrEat.equalsIgnoreCase(Eat || ToEat)) {
System.out.print("What would you like to eat for starters? Press 1 for Cheese & Bacon, 2 for Sald (With options) and 3 for noodles");
public static String starter = userInputStarters;
}
}
}
The || operator can not be applied to java.util.String.
restOrEat.equalsIgnoreCase(Eat || ToEat);
The both variables Eat and ToEat are Objects from the the class java.util.String. That's why the || operator does not work in this case.
Change it to:
restOrEat.equalsIgnoreCase(Eat) || restOrEat.equalsIgnoreCase(ToEat);
This will work.
You can't write 'public static' the way you are writing inside the main method.
if (restOrEat.equalsIgnoreCase(TheRestroom) || restOrEat.equalsIgnoreCase(Restroom)) {
Replace both of your lines with above reference
First of all the if statements are incorrect, they are supposed to be like this if (restOrEat.equalsIgnoreCase(TheRestroom) || restOrEat.equalsIgnoreCase(Restroom)) {
Secondly, you can't have the public and static keywords infront of the variables inside methods.
So change the public static restOrEat (You're also missing the object definition) to int restOrEat (Seemingly you already defined it as a String in the class) and the NextInt is supposed to be nextInt()
Also check this out Java Naming Conventions
You have several problems:
Or condition (||) should be between to checks
if (restOrEat.equalsIgnoreCase(TheRestroom) || restOrEat.equalsIgnoreCase(Restroom)) { }
You defined restOrEat as class variable but redefined it (sort of) in main
public static restOrEat = userInput.NextInt();
public static is not valid syntax for a method variable (not even separately) and you don't have the type.
userInput.NextInt(); will return int but restOrEat is String. Use userInput.next() instead.
First of all in a main method you can't use public static. Also restOrEat sounds like is supposed to be a yes or no string, so you can't say nextInt(). use either next() if its just one word or nextLine() if you want a whole line. And in the if statements you can't use the || operator inside the equalsIgnoreCase() call but you need to make two and put the || in the middle.
Try this if it fits what you need.
import java.util.Scanner;
public class CheckOrder {
public static Scanner userInput = new Scanner(System.in);
public static Scanner userInputStarters = new Scanner(System.in);
public static String menu;
public static String check;
public static String TheRestroom = "The Restroom";
public static String Restroom = "Restroom";
public static String Eat = "Eat";
public static String ToEat = "To Eat";
public static String restOrEat;
public static void main(String[] args) {
System.out.print("Hello and Welcome to the SOMETHING restaurant. Would you like to eat or use the restroom?");
restOrEat = userInput.next();
if (restOrEat.equalsIgnoreCase(TheRestroom) || restOrEat.equalsIgnoreCase(Restroom)) {
System.out.println("Please go ahead to the restroom, it's over there.");
System.exit(1);
}
if (restOrEat.equalsIgnoreCase(Eat) || restOrEat.equalsIgnoreCase(ToEat)) {
System.out.print("What would you like to eat for starters? Press 1 for Cheese & Bacon, 2 for Sald (With options) and 3 for noodles");
int starter = userInputStarters.nextInt();
}
}
}
import java.util.Scanner;
class CheckOrder {
public static Scanner userInput = new Scanner(System.in);
public static Scanner userInputStarters = new Scanner(System.in);
public static String menu;
public static String check;
public static String TheRestroom = "The Restroom";
public static String Restroom = "Restroom";
public static String Eat = "Eat";
public static String ToEat = "To Eat";
public static String restOrEat;
public static void main(String[] args) {
System.out.print("Hello and Welcome to the SOMETHING restaurant. Would you like to eat or use the restroom?");
restOrEat = userInput.next();
if (restOrEat.equalsIgnoreCase(TheRestroom)||restOrEat.equalsIgnoreCase(Restroom)) {
System.out.println("Please go ahead to the restroom, it's over there.");
System.exit(1);
}
if (restOrEat.equalsIgnoreCase(Eat)||restOrEat.equalsIgnoreCase(ToEat)) {
System.out.print("What would you like to eat for starters? Press 1 for Cheese & Bacon, 2 for Sald (With options) and 3 for noodles");
int starter = userInputStarters.nextInt();
}
}
}
Here is the solution for your problem.
Related
import java.util.Scanner;
public class A {
static void aValue1(){
Scanner scan = new Scanner(System.in);
System.out.print("Enter value of a: ");
double numA = scan.nextDouble();
static void aValue2(){
System.out.println(numA);}
public static void main(String[] args) {
aValue1();
aValue2();
}
}
I understand that numA is a local variable and can not be used again in a different method, but is there any way I declare numA as static outside of the method and still be able to get the user input for it?
I want the user to type what numA is and I want to use it in every method.
Yes you can, you could have numA as a static field
import java.util.Scanner;
public class A {
static double numA;
static void aValue1(){
Scanner scan = new Scanner(System.in);
System.out.print("Enter value of a: ");
numA = scan.nextDouble();
static void aValue2(){
System.out.println(numA);}
public static void main(String[] args) {
aValue1();
aValue2();
}
}
import java.util.Scanner;
class BloodData{
static Scanner in = new Scanner(System.in);
static String bloodType;
static String rhFactor;
public BloodData(){
bloodType = "O";
rhFactor = "+";
}
public BloodData(String bt, String rh){
bloodType = bt;
rhFactor = rh;
}
public void display() {
System.out.println(bloodType+rhFactor+" is added to the blood bank");
}
public static void main(String[]args) {
System.out.println("Enter Blood Type(O, A, B, AB)");
System.out.println("Enter rhFactor('+' or '-')");
BloodData bd= new
BloodData(BloodData.bloodType=in.nextLine(),BloodData.rhFactor=in.nextLine());
BloodData bd1= new BloodData();
bd.display();
}
}
How can i use the constructor with 2 String parameters? because when I always run the code the first one only run. I'm only a beginner so hope someone could help because I already watched a lot of Youtube vids and I really didn't know why this happens
Java doesn't use named parameters like this for method or constructors. I would be surprised if a video showed such syntax...
new BloodData(BloodData.bloodType=in.nextLine(),BloodData.rhFactor=in.nextLine())
You need to define String variables on the line above, then pass them into the constructor. You also shouldn't be combining instance constructors with static variables
class BloodData{
private String bloodType;
private String rhFactor;
public BloodData(String type, String rhFactor) {
this.bloodType = type;
this.rhFactor = rhFactor;
}
...
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String x = in.nextLine();
String y = in.nextLine();
BloodData bd = new BloodData(x, y);
}
if you want to call a constructor having two parameters: Use this
BloodData blooddata = new BloodData("A","+");
I am learning Java and currently attempting to combine if statements and multiple class files.
It is a simple I/O program with a twist, if userName = JDoe I want the program to say something other than the standard saying.
From main.java:
import java.util.Scanner;
import java.lang.String;
class main {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
UInput uInput = new UInput();
System.out.println("What is your name: ");
uInput.setName(input.nextLine());
uInput.saying();
}
}
class ifMain {
public static void main(String[] args){
String userName = "JDoe";
if (test.matches("JDoe")) {
System.out.println("You smell!");
} else {
UInput.saying();
}
}
}
From UInput.java:
public class UInput {
private String userName;
public void setName(String name){
userName = name;
}
public String getName(){
return userName;
}
public void saying(){
System.out.printf("Hello %s", getName());
}
}
However, in class ifMain{}, IntelliJ is saying "Variable userName never used", what am I missing?
See comments:
class ifMain {
public static void main(String[] args){
String userName = "JDoe"; // <=== Declared here
if (test.matches("JDoe")) { // <=== Not used here
System.out.println("You smell!");
} else {
UInput.saying();
}
}
}
The local variable userName is never used in the main method of the ifMain class.
You probably meant:
if (test.matches(userName)) {
Side note: The overwhelming convention in Java is that class names start with an uppercase character. So IfMain, not ifMain.
Your program wouldn't even compile in first place. I believe that you are new to Java. But still, look at this code.
class ifMain {//Please change the class name to CamelCase convention
public static void main(String[] args){
String userName = "JDoe";
if (test.matches("JDoe")) {// Compile error. Variable test is not declared.
System.out.println("You smell!");
} else {
UInput.saying();
}
}
}
Are you trying in a notepad and executing it? You can try using eclipse/NetBeans/IntelliJ IDEs in that case to help you better.
I have the object numberlist that i created in create() method and i want to access it so i can use it in the question() method.
Is there another way to do this that I probably missed? Am I messing something up? If not, how should I do this to get the same functionality as below?
private static void create() {
Scanner input = new Scanner(System.in);
int length,offset;
System.out.print("Input the size of the numbers : ");
length = input.nextInt();
System.out.print("Input the Offset : ");
offset = input.nextInt();
NumberList numberlist= new NumberList(length, offset);
}
private static void question(){
Scanner input = new Scanner(System.in);
System.out.print("Please enter a command or type ?: ");
String c = input.nextLine();
if (c.equals("a")){
create();
}else if(c.equals("b")){
numberlist.flip(); \\ error
}else if(c.equals("c")){
numberlist.shuffle(); \\ error
}else if(c.equals("d")){
numberlist.printInfo(); \\ error
}
}
While interesting, both of the answers listed ignored that fact that the questioner is using static methods. Thus, any class or member variable will not be accessible to the method unless they are also declared static, or referenced statically.
This example:
public class MyClass {
public static String xThing;
private static void makeThing() {
String thing = "thing";
xThing = thing;
System.out.println(thing);
}
private static void makeOtherThing() {
String otherThing = "otherThing";
System.out.println(otherThing);
System.out.println(xThing);
}
public static void main(String args[]) {
makeThing();
makeOtherThing();
}
}
Will work, however, it would be better if it was more like this...
public class MyClass {
private String xThing;
public void makeThing() {
String thing = "thing";
xThing = thing;
System.out.println(thing);
}
public void makeOtherThing() {
String otherThing = "otherThing";
System.out.println(otherThing);
System.out.println(xThing);
}
public static void main(String args[]) {
MyClass myObject = new MyClass();
myObject.makeThing();
myObject.makeOtherThing();
}
}
You would have to make it a class variable. Instead of defining and initializing it in the create() function, define it in the class and initialize it in the create() function.
public class SomeClass {
NumberList numberlist; // Definition
....
Then in your create() function just say:
numberlist= new NumberList(length, offset); // Initialization
Declare numberList outside your methods like this:
NumberList numberList;
Then inside create() use this to initialise it:
numberList = new NumberList(length, offset);
This means you can access it from any methods in this class.
public class QuestionBank {
public static void main(String[] args) {
int k = 0;
String Bank[][] = {{"The sun is hot.","A. True","B. Flase","A"},
{"Cats can fly.","A. True","B. False","B"}};
}
}
Above is my QuestionBank class that creates a 2X4 string array. First column being the question, 2nd and 3rd being the answer choices, and 4th being the correct answer.
Below is my RealDeal class.
import javax.swing.JOptionPane;
import java.util.Scanner;
public class RealDeal {
public static void main(String[] args) {
input = JOptionPane.showInputDialog(Bank[0][0]\nBank[0][1]\nBank[0][2]);
if (input == Bank[0][3]) {
input = 10;
} else {
input = 0;
}
total = input/1;
JOptionPane.showMessageDialog(null,"You scored a " + total + " out of 10. Great job!");
System.exit(0);
}
}
What I'm trying to do is to get Bank[0][0], Bank[0][1], and Bank[0][2] to output on my RealDeal class and then to check whether Bank[0][3] matches with the users input. Can anyone please help me with this. Im really new to java so if anyone could actually draw out the answer and explain it to me that would be great.
I think the best way is reading a good Java book and become familiar with the language itself and then try to solve this by your own. If you then have a real question there is no problem asking it here again. But your code is... not really working at all.
I don't think this portal is a "please do my work for me" portal.
To call anything from another class you will need to either setup a method for a return or make the variables public.
So:
public class Class1
{
// for method 1
public String s1 = "This is a string"
// for method 2
public Class1 {}
public returnString()
{
return s1;
}
}
public class CLASS2
{
public static void main(String args[])
{
// get the class
cls1 = new Class1();
// retrieving - method 1
String str = cls1.s1;
// retrieving - method2
str = cls1.returnString();
}
}