-1
public class House extends Device {

static final int ADD = 'a';
static final int SHOW = 's';
static final int ONOFF = 'o';
static final int QUIT = 'q';


public void main() {

    Scanner in = new Scanner(System.in);
    Device theDevice = new Device();
    while (true) {
        System.out.print("(a)dd, (s)how, (o)n/off, (q)uit: ");
        char input = in.next().toLowerCase().charAt(0);
        if (input == ADD) {
            System.out.print("Device name: ");
            String deviceName = in.nextLine();
            theDevice.addDevice(deviceName);
        }
    }
}
}

class Device {

List<String> deviceName = new ArrayList<String>();
List<Boolean> deviceStatus = new ArrayList<Boolean>();
List<Long> deviceOnTime = new ArrayList<Long>();

void addDevice(String deviceName) {
    this.deviceName.add(deviceName);
    this.deviceStatus.add(false);
    this.deviceOnTime.add(0L);
}
}

I put my code like this and when executed it was shown like this

(a)dd, (s)how, (o)n/off, (q)uit: a Device name: (a)dd, (s)how, (o)n/off, (q)uit:

it's not waiting for me to input something like it suppose to be. How can I fix it.

Thx a lot.

Pakanon Pantisawat
  • 105
  • 1
  • 1
  • 7
  • Do you mean your program exits without taking any input and without any exception?Could you explain *it's not waiting for me to input something like it suppose to be.* – Naman Sep 18 '17 at 09:31
  • 2
    Possible duplicate of [Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) – Karl Reid Sep 18 '17 at 09:33

2 Answers2

0

Your main method does not have the correct format: in Java, a main method must be declared like this:

public static void main(String[] args) {
}

If you change that, it will work.

Another thing is -indeed- that you should use

in.nextLine()

to consume the line break.

JensS
  • 1,093
  • 2
  • 12
  • 18
  • 1
    But then where do you think did the *when executed it was shown like this (a)dd, (s)how, (o)n/off, (q)uit: a Device name: (a)dd, (s)how, (o)n/off, (q)uit:* in the question came from? – Naman Sep 18 '17 at 09:33
0

When you call in.next() it takes the next value ignoring the return character '\n'. Then you call in.nextLine() and it takes the already existing line and skips. To solve this use in.nextLine() instead of in.next() and trim the result, or in a dirty way call in.nextLine() two consecutive times.