Breaking a do..while loop with a keypress - java

I just want continue a program when a user presses the y key with a do while loop in this code e.g. while('o'!=='y'). I have also tried this:
public class clac {
do {
System.out.println("\nwant to try again\n press y/n");
String o;
o=sc.next();
} while(!o.contentEquals("y"));
}

use below code if you want to ask question on "y" input :
public static void main(String r[]) throws IOException {
Scanner sc = new Scanner(System.in);
String o;
do {
System.out.println("\nwant to try again\n press y/n");
o=sc.next();
} while(!o.contentEquals("y"));
}
Hope that it is what you are looking for.

Related

User input is not calling method (Java)

When I run my program and type user input as (A-F) the program does nothing. I'm trying to figure out why when I enter any of the letters A through Z the program doesn't call the method. If I input one of the numbers they will run perfectly fine and call the methods, but the letters are giving me problems for some reason.
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String userInput;
initializeShipList(); // initial ships
initializeCruiseList(); // initial cruises
initializePassengerList(); // initial passengers
// add loop and code here that accepts and validates user input
// and takes the appropriate action. include appropriate
// user feedback and redisplay the menu as needed
do {
displayMenu();
userInput = scnr.nextLine();
if(userInput.equals("1")) {
addShip();
}
if(userInput.equals("2")) {
editShip();
}
if(userInput.equals("3")) {
addCruise();
}
if(userInput.equals("4")) {
editCruise();
}
if(userInput.equals("5")) {
addPassenger();
}
if(userInput.equals("6")) {
editPassenger();
}
if(userInput.toUpperCase().equals("A")) {
printShipList("active");
}
if(userInput.toUpperCase().equals("B")) {
printShipList("name");
}
if(userInput.toUpperCase().equals("C")) {
printShipList("full");
}
if(userInput.toUpperCase().equals("D")) {
printCruiseList("list");
}
if(userInput.toUpperCase().equals("E")) {
printCruiseList("details");
}
if(userInput.toUpperCase().equals("F")) {
printPassengerList();
}
}while(userInput != "x");
}

Java Scanner how to input string if function

I am very new in programming into Java.
My question is that I have a code (see below) and I want to compare them with if statement. An errors occur at line 9 (string test) and 11(if(test.equals). I completely do not have idea.
I have made a code with int and it works perfect, but that.
package bucky;
import java.util.Scanner;
class apples {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
Scanner sc = new Scanner(System.in);
String test = sc.nextLine();
if (test.equals("YES")) {
System.out.println("YES");
} else {
System.out.println("TIS IS ELSE");
}
}
}
You are almost there... define YES as string and that it
String test = sc.nextLine();
String YES = "yes";
if (test.equals(YES)) {
or even better use equalsIgnoreCase() so you can get rid off the case sensitive input
if (test.equalsIgnorecase(YES))

How do I make a method work in my class in Java [duplicate]

This question already has answers here:
How to call a method in another class of the same package?
(8 answers)
Closed 7 years ago.
Hi I have a robot which I need to tell it to move a certain number of times forward by saying forward 5. I have the method, I just need to get it to work in my class. Here is the method:
public void moveNumOfTimes(int num)
{
//-----------------------------------------------------------------------------------------------
int i=0;
while(i<num) {
if (this.frontIsClear()){ // if the front is NOT clear the robot should not move, otherwise will collide into the wall
this.move();
}
i++; // same as i=i+1;
}
//-----------------------------------------------------------------------------------------------
}
How do I enter that in my program? Is it like this?
moveNumOfTimes(int num);
Hope someone can help. Thanks
You should use a Scanner object to take input from the console.
Scanner sc = new Scanner(System.in);
You can implement like this.
class Robot{
public static void main(String args[]){
Scanner sc = new Scanner();
int numOfSteps = sc.nextInt(); //This line takes a integer input from the user
YourClassName r = new YourClassName();
//DO any initialization operations here
r.moveNumOfTimes(numOfSteps);
//Post movement operations come here
}
You can learn more on scanner here
How do I enter that in my program? Is it like this?
moveNumOfTimes(int num);
Yes, you could use something similar to this if you were trying to pass a command in from another method call. Something like:
public void Control(int moveNumber) {
... some other code, do some other stuff...
moveNumOfTimes(moveNumber); //Here you are passing a parameter value to your original method;
}
Or you could control it directly with another method like:
public void moveFive() {
moveNumOfTimes(5);
}
More likely, however, you wouldn't want to hardcode a method but rather call your original method directly through your Main method.
public static void main(String [ ] args) {
Robot r = new Robot();
r.moveNumOfTimes(5); //Here you have moved your new robot!
}
And if you really want to get fancy, look into working with the System and Scanner classes so you can prompt your user to tell the robot how much to move:
public static void main(String [ ] args) {
Robot r = new Robot();
Scanner reader = new Scanner(System.in);
System.out.println("How far should the robot move?"); //Output to the console window
int input = reader.nextInt(); //Reads the next int value
r.moveNumOfTimes(input); //Calls your method using the scanned input
}

Java - Method structure execution

I am going through the University of Helsinki's Java course and I have a question on one of the examples.
The code in question:
import java.util.Scanner;
public class UserInterface {
private Scanner reader;
public UserInterface(Scanner reader) {
this.reader = reader;
}
public void start() {
while (true) {
String command = reader.nextLine();
if (command.equals("end")) {
break;
} else {
handleCommand(command);
}
}
}
public void handleCommand(String command) {
if (command.equals("buy")) {
String input = readInput("What to buy: ");
System.out.println("Bought!");
} else if (command.equals("sell")) {
String input = readInput("What to sell: ");
System.out.println("Sold!");
}
}
public String readInput(String question) {
while (true) {
System.out.print(question);
String line = reader.nextLine();
if (line.equals("carrot")) {
return line;
} else {
System.out.println("Item not found!");
}
}
}
}
If you choose to buy or sell something that isn't a carrot why does it not run the line directly below the input line in the handleCommand method (printing Bought! or Sold!)? I don't understand how it terminates the conditional in the case that a carrot is not bought or sold. How is the readInput method manipulating the handleCommand method here? Thanks.
The function readInput() is rather oddly defined: it will only return if you enter carrot.
readInput() contains a while loop that keeps looping until the user enters carrot, otherwise it says Item not found! and tries again. The output lines in handleCommand() are only executed when readInput() returns.
Basically your readInput() method returns the String "line" only if the user inputs "carrot" otherwise it will keep on going inside the else part and continue printing "Item not found!". But instead of System.out.println("Item not found!") ,
if you just return that statement then either "carrot" or "Item not found!" will be returned and stored in the "input" variable of handleCommand() method. In that case now it will print your "Bought!" or "Sold!" print statement irrespective whether you choose "carrot" or something else

Mixed up generated replies with if statements

I was doing some programming (self taught) and I have been struggling with an else statement but I fixed it. Now when I run it, it runs the wrong reply.. If that makes sense. I hope I am clear enough for you to help!w
import java.util.Scanner;
public class Test {
private static Scanner scanner;
public static void main(String[] args) {
// TODO Auto-generated method stub
String answer;
scanner = new Scanner(System.in);
System.out.println("Hello Human");
System.out.println("Do you want to build a snowman?");
answer = scanner.next();
if (answer.equals("Yes"))
System.out.println("Yay, Now you must think hard of what Olaf Looks like Okay? (say okay to coninue)");
{
else
System.out.println("Go away! I hate you"); //This happens when I try to say Yes.. This is meant for no.
}
}
}
You provided the wrong braces for if-else block. Please check the below code.
You can use equalsIgnoreCase() for case in-sensitivity for the user input.
import java.util.Scanner;
public class Test {
private static Scanner scanner;
public static void main(String[] args) {
// TODO Auto-generated method stub
String answer;
scanner = new Scanner(System.in);
System.out.println("Hello Human");
System.out.println("Do you want to build a snowman?");
answer = scanner.next();
if (answer.equalsIgnoreCase("Yes")) {
System.out.println("Yay, Now you must think hard of what Olaf Looks like Okay? (say okay to coninue)");
}
else {
System.out.println("Go away! I hate you"); //This happens when I try to say Yes.. This is meant for no.
}
}
}
PS: The if andelse` block here, are having only single statements, you if you want then you can remove them completely. But you have more than one then you must use the brackets.
The position of your brace: { is wrong. The message doesn't fall inside the if block and it'll always get printed. It should be as below,
import java.util.Scanner;
public class Test {
private static Scanner scanner;
public static void main(String[] args) {
// TODO Auto-generated method stub
String answer;
scanner = new Scanner(System.in);
System.out.println("Hello Human");
System.out.println("Do you want to build a snowman?");
answer = scanner.next();
if (answer.equals("Yes")) {
System.out.println("Yay, Now you must think hard of what Olaf Looks like Okay? (say okay to coninue)");
{ else
System.out.println("Go away! I hate you"); //This happens when I try to say Yes.. This is meant for no.
}
}
}

Categories

Resources