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: I except show this FIRST - JAVA

  1. #1
    Junior Member
    Join Date
    Nov 2019
    Posts
    22
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default I except show this FIRST - JAVA

    Hello,

    Could you please run this?
    I except show "Enter N:" but the program waiting to receive the number.

     
    import java.util.Scanner;
     
    public class NewMain {
     
        public static  boolean isPrime(int n)
        {
            boolean flag = true;
            for(int i = 2 ; i < n ; i++)
            {
                if (n %i == 0)
                {
                    flag = false;
                    break;
                }
            }
            return flag;
        }
     
        public static void Range(int a , int b)
        {
            for (int i = a ; i <=b ; i++)
            {
                   if (!isPrime(i))
            {
                System.out.printf("\n%d is NOT Prime!",i);
            }
            else
            {
                  System.out.printf("\n%d is Prime!",i);
            }
            }
        }
     
        public static void main(String[] args)
        {
            Scanner scan = new Scanner(System.in);
            System.out.printf("Enter N: "); // I except show this FIRST
     
            int n = scan.nextInt(); // But this run FIRST!
            boolean flag = isPrime(n);
                 if (!flag)
            {
                System.out.printf("\n%d is NOT Prime!",n);
            }
            else
            {
                  System.out.printf("\n%d is Prime!",n);
            }
              System.out.printf("Enter A: ");
              int a = scan.nextInt();
              System.out.printf("Enter B: ");
              int b = scan.nextInt();
              Range(a,b);
        }
     
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I except show this FIRST - JAVA

    The program prints a prompt on the console and waits for me to enter a number.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    shervan360 (April 10th, 2021)

  4. #3
    Junior Member
    Join Date
    Nov 2019
    Posts
    22
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: I except show this FIRST - JAVA

    Thanks, but this is for me:

    10
    Enter N:
    20
    30
    10 is NOT Prime!Enter A: Enter B:
    20 is NOT Prime!
    21 is NOT Prime!
    22 is NOT Prime!
    23 is Prime!
    24 is NOT Prime!
    25 is NOT Prime!
    26 is NOT Prime!
    27 is NOT Prime!
    28 is NOT Prime!
    29 is Prime!
    30 is NOT Prime!

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I except show this FIRST - JAVA

    Where and how are you executing the program?
    On Win10 I opened a command prompt window and entered the command: java NewMain
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    shervan360 (April 10th, 2021)

  7. #5
    Junior Member
    Join Date
    Nov 2019
    Posts
    22
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: I except show this FIRST - JAVA

    Win 10 and netbeans 13 (output window)

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I except show this FIRST - JAVA

    What happens when you execute the program in the command prompt outside of NB?
    What is output window? Does NB interact with the console in the same way the the command prompt window does?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #7
    Junior Member
    Join Date
    Apr 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I except show this FIRST - JAVA

    Try to add

    System.out.flush();

    after System.out.printf

Similar Threads

  1. Replies: 0
    Last Post: March 21st, 2021, 03:32 AM
  2. Replies: 1
    Last Post: September 17th, 2018, 09:40 PM
  3. New to Java, Shape Won't Show in Console
    By BulgeBracket in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 30th, 2012, 07:30 PM
  4. can someone show me how to change speed of an java engine.
    By codenoob in forum Java Theory & Questions
    Replies: 19
    Last Post: December 14th, 2011, 12:26 PM
  5. Beginner: Show Image in Label when Results Show Up
    By Big Bundy in forum Java Theory & Questions
    Replies: 3
    Last Post: April 4th, 2011, 02:43 PM