Jump to content

Scannar problem please help

Featured Replies

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");
         }
        
        
        
     }      
        
 }
  • Author

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

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

 

  • Author

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!

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

Archived

This topic is now archived and is closed to further replies.

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.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.