aspose file tools*
The moose likes Beginning Java and the fly likes Help with Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with "scan.next() incompatible types"" Watch "Help with "scan.next() incompatible types"" New topic
Author

Help with "scan.next() incompatible types"

Brian Mart
Ranch Hand

Joined: Apr 17, 2012
Posts: 34
Hi,
New here, great place. I will pay back the knowledge once I am capable. But keeping it short, here is my code:


I know it is a simple problem but I am having a problem trying to make it a compatible type. I need to create a program that asks for the date, and then asks if the user would like to see the previous or next day. Thank you in advance.
Mahesh Murugaiyan
Greenhorn

Joined: Jun 25, 2009
Posts: 21
Hi Brian,

You can add a explicit type cast which will covert the string into a "double" or just do a scanDouble().




- Mahesh
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Welcome to the Ranch

Don’t use the == operator to compare reference types. It won’t do what you want.
Your block is self-contradictory. You are asking whether the user wants something, then calling System.exit(). Beware of System.exit(); it is vicious and can (in more complicated circumstances) cause file corruption.
You need to go through your if blocks and see under which circumstances they are likely to be entered (I can see one which can never be entered).
You need to get rid of all those == operators and the exit calls. Also indent your code properly; you can get all sorts of errors because of misplaced { and }, but you have two }s on the same line.
What is response? Where did you declare it? What are you using s1 for?
You have three Scanner objects, and are only using one. You can get rid of the other two.
Sorry to have so much to say about your code. But the other advice you have been given is likely to be unhelpful, I am afraid.
Brian Mart
Ranch Hand

Joined: Apr 17, 2012
Posts: 34
Thanks for the help so far, but I am having new problems. The compiler says that there are no syntax errors, but when I run the program and get to the "next or previous" prompt, the system will always output "Please enter'next' or 'previous'" even if I type in next or previous Anyone know what is happening here? Here is my code:

Brian Mart
Ranch Hand

Joined: Apr 17, 2012
Posts: 34
And using == operators is a part of the chapter of the book I am learning. Is there an alternative/better method?
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
For comparing Strings, use the String equals() method.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Brian Mart wrote:And using == operators is a part of the chapter of the book I am learning. Is there an alternative/better method?
There is a right method, which you have already been told: the equals() method. Does your book really tell you to use == for Strings? Are you sure you have read that bit correctly? We have an explanation of the strange behaviour of Strings: here.
Vince Stout
Greenhorn

Joined: Apr 13, 2012
Posts: 19
You still need to look closer at your if statement on line 20. As stated earlier, it can never be true. And even if it could, you are not doing anything to correct the error that caused it to be.


-VS

Always consult the oracle before asking a question.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Mahesh Murugaiyan wrote:Hi Brian,

You can add a explicit type cast which will covert the string into a "double" or just do a scanDouble().

[code=java]
response = Double.parseDouble(scan.next())


That's not a typecast. You cannot cast a String to a double or a Double.
Sonia Ballesteros Rey
Greenhorn

Joined: Apr 05, 2012
Posts: 9

Hello, Brian.
I think that your logical operators are not correct. Perhaps you should try to write in a phrase what you intend to do and then look for the logical operator that means the same.
I do not know if I have explained myself clearly with my bad english, sorry.
And what is the use of the static Scanner console and the Scanner console?


Sonia.


Lo divertido es el viaje, no el llegar.
Prabaharan Gopalan
Ranch Hand

Joined: Oct 16, 2009
Posts: 66

Right after at line 26 try doing a and see if it prints what you are inputting. And as others had mentioned use equals() ( OR equalsIgnoreCase() , if you are not keen on case-sensitive inputs) instead of ==.


Googling doesn't make you a genius. But not Googling makes you dumber.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with "scan.next() incompatible types"