Jump to content
Sign in to follow this  
Jay001

Scannar problem please help

Recommended Posts

which part did I do wrong with my code?
 
 
import java.util.Scanner;

public class program {
     public static void main(String[] args) {
      int x;
      Scanner scan = new Scanner(System.in);
       
        
         System.out.println("please type your score");
         x=scan.nextLine();
        
         if (x>90) {
          System.out.println("you got an A");
         }
        
        
        
     }      
        
 }

Share this post


Link to post
Share on other sites

I tried but is still not working

import java.util.Scanner;

public class program {
     public static void main(String[] args) {
      string score;
      Scanner scan = new Scanner(System.in);
       
        
         System.out.println("please type your score");
         score=scan.nextLine();
        
         if (score>90) {
          System.out.println("you got an A");
         }
        
        
        
     }      
        
 }

it shows > undefine

Edited by Jay001

Share this post


Link to post
Share on other sites

If you have String (not string) object, you have to convert it to integer. You used comparison operator > (greater than) on String object.

What methods and operators are available for String you can obviously check in Oracle Java docs:

https://docs.oracle.com/javase/7/docs/api/java/lang/String.html

 

How to convert string to integer:

https://stackoverflow.com/questions/5585779/how-do-i-convert-a-string-to-an-int-in-java

 

You should also read entire Scanner class description:

https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

 

Share this post


Link to post
Share on other sites

I am a rookie and I am learning Java on my own, so could you please fix my code. Then,I will understand more. Thank you so much!

Share this post


Link to post
Share on other sites

I gave you references to read. You didn't do it. I won't write so simple code for you. You won't learn anything this way. If you would read links that I gave, instantly you would notice there are special methods designed for your job, instead of nextLine()..

Reading documentation and learning what classes have what methods with what parameters is majority of programmer's time spend on project.

Built-in classes and methods you must learn (and remember!) by reading docs.

https://docs.oracle.com/javase/7/docs/api/overview-summary.html

java.lang , java.io, java.util, java.text, java.applet , java.math etc. etc. This is what you must read the all (at least). java.sql, java.net and java.awt won't hurt later. Print them (description of the all classes with methods), and read them in WC, metro, or bus etc. etc. in your free time. Make little test programs to check how to use methods that you just read about.

 

Edited by Sensei

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.