-4

im trying to make a small program that asks for a number between 1-10 and then gives out a different prewritten response. this is what i have so far.

import java.util.Scanner;
public class AlasFutureTeller
{
        public static void main(String [ ] args)
    {
        System.out.println("I CAN READ YOUR FUTURE!");
        System.out.println("TYPE A NUMBER BETWEEN 1 AND 10 TO MEET YOUR FATE!!");
        Scanner oneTen = new Scanner (System.in);
        int one == oneTen;
        if (oneTen=="one")
            System.out.println("YOU WILL GO TO SLEEP TODAY!!");
        else if ( oneTen=="two")
            System.out.println("YOU WILL HAVE A BIRTHDAY EVERY YEAR UNTIL YOU DIE!!");
        else if ( oneTen=="three")
            System.out.println("YOU WILL SHOWER SOMETIME IN THIS YEAR!!");
        else if ( oneTen=="four")
            System.out.println("YOU WILL LIVE UNTIL YOU THE DAY YOU DIE!!");
    }
}
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179

2 Answers2

0

You can't compare Strings to ints silly goose!

if (oneTen=="one")

Try:

if (oneTen.nextInt() == 1)
Tdorno
  • 1,571
  • 4
  • 22
  • 32
-1
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        System.out.println("I CAN READ YOUR FUTURE!");
        System.out.println("TYPE A NUMBER BETWEEN 1 AND 10 TO MEET YOUR FATE!!");

        Integer number = new Scanner (System.in).nextInt();

        if (number.equals(1))
            System.out.println("YOU WILL GO TO SLEEP TODAY!!");
        else if (number.equals(2))
            System.out.println("YOU WILL HAVE A BIRTHDAY EVERY YEAR UNTIL YOU DIE!!");
        else if (number.equals(3))
            System.out.println("YOU WILL SHOWER SOMETIME IN THIS YEAR!!");
        else if ( number.equals(4))
            System.out.println("YOU WILL LIVE UNTIL YOU THE DAY YOU DIE!!");

    }

}

You can change the type of variable number to 'int' and change the compare mode to

if(number == 1)...

You can choose what do you like more. But for now you need understand that int is a primitive type and Integer is a wrapper of this primitive type that give to you an object that represent the primitive type.

  • 1
    Why would he need to extend past primitive types for this simple problem? – Tdorno Sep 08 '14 at 00:56
  • ​Java is OO Language... Why should i use primitive variables? Ok... have cases that i need use it but at almost all cases i don't need it.​ An person that is beginning at language could be confused the symbols '=' and '=='. If i use primitive variable i need compare with '==' when i use 'Wrapper' i could use .equals and it is very better to understand. – Gabriel Cardelli Sep 08 '14 at 01:10
  • I'm pretty sure any and every CS course (that deals with Java) forbids students to stray away from primitive types, kind of contradicts what you're saying. – Tdorno Sep 08 '14 at 01:22
  • Do you prefer work with primitive types or wrapper types? – Gabriel Cardelli Sep 08 '14 at 01:58
  • Depends on what I'm doing. – Tdorno Sep 08 '14 at 01:59