-1

so, for an assignment, I have to do this part, but the if parts are red and i don't understand why

if (int >=1 && int<=7){
    Scanner sc = new Scanner(System.in);
    String direction;
    System.out.println("Enter your choice: ");
    direction=sc.nextD();
    if (direction = 'left'){
        maze.push('left');
        System.out.println("Pushed left to stack");

I realize I forgot part of it. updated code:

Stack maze = new Stack();
for(int x=1; x<=10; x++)
{
    Random ran = new Random();
    int a = ran.nextInt(10) + 1;
    if (int a>=1 && int a<=7){
        Scanner sc = new Scanner(System.in);
        String direction;
        System.out.println("Enter your choice: ");
        direction=sc.nextLine();
        if (direction.equals ("left") || direction.equals ("Left")){
            maze.push("left");

the if (int a>=1 && int a<=7) part doesn't work. everything else is fine

Chiaki29
  • 1
  • 1
  • That's the compiler telling you that something is wrong with your code. I'm not a compiler, but off the top of my head `'left'` is not a `String`, in Java strings are denoted with double quotes: `"left"`. – azurefrog Feb 24 '17 at 19:15
  • 2
    Also, `if (direction = "left")` is assignment, not comparison. Also, also, `if (direction == "left")` is comparing reference equality and will also fail. See [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – azurefrog Feb 24 '17 at 19:17
  • Replace `if (int a>=1 && int a<=7){` with `if (a >= 1 && a <=7) {` `int a` will try to declare a new integer called a. – alexgbelov Feb 24 '17 at 19:51
  • thanks. it works now – Chiaki29 Feb 24 '17 at 20:01
  • Great! Just a tip for the future: when writing code, make sure to fix the errors as they come up, instead of at the end. This will save you a lot of time in the future. – alexgbelov Feb 24 '17 at 21:19

3 Answers3

1

So, here are some issues that I see:

  1. int is not a variable; it is the name of a primitive type. You cannot name any variable as int in java, and you certainly can't do comparisons to it.

  2. Scanner does not have a class called nextD(). You're probably looking for nextLine

  3. In Java, strings are enclosed in double quotes ", not single quotes '. In addition, to compare strings you need to use direction.equals("string"). See azurefrog's comment.

I'm assuming you're using an IDE like Eclipse or IntelliJ (if you're not, you probably should). Hover over the red parts with your mouse and it should tell you what its complaining about.

alexgbelov
  • 3,032
  • 4
  • 28
  • 42
0

there is a variable a of type int do it like if(a >=1 && a <=7)

prashant dhuri
  • 51
  • 1
  • 2
  • 11
-2

Here is the correction for your code :-

int i=0;
int b=0;
if (i>=1 && b<=7){ /*you need to have variables for your int and u must 
define them before the if statment*/ 
Scanner sc = new Scanner(System.in);
String direction;
System.out.println("Enter your choice: ");
direction=sc.next(); /*no D for Strings and .nextLine() if its separated by 
spaces*/.
if (direction.equals("left")) { /*String uses .equals() method inside for  
comparing equal strings*/.
    maze.push("left"); //String uses literals " "
    System.out.println("Pushed left to stack");
}}
Omar Boshra
  • 447
  • 7
  • 21