Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8847349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:10:12+00:00 2026-06-14T12:10:12+00:00

I am trying to learn Java programming by myself and came across the course

  • 0

I am trying to learn Java programming by myself and came across the course CS106A provided by Stanford. It’s a great free online course. I watched several of the lecture videos and I enjoyed them so far. I am now trying to do the assignments and I have this problem I can’t solve by myself.

It is the number 5 of this assignment. Basically, it requires the learner to create a console program to get some integers input by the user and in response, showing the biggest and smallest number.

The following code is what I have done and the problem is when I try to input integers, it will skip the even number of inputs. For instance if I enter 3,12,6,15,9 to the console, it will only get 3,6,9 , ignoring 12,15.

What did I do wrong? Any help would be appreciated.

import java.util.*;

public class Ass2_5MaxMinNumbers {
    public static void main (String args[]) {
        Scanner scanner;

        System.out.println ("This programme finds the largest and smallest numbers.");
        System.out.println ("After finished entering, enter \"End\" (without double quotes) to show the results.");

        List<Integer> list = new ArrayList<Integer>();
        int max = 0, min = 0;

        do {
            scanner = new Scanner(System.in);

            if(scanner.hasNextInt()){
                int x = scanner.nextInt();
                list.add(x);
                System.out.println("user input: " + x);
            } else if(!scanner.hasNext("End")){
                System.out.println("Please enter an integer!");
            }
        } while (!scanner.hasNext("End"));

        max = list.get(0);
        min = list.get(0);
        for(int x = 1; x < list.size(); x++){
            if(list.get(x) > max){
                max = list.get(x);
            } else if(list.get(x) < min){
                min = list.get(x);
            }
        }

        System.out.println ("Smallest number: " + min);
        System.out.println ("Biggest number: " + max);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:10:13+00:00Added an answer on June 14, 2026 at 12:10 pm

    Scanner.nextInt method only reads the next token from the input passed from user. So, it ignores the linefeed that is at the end of each input. And that linefeed goes as input to the next call to Scanner.nextInt, and hence your input is ignored.

    You can use a blank Scanner.nextLine after each call to Scanner.nextInt to consume the linefeed.

    if(scanner.hasNextInt()){
        int x = scanner.nextInt();
        scanner.nextLine();  // Add a blank nextLine
        list.add(x);
        System.out.println("user input: " + x);
    }
    

    Or you can also use scanner.nextLine() only for reading integers, and convert the input to integer using Integer.parseInt.

    if(scanner.hasNextInt()) {
        int x = 0;
    
        try {
             x = Integer.parseInt(scanner.nextLine());
        } catch (NumberFormatException e) {
             e.printStackTrace();
        }
    
        list.add(x);
        System.out.println("user input: " + x);
    }
    

    Actually, since you are using scanner.hasNextInt in your if, you don’t really need that try-catch around your Integer.parseInt, so you can remove that.


    UPDATE : –

    I would replace your do-while with a while and just remove the if-else check from inside.

    while (scanner.hasNextLine()) {
        String input = scanner.nextLine();
    
        if (input.equals("End")) {
            break;
        } else {
            try {
                int num = Integer.parseInt(input);
                list.add(num);
    
            } catch (NumberFormatException e) {
                System.out.println("Please Enter an Integer");
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn Java, and came across a problem on one of the
I am trying to learn web programming using Java, and I ran into bunch
iv'e been trying to learn basic java programming for the last 2 days, and
trying to learn windows programming in java, want to display a image to a
Greetings, I am currently trying to learn some Java programming. To do this I'm
I am trying to learn both java and pig programming.. So basically.. not an
I am trying to learn C#! I am most familiar with Java between programming
I am trying to learn Java by doing some assignments from a Stanford class
I am trying to learn java web programming. I come from a perl scripting
I'm making my first Java project, and trying to learn OO programming as well

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.