Results 1 to 6 of 6
Thread: check if String is an integer?
- 05-02-2009, 05:27 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 47
- Rep Power
- 0
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.
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 thinkingJava Code:String term = args[0];
but i dont know how to check if it starts with a whole number. can anyone help me please?Java Code:if(term.startsWith()...
- 05-02-2009, 05:33 PM #2
String.matches() to check format of string
Scanner to parse a command line
Integer.parseInt() to convert string to intDon't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-02-2009, 06:10 PM #3
Member
- Join Date
- Feb 2009
- Posts
- 47
- Rep Power
- 0
Okay well my whole program turned out to look like this
When i run it, I get the following message --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; } }
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.
- 05-02-2009, 07:13 PM #4
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)
- 05-02-2009, 07:27 PM #5
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
- 05-02-2009, 07:51 PM #6
Member
- Join Date
- Feb 2009
- Posts
- 47
- Rep Power
- 0
Similar Threads
-
characters from a string into an integer
By 2potatocakes in forum New To JavaReplies: 7Last Post: 05-08-2012, 12:31 PM -
String to Integer Conversion in JSP
By vinikz in forum New To JavaReplies: 8Last Post: 11-10-2010, 02:45 PM -
Integer to String
By zervine in forum Forum LobbyReplies: 3Last Post: 09-12-2008, 12:07 PM -
Method to write if the string object can be converted to Integer or not.
By Abhishek in forum New To JavaReplies: 4Last Post: 03-25-2008, 12:16 PM -
String to Integer conversion
By eva in forum New To JavaReplies: 2Last Post: 12-17-2007, 03:59 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks