Mixed up generated replies with if statements - java

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.
}
}
}

Related

Breaking a do..while loop with a keypress

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.

scan error : the value of local variable scan is not used

import java.util.Scanner;
public class KillBill {
public KillBill() {
// TODO Auto-generated constructor stub
}
public static void Main(String[] args) {
// TODO Auto-generated method stub
Scanner Scan = new Scanner (System.in);
}
}
IN 11th LINE .IT SAYS THE VALUE OF LOCAL VARIABLE SCAN IS NOT USED
If a variable is set and not used, it gives an error. You could correct the warning by using Scan, with something like:
public static void Main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner (System.in);
String text = scan.nextLine();
System.out.println(text);
}
The idea here is that every variable is being used now.
Well, to be entirely honest, you're Scanner object ISN'T BEING USED elsewhere in the code you've provided. But don't worry, this is just a warning (yellow jagged underline) and not an error (red jagged underline). In this case, it's just telling you that your program, at the moment, has an unused variable, whose deletion won't affect the code than what it currently is. Remember, a computer still has to read each line, and reading unused variables, especially objects, might slow your program down, even though by only a fraction of a millisecond. So it is advised to delete this unnecessary lines of code.
But remember, this is all relevant for the CURRENT situation, and not if you're gonna make changes to your program, by literally USING your unused variable.
Try this.
public class KillBill {
public KillBill(Scanner scan) {
// TODO Auto-generated constructor stub
int value = scan.nextInt();
System.out.println(value);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner Scan = new Scanner (System.in);
new KillBill(Scan);
}
}
That problem/warning is caused by unused variable. Not a serious issue.
This is not a problem to declare Scanner and not use it, the real problem is the signature of your main method signature, it should not UpperCase you have to use :
public static void main(String[] args) {
// ^^-----------------In your program it is M
Note java use CamelCase your names of variables shold start with lower case for the good practice (Scan should be scan)

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))

print out each character in an input

I've been teaching myself Java with http://www.cs.princeton.edu/courses/archive/spr15/cos126/lectures.html as a reference. They have a library called algs4 and it has several classes including StdIn, which I'm trying to implement below.
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
public class Tired
{
public static void main(String[] args)
{
//I thought this while statement will ask for an input
//and if an input is provided, it would spell out each character
while (!StdIn.hasNextChar()) {
StdOut.print(1); //seeing if it gets past the while conditional
char c = StdIn.readChar();
StdOut.print(c);
}
}
}
//This is from StdIn class. It has a method called hasNextChar() as shown below.
/*
public static boolean hasNextChar() {
scanner.useDelimiter(EMPTY_PATTERN);
boolean result = scanner.hasNext();
scanner.useDelimiter(WHITESPACE_PATTERN);
return result;
}
*/
If i run the code, it does ask for an input, but regardless of what i type in, nothing happens and nothing gets printed out.
I see that even StdOut.print(1); doesnt get printed out, so for some reason, it just gets stuck on while
It looks like the issue is with the condition for your while loop:
!StdIn.hasNextChar()
This says to continue as long as there isn't a next char. But you want to continue while there is one, so get rid of that ! and you should be good.
Here is some alternative code that works similarly. Not the best coding but works.
import java.util.Scanner;
public class test{
static Scanner StdIn = new Scanner(System.in);
static String input;
public static void main(String[] args){
while(true){
if(input.charAt(0) == '!'){ // use ! to break the loop
break;
}else{
input = StdIn.next(); // store your input
System.out.println(input); // look at your input
}
}
}
}

Make Main Method restart/refresh

My code so far:
import java.util.*;
import java.util.Scanner.*;
public class Project{ // The Main Method
public static void main(String [] args){ // Creates the Main Method
System.out.println("Name a Method (Stability, efficiency ..)"); // Asks the user to select a method
Scanner scan = new Scanner(System.in); // Creates the Scanner
String splash = scan.nextLine(); // Transitions the user to the next line after choosing a method
if(splash.equals("efficiency")) // If users chooses Efficiency then it goes to the Efficiency method
{
efficiency(); // Calls the Efficiency method
}
if(splash.equals("Stability")) // If user chooses Stability then it goes to the Stability Method
{
stable(); // Calls the Stability method
}
else // What happens if the input wasnt recognized
{
System.out.println("I don't recognize this"); // what happens if an irrelevant method is chosen
}
}
}
How would I make it so that instead of:
else // What happens if the input wasnt recognized
{
System.out.println("I don't recognize this"); // what happens if an irrelevant method is chosen
}
It will refresh or restart the main method?
Wrap your code in a while loop which you leave when the user chooses the exit command:
public static void main(String [] args){
while (true) {
System.out.println("Name a Method (Stability, efficiency ..)");
Scanner scan = new Scanner(System.in);
String splash = scan.nextLine();
if (splash.equals("exit")) {
break;
} // else if (splash.equals("efficiency")) ...
}
}

Categories

Resources