This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-02-14 22:08:44

ActionboyX
Scratcher
Registered: 2011-09-01
Posts: 10

Java Programing Help

Hi,
I made a program in java here is the code:


import  java.util.Scanner;

class apples{
    public static void main(String args[]) {
        Scanner x = new Scanner(System.in);
       
        int bucky = 6;
       
        System.out.println("Try to Guess What Number I am.");
        if (bucky == x){
            System.out.println("Congradulations, you guessed it.");
        }else{
            if (bucky >= x){
            System.out.println("Your Wrong Try Again. The Number is > the number you guessed.");
            }else{
                if (bucky <= x){
                    System.out.println("Your Wrong Try Again. The Number is < the number you guessed.");

                }
            }
        }
    }
}



I am working with eclipse and it gives me this error:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Incompatible operand types int and Scanner
    The operator >= is undefined for the argument type(s) int, Scanner
    The operator <= is undefined for the argument type(s) int, Scanner

    at apples.main(apples.java:10)

I get that my code is wrong at line 10, but I don't know how to fix it. I am new to this. Please Help!

Offline

 

#2 2012-02-14 22:42:29

ActionboyX
Scratcher
Registered: 2011-09-01
Posts: 10

Re: Java Programing Help

Hello Again,
I edited my program and it still doesn't work help.

import  java.util.Scanner;

class apples{
    public static void main(String args[]) {
        Scanner x = new Scanner(System.in);
       
        int num = 6;
       
        System.out.println("Try to Guess What Number I am.");
        switch (x){
        case (x == num): 
            System.out.println("Congradulations, you guessed it.")
            brake;
        case (x <= num):    
        System.out.println("Your Wrong Try Again. The Number is < the number you guessed.");
        case (x >= num):
            System.out.println("Your Wrong Try Again. The Number is > the number you guessed.");
        }
    }
}

I finally understand that I can't do this with the scanner, but is there another way to do this? Please Help. Thanks!

Offline

 

#3 2012-02-15 00:23:24

slinger
Scratcher
Registered: 2011-06-21
Posts: 1000+

Re: Java Programing Help

Maybe in the java official topic?


http://s0.bcbits.com/img/buttons/bandcamp_130x27_blue.png

Offline

 

#4 2012-02-15 16:55:50

Harakou
Community Moderator
Registered: 2009-10-11
Posts: 1000+

Re: Java Programing Help

x itself isn't an integer value - it's a reference to the object of the Scanner class that you created. So naturally, you can't check its value against that of an integer, because it doesn't have a numerical value in that sense.

Instead, what you want is a method from the Scanner class that takes the next parseable integer - that is, the next integer entered by the user that it can find. What you want to do is to create a variable of type int and make it equal to "x.nextInt()", then check that against the num variable.

I hope that makes sense. I'm not terribly good at explaining things. :S


http://www.blocks.scratchr.org/API.php?action=random&amp;return=image&amp;link1=http://i.imgur.com/OZn2RD3.png&amp;link2=http://i.imgur.com/duzaGTB.png&amp;link3=http://i.imgur.com/CrDGvvZ.png&amp;link4=http://i.imgur.com/POEpQyZ.png&amp;link5=http://i.imgur.com/ZKJF8ac.png

Offline

 

#5 2012-02-17 21:36:33

16Skittles
Scratcher
Registered: 2009-08-26
Posts: 1000+

Re: Java Programing Help

Try heading over to the official topic. However, since I am the owner of the official topic, I'll try to help you here. Actually, Harakou is right, and he already answered it. I went through a whole long thing about this about how to get a string, then realizing that it was completely unnecessary XD


http://16skittles.tk/sig.png
Are you a student? Check out OnSchedule!

Offline

 

#6 2012-02-17 22:04:44

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: Java Programing Help

16Skittles wrote:

Try heading over to the official topic. However, since I am the owner of the official topic, I'll try to help you here. Actually, Harakou is right, and he already answered it. I went through a whole long thing about this about how to get a string, then realizing that it was completely unnecessary XD

http://scratch.mit.edu/forums/viewtopic.php?id=88581

Offline

 

Board footer