Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: what is wrong with my code???

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default what is wrong with my code???

    Most of my variable say that they cannot be resolved and I think my bracket/culrys are in the wrong place.

     
    import java.util.*;
     
    public class test2 {
     
    	public static void main(String[] args) {
     
    		Scanner k=new Scanner(System.in);
     
     
     
    		System.out.println("What is your first name?");
    		String f =k.nextLine();
     
     
    		System.out.println("What is your last name?");
    		String l=k.nextLine();
     
    		String n= (f + " " + l);
     
    		System.out.println("Hello, " + n);
     
     
    		System.out.println("How was your day? (good/bad)");
    		String d=k.nextLine();
     
     
    		if (d.equals("good")) {
    			System.out.println("That's great. I'm so happy for you, " + f);
    			System.out.println("What made your day so great");
    			String w=k.nextLine();
    			System.out.println("That is awesome!");
     
    		}
     
    		else if (d.equals("bad")) {
     
    			System.out.println("That's terrible!       I feel so bad for you, " + f);
     
    			System.out.println("What made your day bad");
    			String w=k.nextLine();
    			System.out.println("That stinks!");
     
    		}
     
    		System.out.print("Did you have a good Thanksgiving?");
    		String t=k.nextLine();
     
     
    		if (t.equals("yes"))
    		{
    			System.out.println("Cool");
     
    			System.out.println("Was your whole family there?");
    			String y=k.nextLine();
     
    		}
    		if (y.equals("yes")) 
    		{
    			System.out.print("That must have been fun");
     
    			System.out.println("Was your whole family there?");
    			String y=k.nextLine();
     
     
    		else if (y.equals("no")) 
    		{
     
     
    			System.out.print("That's too bad");
     
     
    			System.out.println("Did you have at least most of your family there?");
    			String h=k.nextLine();
     
    		}
     
    		if (h.equals("yes")) 
    		{
    			System.out.print("That's good");
     
    		else if (h.equals("no")) 
    		{
     
     
    			System.out.print("Thats too bad, but at least you had a good Thanksgiving");
     
     
     
     
     
     
    		}
    		else if (t.equals("no")) 
    		{
     
    			System.out.println("Why not");
     
    			System.out.println("That's too bad");
    			System.out.println("Was your whole family there?");
    			String y=k.nextLine();
     
    		}
    		else if (y.equals("no"))
    		{
     
    			System.out.print("That's too bad");
     
     
    		System.out.println("Did you have at least most of your family there?");
    		String h=k.nextLine();
    		}
    }
    	if (h.equals("yes"))
    	{ 
     
    		System.out.print("That's good");
     
     
    		if (h.equals("no")) 
    		{ 
     
    			System.out.print("Your Thanksgiving must have been terrible");
    		}
     
     
     
     
     
     
     
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: what is wrong with my code???

    Please explain what the problem is. Post the program's output and add some comments showing what you want the output to look like.

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: what is wrong with my code???

    I have had a go at tidying it all up. Some of your if and if else statements were muddled and some curly brackets needed altering.

    I have never seen so many if and else statements to be honest but let me know how this works for you:

    ...code removed


    Let me know how it goes and if there is anything missing.


    Also Tip: Try using 'Format' to arrange the code nicely. When i first started it was near impossible to work with, i had to delete all the spaces to see where i was going with it.

    --- Update ---

    P.S.

    Why is my Java code not Color coded in my post above like the OP is ?
    Last edited by copeg; November 25th, 2012 at 01:13 PM. Reason: Removed spoonfed code

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: what is wrong with my code???

    @djl1990
    Why are you doing the OPs work for him? Spoonfeeding code doesn't help an OP learn how to debug and fix his code.

    Where is your explanation of the problems with the OPs code and what you did to fix it?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: what is wrong with my code???

    I did say some If and Else's were muddled as were some curly brackets.

    I also advised to Format his code and delete unnecessary large spaces which will make it easier for future problems.

    There were no major issues as such, more layout issues.

    I don't see what else i can explain ?

    I should also note I am also a complete beginner so any explanations i try to attempt would only confuse the OP.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: what is wrong with my code???

    So why spoonfeed working code the OP can copy without having to learn how to find problems in his code and how to fix it?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: what is wrong with my code???

    @djl1990, please read the forum FAQ, as well as
    http://www.javaprogrammingforums.com...n-feeding.html

Similar Threads

  1. What's wrong with my code
    By maronski in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 26th, 2012, 09:07 AM
  2. What's Wrong With My Code?
    By Nuggets in forum What's Wrong With My Code?
    Replies: 11
    Last Post: January 31st, 2012, 10:11 PM
  3. What is wrong with this code?
    By A19 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: August 11th, 2011, 09:45 PM
  4. What is Wrong with my code
    By xew123 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 8th, 2011, 04:59 AM
  5. What is wrong with my code?
    By phantom06 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 3rd, 2011, 05:21 AM