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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:49:56+00:00 2026-05-26T10:49:56+00:00

I am trying to get the program to recognize if an int is not

  • 0

I am trying to get the program to recognize if an int is not entered .

I have seen everything from:

   if  (v % 1) 

to

    parseInt();

but they aren’t working for me .

import java.util.Scanner;

public class LinearSlopeFinder {
    public static void main(String[]args){
        double x1, y1, x2, y2, n1, equation, constant = 0 ;
        double slope, slope1, slopeAns;
        Scanner myScanner = new Scanner(System.in);

        System.out.print("    What is the first set of cordinants? example: x,y ... ");
        String coordinate1 = myScanner.nextLine();

        //below is what i am referring to 


        if (coordinate1 != int ){    // if it is a double or String 
            System.out.println("Sorry, you must use whole numbers.What is the first set of cordinants? example: x,y ... ");
            System.out.print("    What is the first set of cordinants? example: x,y ... ");
            String coordinate1 = myScanner.nextLine();
        }
  • 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-26T10:49:57+00:00Added an answer on May 26, 2026 at 10:49 am

    Checking that a value is a primitive like that won’t work. Java will not be able to compare a value to a type in such a way.

    One method is to take advantage of the static function Integer.parseInt(String s) to see if an appropriate int value has been entered. Notice that it throws a NumberFormatException. If you can take advantage of this fact, you can obtain whether or not an integer was provided from a function.

    try {
       //Attempt the parse here
    } catch (...) {
       //Not a proper integer
    }
    

    A second technique (since you’re already taking advantage of the Scanner class) is to use the Scanner methods hasNextInt() and nextInt() to determine if:

    1. The Scanner has a new integer in the stream
    2. Get the actual integer value off of the stream

    An example usage would be:

    if (myScanner.hasNextInt()) {
       int totalAmount = myScanner.nextInt();
       // do stuff here
    } else {
       //Int not provided
    }
    

    As you mentioned in your update, this is all fine and good when input from the Scanner is delimited by spaces. By default, Scanner delimits values within the stream by spaces. What happens when you’ve got a different delimiter (ex: “,” or “//” etc) that separates two unique values logically on the stream?

    The solution to that is to modify the delimiter that the Scanner is using. There is a method called useDelimiter(String pattern) which allows you to specify how values will be separated logically within the stream. It’s very useful for cases like what you’re dealing within (or any cases where spaces do not delimit the values).

    The usage will look something like this:

    Scanner myScanner = ...; # Get this how you normally would
    String delimiter = ...;  # Decide what the separator will be
    myScanner.useDelimiter(delimiter); # Tell the Scanner to use this separator
    

    There is a good example of this within the Scanner API (see the first link on Scanner I included for that example) that describes how this is used. I suggest checking that out, and it’ll come together very nicely for you (I believe).

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

Sidebar

Related Questions

I'm trying to get this simple program to work on windows, but it crashes:
I'm trying to get console input in my Clojure program, but when it gives
I'm trying to get my java program to run an svn command from the
I am trying to get Windows to recognise my program's File extensions but without
I am trying to get the program to call up the current date, add
i'm trying to get this program to break down a user defined amount of
I'm trying to get started with Hibernate, and when executing my program I get
Below is my program and I am trying to get the call stack when
I am trying to figure out how to get the program to create a
I'm trying to use rusage statistics in my program to get data similar to

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.