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

  • Home
  • SEARCH
  • 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 3334918
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:57:29+00:00 2026-05-17T23:57:29+00:00

This is a school project: Objective: Ask the user to input 2 number A

  • 0

This is a school project:

Objective:

Ask the user to input 2 number

A random number will be print to the user.

Must catch if input isn’t Integer.

I have check online source, which i copied the code and use it.

Min + (int)(Math.random() * ((Max - Min) + 1))

The code work fine except if I input any integer less than 10. The Program will consider it as a letter and says “ERROR”

My Program:

import java.lang.StringBuffer;
import java.io.IOException;
import java.util.Random;


class RandomInput2
{
    public static void main(String args[])
    {
        System.out.println("Programe Begins");


        Random seed = new Random();
        int n1 , n2, rand ;
        System.out.println("What is your name?");
        String InputString = GCS();
            while(true)
            {
                try
                {
                    System.out.println("What is your First number?");
                    n1 = Integer.parseInt(GCS());
                    System.out.println("What is your second number");
                    n2 = Integer.parseInt(GCS());
                    rand = n2+ (int)(seed.nextDouble()*((n1-n2)+1));
                    System.out.println(InputString+" Your number is "+rand);
                }

                catch(NumberFormatException NFE)  //catch if integer's not number
                {
                    System.err.println("ERROR");
                    System.err.println("Type in Integer only");
                }
                catch(Exception E)  //catch General Error
                {
                    System.err.println("ERROR");
                }


;
            }
    }


    public static String GCS() //Get Console String
    {
        int noMoreInput =-1; //set int
        char enterKeyHit= '\n'; //set char

        int InputChar;
        StringBuffer InputBuffer = new StringBuffer(100);

        try
        {
            InputChar=System.in.read();
            while(InputChar != noMoreInput)
            {
                if((char)InputChar!=enterKeyHit)
                {
                    InputBuffer.append((char)InputChar);
                }
                else
                {
                    InputBuffer.setLength(InputBuffer.length()-1);
                    break;
                }
                InputChar = System.in.read();
            }//ends while loop
        }
        catch(IOException IOX)
        {
            System.err.println(IOX);
        }
        return InputBuffer.toString();
    }
}
  • 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-05-17T23:57:29+00:00Added an answer on May 17, 2026 at 11:57 pm

    Look at the GSC code — if I enter 1 character and then hit enter, what’s the length of the InputBuffer when I do hit enter?

    Since you want to read a whole line, consider using an InputStreamReader to read System.in, and then a BufferedReader to wrap that reader (so that you can call readLine).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using this library, ZXing , we have a project at school in which we'll
This is about a school assignment so I'm trying to do things by the
This is my first post here and I wanted to get some input from
I'm writing a program for a school project that is supposed to emulate the
I am making a fraction class for a school project, and my brain is
I'm just learning to build a website. It's a school project and after I
I am working on a project for a class at school. It is a
So I had to write a program for a computer project for high school
I know this will seem like a dumb question but I'm just getting started
This is a bit of a long shot, but if anyone can figure it

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.