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: Simple coding question, 1 error

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple coding question, 1 error

    Im reading this book called "Head First Java" by Kathy Sierra and on page 20 it says this. This is the link to the page i am on and the problem i need to work.

    http://books.google.com/books?id=uIV...uffle1&f=false


    this is the code i wrote.

    public class Practice {
    public static void main(String[] args){
    int x = 3;
    while (x > 0) {
    if (x > 2) {
    System.out.print("a");
    }
    if (x == 2) {
    System.out.print("b c");
    }
    if (x == 1) {
    System.out.print("d");
    x = x - 1;
    }
    x = x - 1;
    System.out.print("-");
    }
    }
    }

    I am supposed to get the answer: a-b c-d
    instead i get the answer: a-b c-d-

    How is this?
    Last edited by Extrocity; April 12th, 2014 at 08:49 PM.


  2. #2
    Junior Member
    Join Date
    Mar 2014
    Location
    sweden
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Simple coding question, 1 error

    It's
    System.out.print("-");

    Put some breakpoints out and follow whats happens and im sure youll get a clearer picture of what's happening.

    --- Update ---

    public class Practice {
     
    	public static void main(String[] args) {
    		int x = 3;
    		while (x > 0) {
     
    			if (x > 2) {
    				System.out.print("a");
    				System.out.print("-");
    			}
     
    			if (x == 2) {
    				System.out.print("b c");
    				System.out.print("-");
    			}
     
    			if (x == 1) {
    				System.out.print("d");
    				x = x - 1;
    			}
     
    			x = x - 1;
    			//System.out.print("-");
    		}
    	}
    }

    What you're doing is printing out a "-" every time in the while loop. You loop 3 times therefore you'll get the "-" 3 times as well. But like a said follow the debuggers it's a good way to learn.
    Last edited by sebbe605; April 12th, 2014 at 02:09 PM.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple coding question, 1 error

    Is there another way to do it by re-organizing things? i was given blocks of code and told to organize them to get the answer.

    x = x - 1;
    System.out.print("-");

    was one whole block of code that i had to place somewhere.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Simple coding question, 1 error

    Please post your code correctly.

    What are the assembled blocks of code supposed to do? We're a bit blind here.

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Location
    sweden
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Simple coding question, 1 error

    Im not sure what your trying to do. please elaborate.

    Also use code block when posting code, see FAQ!

  6. #6
    Junior Member
    Join Date
    Mar 2014
    Location
    sweden
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Simple coding question, 1 error

    How did you get the block from the beginning?

  7. #7
    Junior Member
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple coding question, 1 error

    I updated the thread. Now it has the page of the book i am on that explains the dirrections. I also put my code in a code block. Hopefully someone can help me clear this up!

    --- Update ---

    I updated my thread, i got the code blocks from the page in the java book i am reading. I put a link to the page in my thread.

Similar Threads

  1. Help with simple math game coding
    By jmd in forum Java Theory & Questions
    Replies: 17
    Last Post: February 28th, 2013, 10:49 PM
  2. Can someone help me(Simple coding)
    By poldz123 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 18th, 2012, 10:14 PM
  3. Please i need help in a simple coding
    By ookhai in forum Object Oriented Programming
    Replies: 13
    Last Post: July 6th, 2012, 10:19 AM
  4. i need coding for simple login
    By nag in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: September 29th, 2011, 02:30 PM
  5. [SOLVED] simple question, coding unrelated, how to delete multiple tabs?
    By Perd1t1on in forum Java Theory & Questions
    Replies: 1
    Last Post: September 8th, 2010, 07:44 PM