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

Thread: It is possible that not using array to list the positive integer <100??

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default It is possible that not using array to list the positive integer <100??

    hi...
    i got problem with my code.
    i unable to display the largest and smallest integer in range of 1 to 100...

    This is the title.
    Write a program that reads a list of positive numbers < 100 and displays the largest and the smallest. A 0 (the number zero) should terminate the list. Do not use an array to answer this question. There is at least one number before the 0.


    this my code.
    [CODE]
    public class Integer3 
    {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) 
        {
            // TODO code appli  public static void main(String [] args)
     
            {
                int min = 0;
                int max = 0;
                int integer = 0;
                max=min=integer;
     
                Scanner keyboard = new Scanner(System.in);
     
     
            System.out.print("Please enter a positive integer or 0 to terminate: ");
            integer = keyboard.nextInt();
            while(integer != 0)
            {
     
                if(integer >max )
                {   
                    max = integer;
                }
                if(integer < min )
                {   
                    min =integer;
     
                }
     
                if (integer < 1 || integer >100)
                {
                    System.out.println("Please enter again a positive integer");
                        integer = keyboard.nextInt()
                }
     
            }
     
                System.out.println("Smaller integer: " + min);//display the smallest integer
                System.out.println("Largest integer: "+max);//display the largest 
            }
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: It is possible that not using array to list the positive integer <100??

    This question should be posted in the "What is wrong" section.

    Can you post the output from when you execute your code that shows the problem?
    And explain what is wrong with it.

    The trick with searching for a largest/smallest number is to assume the first number is the correct one and assign its value to the variable. As you compare against the other numbers, change it if you find one that is a better answer.

    Another way is to initialize the largest variable to the smallest possible value of a number so that any number that you look at will be larger. And the opposite for smallest, start it at the high value so any number will be smaller.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: It is possible that not using array to list the positive integer <100??

    sorry to sent wrong site...
    can wait a minute..due to my netbean got a problem.i need re install back..sorry for that..

  4. #4
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: It is possible that not using array to list the positive integer <100??

    i change to this code.

    Scanner in = new Scanner(System.in);
     
            int biggest =0;
            int smallest =100;
            int number =1;
     
            System.out.print("Write number at one at  time and press the enter\n(O to end the program and display result:)\n");
     
            while(number !=0)
            {
                number=in.nextInt();
     
                if(number <1)
                {
                    System.out.println("Please enter positive integer");
     
                }else if( number >100)
                {
                 System.out.println("Please enter  less than 100 of  positive integer");
                }
                if(number >biggest)
                    biggest=number;
                if(number < smallest)
                    smallest =number;
            }
            System.out.println("Biggest:" +biggest+  "Smallest:"+smallest);
                    }
            }

    the output:
    Write number at one at time and press the enter
    ( O to end the program and display result: )
    123
    Please enter less than 100 of positive integer
    12
    -1
    Please enter positive integer
    100
    2
    0
    Please enter positive integer
    Biggest:123Smallest:-1


    The biggest should be 100 and smallest should be 2..
    but i can't figure out how to do it..
    Last edited by lim131; April 14th, 2012 at 11:39 AM.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: It is possible that not using array to list the positive integer <100??

    What does the code do when it finds a number out of range and prints a message? What does it do next?

    Do you know about the continue statement? That will help you change how the code executes.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: It is possible that not using array to list the positive integer <100??

    skip to next iteration..
    but just now i put continue inside the while...it seem appear same things..

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: It is possible that not using array to list the positive integer <100??

    Post the new code and the console from when you execute it.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: It is possible that not using array to list the positive integer <100??

    while(number !=0)
            {
                number=in.nextInt();
     
                if(number <1)
                {
                    System.out.println("Please enter positive integer");
     
                }else if( number >100)
                {
                 System.out.println("Please enter less than 100 of positive integer");
                }
                if(number >biggest)
                    biggest=number;
                if(number < smallest)
                    smallest =number;
                 continue;
            }
     
            System.out.println("Biggest:" +biggest+  "Smallest:"+smallest);
                    }

    i put inside it...
    Does this " Write a program that reads a list of positive numbers < 100" mean print out the 100 integer??
    Last edited by lim131; April 14th, 2012 at 12:20 PM.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: It is possible that not using array to list the positive integer <100??

    What do you want the program to do when it finds an invalid input and prints out a message?
    Your code then tests the number.

    continue at the end of a loop is redundant. The loop will normally go to the top there without the continue.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Array List of Array Lists working for first item but not for second.
    By javapenguin in forum Collections and Generics
    Replies: 6
    Last Post: February 15th, 2012, 04:12 PM
  2. The sum and product of a positive number between 1000 and 9999.
    By metaleddie13 in forum Member Introductions
    Replies: 1
    Last Post: September 15th, 2011, 04:39 AM
  3. Array List
    By nadirkhan in forum Collections and Generics
    Replies: 6
    Last Post: September 9th, 2011, 09:39 AM
  4. Need help deserializing an integer array.
    By jeremy_ in forum Collections and Generics
    Replies: 10
    Last Post: May 30th, 2011, 11:50 AM
  5. Read A File and Store Values into a 2-Dimensional Integer Array?
    By Kimimaru in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 9th, 2011, 08:13 PM