If ... is not int {

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

I saw everything:

   if  (v % 1) 

to

    parseInt();

but they do not work 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
source share
6 answers

Verifying that the value is primitive will not work. Java will not be able to compare the value with the type in this way.

One way is to use a static function Integer.parseInt(String s)to find out if the corresponding int value has been entered. Note that it throws NumberFormatException. If you can take advantage of this fact, you can get whether an integer was provided from the function.

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

The second method (since you are already using a class Scanner) is to use methodsScanner hasNextInt()and nextInt()to determine:

  • Scanner

:

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

, , Scanner . Scanner . , (: "," "//" ..), ?

, Scanner. useDelimiter(String pattern), , . , , ( , ).

:

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

API Scanner (. Scanner, ), , . , ( ).

+6

, : JLS ยง3.10.1 , . , :

// input s
if (s.matches("(?i)[+-]?(0|[1-9][0-9]*|0[x][0-9a-f]+|0[0-7]+)L?")) { /* do something */}

, . Java . , , , 2,3 :

String intLiteral = "[+-]?(0|[1-9][0-9]*|0[x][0-9a-f]+|0[0-7]+)L?";
// input s
if (s.matches("(?i)" + intLiteral + "," + intLiteral)) { /* do something */}

( , , , , 2, 3, .)

2,3, : , . :

    String s = "2,3";
    String[] parts = s.split(","); // here we deal with commas

    try {
        // now when we have parts, deal with parts separately
        System.out.println("First:  " + Integer.parseInt(parts[0]));
        System.out.println("Second: " + Integer.parseInt(parts[1]));
    } catch (NumberFormatException e) {
        System.out.println("There was a problem, you know: " + e.getMessage());
    }
+2

Integer.parseInt(yourInt), , , , :

try {
  Integer.parseInt(yourInt);
}
catch (NumberFormatException e) {
  // do something that indicates to the user that an int was not given.
}
+1
if (coordinate1 != int)

, . .

:

try {
    int integer = Integer.parseInt(value);
} catch (NumbumberFormatException e) {
    // Not an integer
}
0

:

Integer result = null;
try {
    result = Integer.valueOf(input);
} catch (NumberFormatException e) {}
if (result != null) {
    // An integer was entered
}

Integer.valueOf will throw an exception for numbers with deciamls, so you shouldn't have any problems. I am using the Integer vs int class so that it can be set to null. Using -1 means -1 cannot be entered.

0
source

One of the methods that you can try to use is a new method. Here I use to get integer inputs:

public static int getIntInput(Scanner s) { // Just put your scanner into here so that the method can read inputs without creating a new scanner each time
    System.out.print("Enter int here: ");
    try {
        return Integer.parseInt(s.nextLine());
    } catch (NumberFormatException e) {
        System.out.println("You must enter a valid Integer literal.");
        return getIntInput(s);            // Note that this line just repeats the try statement all over again.
    }

If the input is an integer, it will return int; otherwise he tries again. Note: add a counter so that the code is not violated if someone installs a breakfast bag on your enter key.

0
source

All Articles