Results 1 to 5 of 5
Thread: Basic Scanner API
- 02-03-2011, 01:19 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
Basic Scanner API
This simple program is printing "input 2" even if you type "yes". Why?
I know it's very basic, but I started reading some java tutorials about 2 days ago so I don't know much about it.
import java.util.Scanner;
class test {
public static void main(String[] args) {
System.out.println("Input something");
Scanner output = new Scanner(System.in);
String anw = output.nextLine();
if ( anw == "yes" )
System.out.println("input 1");
else
System.out.println("input 2");
}
}Last edited by guisome; 02-03-2011 at 01:22 AM.
- 02-03-2011, 01:24 AM #2
Never compare Strings or any other object with == use the equals method instead.
Never say never. Ok there are some occassions when you do need to use == but when it comes to Strings and you want to compare contents then use the equals method.
- 02-03-2011, 01:33 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
Using the "equals" method the java points out :
test:java:9: incompatible types
found: java.lang.String
required: boolean
if ( anw = "yes" )
- 02-03-2011, 01:48 AM #4
You didn't use the equals method. All you did is delete one = sign. That changes it from a comparison of equality to an assignment. Do you know what a method is and how to call it?
- 02-03-2011, 01:57 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Need some basic help
By backdown in forum New To JavaReplies: 29Last Post: 01-20-2011, 05:06 AM -
Basic GUI help
By vahshir in forum New To JavaReplies: 5Last Post: 10-11-2010, 02:33 AM -
Need really basic help!
By anthonyalaan in forum New To JavaReplies: 9Last Post: 09-12-2010, 02:43 PM -
Basic Help
By robjames in forum New To JavaReplies: 3Last Post: 02-09-2009, 03:58 AM -
Need some really basic help
By Mayur in forum New To JavaReplies: 6Last Post: 01-24-2009, 07:00 AM
Bookmarks