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 8125813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:56:24+00:00 2026-06-06T06:56:24+00:00

In order to make my program more streamlined (Without try and catch everywhere), I

  • 0

In order to make my program more streamlined (Without try and catch everywhere), I tried to create a seperate method just for getting data. I have one for Doubles, and Strings, as well.
For some reason, when I try to use this method, it is completely ignored, and passed by like a comment. Is there something I’m doing wrong?

public int inputint(){
     Scanner sc = new Scanner (System.in);
     int variable = 0;
     boolean valid = true;
     do{
        try{
           if (variable >= 0 && valid){
           }
           else if(valid){
              System.out.print("Please enter positive values only: ");
           }

           valid = true;
        }
           catch (InputMismatchException e){
              System.out.print("Please enter numerical values only: ");
              sc = new Scanner(System.in);
              valid = false;
           }
     }while (!valid || variable < 0);
     return variable;
  }
  • 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-06T06:56:27+00:00Added an answer on June 6, 2026 at 6:56 am

    Well first of all, you’re code is insanely hard to understand. (or could just be me I guess)

    But if you look at your code

    Scanner sc = new Scanner (System.in);
         int variable = 0;
         boolean valid = true;
    

    You are creating the Scanner object, but nowhere in the method are you actually using it.

    The next few lines,

    if (variable >= 0 && valid){
           }
    

    both of those conditions are met. So with nothing in the brackets, no code is executed. So from there, it just returns the value of the variable, which is 0.

    So you need to actually use your Scanner class to grab an integer. Which I believe, though I’m not sure, the method for that is

    Scanner.nextInt();
    

    Edit: From the JavaDocs http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html

    You can simply use

    Scanner sc = new Scanner(System.in);
     int i = sc.nextInt();
    

    to read an integer from the Scanner.

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

Sidebar

Related Questions

In order to make my Visual C++ program more robust I'm tempted to insert
My program needs to make use of void* in order to transport data or
Sometimes in order to make our application more responsive and fast, we use client
I'm currently trying to create a subclass of UIImageView in order to make it
I am writing a program that accesses a database in order to make sure
in order to make things easier for users i want to add multiple keyword
in order to make sure all form submission and all data submitted to server
It appears that in order to make TinyMCE work with an ASP.NET AJAX website,
I used 320andup in order to make my first real responsive website. I managed
I am currently using Serialization in order to make deep copies of some autogenerated

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.