Results 1 to 15 of 15
Thread: Need help with my code
- 10-02-2011, 06:51 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 10
- Rep Power
- 0
Need help with my code
Hey guys,
Im completely new to coding. I literally started about 3 weeks ago. I haven't done computer science yet so bear with me :)
This code works but i dont know why.. A bunch of errors showed up before and told me to add the two "!= null". Also I had the variable "input" as a String instead of a Boolean. When i tried to switch it to a boolean before the two "!= null", it said i wasn't aloud to do that.. Can anyone explain this to me? Any help would be appreciated.Java Code:import java.util.Scanner; public class MainClass { public static void main(String[] args){ Scanner sc = new Scanner (System.in); boolean input = sc.nextLine() != null; System.out.println(input); if (input = "Hello my friend" != null){ System.out.println("Ahh, hello there!"); } } }Last edited by sunde887; 10-02-2011 at 07:07 PM. Reason: Added code tags, [code]...[/code]
- 10-02-2011, 07:10 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: Need help with my code
sc.nextLine() returns a string, not a boolean. What happens if someone types in "blah", how can that be a boolean value(true/false). If you intend to get a string variable, store it in a String variable. In your code, input is a boolean, "Hello my friend" != null returns true as long as the string is not null. If this makes sense--good. If not ask me to clarify, and I will.
- 10-02-2011, 07:15 PM #3
Re: Need help with my code
Instead of trying to do two things in one statement, break this statement into two simple statements.boolean input = sc.nextLine() != null;
1) get the input
2) test it
- 10-02-2011, 08:03 PM #4
Member
- Join Date
- Oct 2011
- Posts
- 10
- Rep Power
- 0
Re: Need help with my code
Thanks for the help guys :) i changed the variable to a string and then it underlines "input = "Hello my friend" and says i cannot covert it
from a string to a boolean. Can i just simply not have an if statement for a String variable? Is there anyway to go around that? For example, i want the console to respond to the user if he says a certain thing. Sorry if im not making sense.
- 10-02-2011, 08:05 PM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
- 10-02-2011, 08:08 PM #6
Re: Need help with my code
Can you post the code with the problem?
The following statement has a boolean on the left and a String on the right
That doesn't work. You needJava Code:boolean input = "Hello my friend";
either a String variable on the left
or a boolean expression on the right.
- 10-02-2011, 08:13 PM #7
Member
- Join Date
- Oct 2011
- Posts
- 10
- Rep Power
- 0
Re: Need help with my code
Java Code:import java.util.Scanner; public class MainClass { public static void main(String[] args){ Scanner sc = new Scanner (System.in); String input = sc.nextLine(); System.out.println(input); if ([U]input = "Hello my friend"[/U]){ System.out.println("Ahh, hello there!"); } } }Last edited by sunde887; 10-02-2011 at 08:14 PM. Reason: Added code tags, [code]...[/code]
- 10-02-2011, 08:14 PM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: Need help with my code
When posting code, please use code tags: [code] YOUR CODE HERE [/code]
Also, '=' is an assignment operator, comparisons is done with '==' and strings need to be compared with .equals(Object o)
- 10-02-2011, 08:16 PM #9
Member
- Join Date
- Oct 2011
- Posts
- 10
- Rep Power
- 0
Re: Need help with my code
Is there something different besides a string or boolean that i can use? I looked at the .equals (Object o) method. I dont really understand that. It said it was a "superclass" or something...
- 10-02-2011, 08:17 PM #10
Member
- Join Date
- Oct 2011
- Posts
- 10
- Rep Power
- 0
Re: Need help with my code
oh sorry about that :/ i was wondering how to do that haha
- 10-02-2011, 08:19 PM #11
Member
- Join Date
- Oct 2011
- Posts
- 10
- Rep Power
- 0
Re: Need help with my code
Ohhhh hold on i think i know how to fix it..
- 10-02-2011, 08:21 PM #12
Member
- Join Date
- Oct 2011
- Posts
- 10
- Rep Power
- 0
Re: Need help with my code
Alright i fixed it all i had to do was change it from '=' to '=='. Thanks guys!
- 10-02-2011, 08:24 PM #13
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: Need help with my code
That is still actually incorrect. For primitives it is fine to use '==' but for strings this compares references--which is not what you want. Instead use equals.
Will return true if the two strings are equal.Java Code:string1.equals(otherString)
- 10-02-2011, 08:31 PM #14
Member
- Join Date
- Oct 2011
- Posts
- 10
- Rep Power
- 0
Re: Need help with my code
Hold on ill try that right now
- 10-02-2011, 08:39 PM #15
Member
- Join Date
- Oct 2011
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
My code was not executed properly.It will jumping to exception handling.my code is
By vinay4051 in forum EclipseReplies: 3Last Post: 08-10-2011, 09:17 AM -
servlet include method copying sorce code and executing source code as output how to
By shamkuma2k in forum Advanced JavaReplies: 0Last Post: 08-07-2011, 08:32 PM -
C server code - Java CLient Code _ TCP Connection Problem
By rmd22 in forum NetworkingReplies: 0Last Post: 02-21-2011, 11:50 AM -
can any one pls send me a sample code for calling a jsp code in swings
By sniffer139 in forum AWT / SwingReplies: 1Last Post: 03-04-2010, 11:19 AM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks