Jay001 Posted September 2, 2018 Posted September 2, 2018 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"); } } }
Sensei Posted September 2, 2018 Posted September 2, 2018 java.util.Scanner.nextLine() method is returning String object, not integer.. https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine()
Jay001 Posted September 2, 2018 Author Posted September 2, 2018 (edited) 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 September 2, 2018 by Jay001
Sensei Posted September 2, 2018 Posted September 2, 2018 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
Jay001 Posted September 2, 2018 Author Posted September 2, 2018 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!
Sensei Posted September 3, 2018 Posted September 3, 2018 (edited) 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 September 3, 2018 by Sensei
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now