Results 1 to 6 of 6
  1. #1
    McChill is offline Member
    Join Date
    Feb 2009
    Posts
    47
    Rep Power
    0

    Default check if String is an integer?

    Is there a method to check if a String is an integer? For example, I want to take the derivative of a term.
    Java Code:
    String term = args[0];
    I assign the first command line to a String called term. Then I want to check if the first index of the string is a number with an if statement. But I can't figure out a way to check "is the first index in this string a whole number?" I was thinking
    Java Code:
    if(term.startsWith()...
    but i dont know how to check if it starts with a whole number. can anyone help me please?

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    String.matches() to check format of string
    Scanner to parse a command line
    Integer.parseInt() to convert string to int
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    McChill is offline Member
    Join Date
    Feb 2009
    Posts
    47
    Rep Power
    0

    Default

    Okay well my whole program turned out to look like this
    Java Code:
    import java.util.Scanner;
    public class Derivative {
    	
    	private int power;
    	private int coefficient;
    	private String variable;
    	
    	public static void main(String args[]) {
    		Derivative d = new Derivative();
    		System.out.println(d.getDerivative(args[0])); 	//LINE 10
    		
    	}
    
    	public void setPower(int power) {
    		this.power = power;
    	}
    
    	public int getPower() {
    		return power;
    	}
    
    	public void setCoefficient(int coefficient) {
    		this.coefficient = coefficient;
    	}
    
    	public int getCoefficient() {
    		return coefficient;
    	}
    
    	public void setVariable(String variable) {
    		this.variable = variable;
    	}
    
    	public String getVariable() {
    		return variable;
    	}
    	
    	public String getDerivative(String term)
    	{
    		String derivative;
    		Scanner scanner = new Scanner(term);	//pass the term to the scanner
    		setCoefficient(scanner.nextInt());		//set the coefficient as the first integer	LINE 42	
    		setVariable(scanner.next());			//set the variable as the next term after first integer
    		setPower(scanner.nextInt());			//set the exponent power as the second integer
    		int newCoefficient = power * coefficient;	//differentiate
    		int newPower = power - 1;
    		derivative = coefficient + variable + "^" + power;	
    		return derivative;
    		
    	}
    }
    When i run it, I get the following message --
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at Derivative.getDerivative(Derivative.java:42)
    at Derivative.main(Derivative.java:10)

    I'm unsure what it is trying to tell me other than something is wrong with line 42 and line 10. What is another way I can assign the coefficient variable to the first integer value in the String? Or could you point me in the right direction? Thanks.

  4. #4
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    in dos, you need to use:
    java Derivative "1 2 3"
    and NOT
    java Derivative 1 2 3

    because 1 is args[0]
    2 = args[1]
    and so on...
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  5. #5
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    InputMismatchException: The input wasn't what the scanner was expecting. You told it to nextInt() but the next token wasn't an integer.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  6. #6
    McChill is offline Member
    Join Date
    Feb 2009
    Posts
    47
    Rep Power
    0

Similar Threads

  1. characters from a string into an integer
    By 2potatocakes in forum New To Java
    Replies: 7
    Last Post: 05-08-2012, 12:31 PM
  2. String to Integer Conversion in JSP
    By vinikz in forum New To Java
    Replies: 8
    Last Post: 11-10-2010, 02:45 PM
  3. Integer to String
    By zervine in forum Forum Lobby
    Replies: 3
    Last Post: 09-12-2008, 12:07 PM
  4. Replies: 4
    Last Post: 03-25-2008, 12:16 PM
  5. String to Integer conversion
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-17-2007, 03:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •