Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are the or pipes not working?

For my very first Java project I'm trying to make a simple text-based game. It seems like the or pipes do not work and when I try to enter something in after it doesn't work; I have to restart it.

    public void GameStart() {
    String playerName = "Player";
    String Command = "Choice";
        System.out.println("Type Start to start game!");
    if(in.nextLine().equals("Start") || in.nextLine().equals("start")) {
        Help();
        print("Type Begin to start game :D");
        if(in.nextLine().equals("Begin") || in.nextLine().equals("begin")) {
            System.out.println("Who are you?");

Start and Begin works, but having the first letter lowercase like I used || for doesn't. When I use those I have to restart it because I can't type anything, but I clearly have or pipes that says to use either one. Does anyone have any idea what is causing this?

like image 402
Melon Avatar asked Aug 05 '15 20:08

Melon


People also ask

What is the most common plumbing problem?

The most common plumbing problems are clogged drains and toilets, leaky faucets and pipes, water heater issues, low water pressure, and a running toilet.

How do you know if you have a plumbing problem?

Slow Drains It can be caused by a hair clog or food and can be easy to fix. However, when multiple drains are slow, that can be a sign of a deeper plumbing problem. Slow drains are one of the first signs of a problem somewhere in your system, and left alone could lead to bigger issues.

What are the advantages of the plastic pipes over the iron pipes?

They are less affected by ground movements and are strong enough to withstand deformation from traffic loading. Their inherent resistance to corrosion also makes them less vulnerable to leaks originating inside the pipe. Plastic pipes also require fewer joints than those made from concrete, iron or metal.


2 Answers

in.nextLine().equals("Start") || in.nextLine().equals("start")

This will be executed from left to right, so first in.nextLine().equals("Start") is executed.

Let's say you input "start". Left side returns false. Evaluation of the whole expression continues. The right side is executed, in.nextLine().equals("start"). So, it reads another line, waits for your another input. This is not what you want.

To fix this (this is also a general rule): do not change the state in the if condition. In other words: expression in the condition should have no side effects.

You can assign the result of the method which is changing the state, in.nextLine(), to a reference:

String line = in.nextLine();
if (line.equals("Start") || line.equals("start"))

You can also write the condition like:

if (line.equalsIgnoreCase("start"))

or

if (line.toLowerCase().equals("start"))

Now, any case combination is alright, even "START".

Last but not least: these || are not called pipes. If you use word "pipe" in programming context, a lot of people will think about Unix pipes, | symbol in the command line.

In Java || is "logical or operator", "logical or". To distinguish with "binary or", |, which is very different.

like image 51
Adam Stelmaszczyk Avatar answered Sep 20 '22 12:09

Adam Stelmaszczyk


String str = in.nextLine();
str.equalsIgnoreCase("Start");

It will check both the lower and upper cases. You don't need to call equals() method twice, so it will optimize your code and readability.

I hope this will solve your issue.

like image 35
Ankur Mahajan Avatar answered Sep 18 '22 12:09

Ankur Mahajan