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 8 of 8

Thread: Can you help?

  1. #1
    Junior Member
    Join Date
    Oct 2017
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can you help?

    first read a positive integer value, n, from the user
    Write a while loop to output the integer values from 0 up to n exclusive. The output should have five values per line, with values separated by a space.

    My code is
    Scanner in=new Scanner(System.in);



    int n;
    int a;
    a=0;




    System.out.println("Please enter a number: ");
    n=in.nextInt();

    while ( n>0)
    {
    System.out.println(a+ " " +(a+1)+ " "+ (a +2) +" " + (a+3 ) + " " +(a+4));

    if (a== n + 1 )
    {
    System.exit(1);
    }
    a++;
    }

    But the output is like this
    0 1 2 3 4
    1 2 3 4 5
    2 3 4 5 6
    3 4 5 6 7
    4 5 6 7 8
    5 6 7 8 9
    6 7 8 9 10
    7 8 9 10 11
    8 9 10 11 12
    9 10 11 12 13
    I don't know how to solve this and I have no idea

  2. #2
    Member
    Join Date
    Apr 2014
    Posts
    81
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Re: Can you help?

    Hi,

    You're supposed to loop from 0 to 'n', but you're never checking any value against 'n' inside the loop. Instead, you're looping as long as 'n' > 0 but problem is 'n' will ALWAYS be greater than zero. The System.exit(1) call is a hack; you should add an increment counter and fall out of the loop naturally instead (loop while counter < 'n').

    But on to the actual problem, consider what's happening to the value of 'a' in each iteration of the loop. Then consider what the print statement is doing with 'a'. Is incrementing 'a' by 1 enough? Also, did your instructor cover the differences between System.out.print() and System.out.println()? A solution exists that involves using them correctly and in conjunction.

  3. #3
    Junior Member
    Join Date
    Oct 2017
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can you help?

    Hello,
    thanks for your comment
    this is my new code
    int a;
    a=1;
    System.out.println("Please enter a number: ");
    Scanner in=new Scanner(System.in);
    int n=in.nextInt();
    while ( a<n)
    {

    System.out.print(a + " ");
    if(a==n-1)
    {
    System.exit(1);
    }
    a++;

    System.out.print((a) + " ");
    if(a==n-1)
    {
    System.exit(1);
    }
    a++;

    System.out.print((a) + " ");
    if(a==n-1)
    {
    System.exit(1);
    }
    a++;

    System.out.print((a) + " ");
    if(a==n-1)
    {
    System.exit(1);
    }
    a++;

    System.out.println((a)+ "");
    if(a==n-1)
    {
    System.exit(1);
    }
    a++;
    }
    }
    }

    and this is what I get

    Please enter a number: 12
    1 2 3 4 5
    6 7 8 9 10
    11

    how can I change system exit to something else?

  4. #4
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    261
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Can you help?

    You can use if statement to check whether the value a is n. If it is n, then the program will terminated.

    public class Card {
        static int a;
     
        public static void main(String[] args) {
            a = 1;
            System.out.println("Please enter a number: ");
            Scanner in = new Scanner(System.in);
            int n = in.nextInt();
            while (a <= n) {
                System.out.print(a + " ");
                a++;
                if (a == n)
                {
                    System.out.println(a);
                    System.exit(1);
                }
                System.out.print(a + " ");
                a++;
                if (a == n)
                {
                    System.out.println(a);
                    System.exit(1);
                }
                System.out.print(a + " ");
                a++;
                if (a == n)
                {
                    System.out.println(a);
                    System.exit(1);
                }
                System.out.print(a + " ");
                a++;
                if (a == n)
                {
                    System.out.println(a);
                    System.exit(1);
                }
                System.out.println(a + "");
                a++;
            }
        }
    }

    Output
    Please enter a number: 
    12
    1 2 3 4 5
    6 7 8 9 10
    11 12
    Last edited by John Joe; October 25th, 2017 at 01:40 AM. Reason: Add output
    Whatever you are, be a good one

  5. #5
    Member
    Join Date
    Apr 2014
    Posts
    81
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Re: Can you help?

    Quote Originally Posted by berk View Post
    how can I change system exit to something else?
    Something like this:
    int i = 0;
    while (i < 5) {
    	// Do stuff
    	i++;
    }
    // Let program terminate naturally
    It will eventually leave the loop because its condition (i < 5) is guaranteed to eventually become false. After that, the program dies a natural death. No need for System.exit() at all.

    In your new solution (which seems to be working now, right?), you can replace every System.exit(1) with a simple "break" statement and the program still works the same.

    And for trivia, passing anything other than zero to System.exit() means you're telling the system that your program encountered a fatal error. If you do use System.exit() to terminate normally, in any other programs, you should pass zero.

  6. #6
    Junior Member
    Join Date
    Oct 2017
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can you help?

    I have a better idea

    import java.util.Scanner;

    public class FirstProgramme
    {
    public static void main(String[] args)
    {

    System.out.print("Please enter a number: ");
    Scanner in=new Scanner(System.in);
    int n=in.nextInt();
    int a;
    a=1;
    int count;




    while(a<n)
    {
    count=0;

    while( count<= 4 )

    {
    System.out.print((a) + " " );
    a++;
    count++;

    if(a==n)
    {
    break;
    }

    else if (count == 5)
    {
    System.out.println();
    }



    }
    }
    }
    }

    Please enter a number: 14
    1 2 3 4 5
    6 7 8 9 10
    11 12 13

    I think now I understand how while works
    Thanks for your advices
    Last edited by berk; October 25th, 2017 at 06:59 PM.

  7. #7
    Member
    Join Date
    Apr 2014
    Posts
    81
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Re: Can you help?

    Nice job!!

  8. #8
    Junior Member
    Join Date
    Oct 2017
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can you help?

    Good job! The program works!

    Some things can be done now.
    - reformat the code to give clarity
    - give meaningful names to variables
    - put comments to explain what are you doing
    - expressing the solution in a more straightforward and simple way
    - etc

    This kind of things are very important. I'm gonna do the first three. This is your program with very little modifications:

    public class FirstProgramme {
            public static void main(String[] args) {
                // get max number to print
                    System.out.print("Please enter a number: ");
                    Scanner in = new Scanner(System.in);
                    int maxNumber = in.nextInt();
     
                // --------------------------------------------------
                // print numbers from 1 to maxNumber
                // --------------------------------------------------
                    int number = 1;
     
                    // while I have numbers to print
                    while ( number<maxNumber ) {
                            int count = 0;      // count for numbers printed in the line
     
                            // print next 5 numbers in the line
                            while( count <= 4 ) {
                                    System.out.print(number + " " );
     
                                    number++;   // next number to print
                                    count++;    // now we have another number printed in the line
     
                                    // if we have printed all the numbers, exit
                                    if (number == maxNumber) {
                                            break;
                                    // otherwise, jump to the next line every 5 numbers
                                    } else if (count == 5) {
                                            System.out.println();
                                    }
                            }
                    }
                // --------------------------------------------------
            }
    }
    Last edited by German M; October 27th, 2017 at 05:08 PM.