Write a function that reads a string of digits, possibly preceded by + or -, and converts this string to the integer value it represents.

Write a function that reads a string of digits, possibly preceded by + or -, and converts this string to the integer value it represents.

Write a function that reads a string of digits, possibly preceded by + or -, and converts this string to the integer value it represents. Be sure that your function checksthat the string is well formed, that is, that it represents a valid integer. (Hint: use the functions provided in and ). Also, write a driver programs to test the function.

any help would be highly appreciate.

thanks in advance.

View Answers

June 16, 2011 at 4:22 PM

import java.util.*;
public class Driver {

       public static void main(String[] args) {
          Scanner input=new Scanner(System.in);
          int num1=0;    
          int num2=0;   
          char operator=' ';      
          int result=0;       

          System.out.println("Enter expressions such as  17 + 3 or 3.14159 * 4.7");
          System.out.println("Use any of the operators +, -, *, /.");


              System.out.println();
               while (true){
              System.out.print("Enter Expression: ");
              String str=input.nextLine();
              String st[]=str.split(" ");
              if((st[0].matches("((-|\\+)?[0-9]+(\\.[0-9]+)?)+"))&&((st[1].charAt(0)=='+')||(st[1].charAt(0)=='-')||(st[1].charAt(0)=='*')||(st[1].charAt(0)=='/'))&&(st[2].matches("((-|\\+)?[0-9]+(\\.[0-9]+)?)+"))){
                     num1=Integer.parseInt(st[0]);
                     operator = st[1].charAt(0);
                     num2=Integer.parseInt(st[2]);
                  switch (operator) {

                  case '+':
                     result = num1 + num2;
                     break;
                  case '-':
                     result = num1 - num2;
                     break;
                  case '*':
                     result = num1 * num2;
                     break;
                  case '/':
                     result = num1 / num2;
                     break;
                  default:
                    System.out.println("Unknown operator: " + operator);
                      } 
          System.out.println("Result is " + result);
              }
              else{
                  System.out.println("Invalid Expression!");
                  break;
              }
               }


      } 
    }

June 16, 2011 at 5:18 PM

i think here we are not handling Array dynamically, suppose if we given 200+100 , then its trowing array index out of range error,so could you pls help me in this...









Related Tutorials/Questions & Answers:
Write a function that reads a string of digits, possibly preceded by + or -, and converts this string to the integer value it represents.
Write a function that reads a string of digits, possibly preceded by + or -, and converts this string to the integer value it represents.  Write a function that reads a string of digits, possibly preceded by + or -, and converts
Integer value to string - Swing AWT
Integer value to string  How can we convert an integer type into String ,so that we can select some integers from list and sum them and add to a textfield. Here the problem is when we are going to add Integer to TextField.  
Advertisements
Java get Int from String
(st) converts the string into the integer. Here is the code... Java get Int from String       In this section, you will study how to obtain the integer value
integer to string
integer to string  i have to develop a program which convert integer into character from 1 to 10,000 if we input 1 then it give output 'one' n so on till 'ten thousand' kindly give me the codes plzplz sir........   Have
how to write a function to print for finding the longest palindrome in the given string
how to write a function to print for finding the longest palindrome in the given string  how to write a function to print for finding the longest palindrome in the given string
javascript integer to string conversion
javascript integer to string conversion  How to Javascript Integer to String Conversion   <script type="text/javascript"> /** * print out the value and the type of the variable passed
Convert an Integer into a String
() method converts an integer value into a String.  Here is the code... Enter the integer value! 24 string: 24 C:\corejava>... Convert an Integer into a String   
find the given input is integer or string
the given input value is integer or no.If the given value is integer display "Value is integer" otherwise "Value is not a integer"?   class...){ System.out.println("Value is integer!"); } else
java string replace all non digits
except the digits. How to replace all non digits in a string with some character... in combination with the replaceAll function of String. Following regular...java string replace all non digits  Hi, I am doing some processing
Convert a String into an Integer Data
and converts it into an integer. Data at the console is as a string type so.... This method converts the string type number into an integer format. Whenever you give... Convert a String into an Integer Data  
Converting a String to Integer in Java
how to convert a String value to Integer in Java? The easiest way to convert.... There are two methods of converting the String into integer value. One method... to convert the String to Integer. This function throws NumberFormatException in case
Conversion of String to Integer
Conversion of String to Integer  public class Test { public static void main(String[] args) { int countA = 0; int countB = 0...'; chr++) { (int) String "count"+chr = 0
Write String in Word document
Write String in Word document  How to read and write strings in word document
change value of mutable string
change value of mutable string  How to change value of mutable string
String Function Program
String Function Program  The string function program is just an application of the string manipulation functions in JAVA. The program is designed to count the number of one-letter word, two-letter word, and three-letter words
write a program to create an vector with string(add,remove) operation.and value should be enter through keyboard.
write a program to create an vector with string(add,remove) operation.and value should be enter through keyboard.  write a program to create an vector with string(add,remove) operation.and value should be enter through keyboard
write a program to create an arraylist with string(add,remove) operation.and value should be enter through keyboard.
write a program to create an arraylist with string(add,remove) operation.and value should be enter through keyboard.  write a program to create an arraylist with string(add,remove) operation.and value should be enter through
write a program to create an arraylist with string(add,remove) operation.and value should be enter through keyboard.
write a program to create an arraylist with string(add,remove) operation.and value should be enter through keyboard.  write a program to create an arraylist with string(add,remove) operation.and value should be enter through
String
String  write a program using string it should replace 'c'char to integer number as 1 in whole source
Convert a String into an Integer Data
Convert a String into an Integer Data In this section you will learn to convert a string type of data to integer type data. Converting string to integer and integer to string is a basic task in java programming language because
i want display integer number in a string of statement
i want display integer number in a string of statement  i want display integer number in a string of statement
String
String  write down the code of remove any character from a given string without using any string function   please give me the code of remove any given character from a given string without using function
String doubt replace function
String doubt replace function  What is the output and why of below : String s1 = "Hello"; if(s1.replace('H','H')== "Hello") System.out.println("yes"); else System.out.println("No"); if(s1.replace("H", "H")== "Hello
Java integer to string
Java integer to string       Many times we need to convert a number to a string to be able to operate on the value in its string form. Converting numeric values to Strings with Java
C String to Int
>, to convert the string value of digits ("100") into int value...; In this section, you will learn how to convert a string represented value into an integer represented value in C. You can see in the given example that we
string
string  write a program to accept the string and store the reverse stream into another array and print
string
string  a java program using string function to input any string... ArrangeStringAlphabetically { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.print("Enter string
in string how will write the return statement..........?
in string how will write the return statement..........?  import...; public static void main(String args[]){ int i=Integer.parseInt(args[0... 256"); } else{ binary(i); } } public static String binary(int i
I am writing a program which converts a string to a timestamp and add days to it
I am writing a program which converts a string to a timestamp and add days to it  java.sql.Timestamp ts2 = java.sql.Timestamp.valueOf("2012-01-11 12:34:39"); long l=ts2.getTime() +(100060602424);//add 24 days Date d=new Date(l
string
string  a java program using string function that computes your initials from your full name and display them   Please visit the following links: http://www.roseindia.net/tutorial/java/core/printInitials.html
STRING.....
STRING.....  plzz sent me d code for counting vowels in a string... gui programme
string
string   difference detween "public static void main (String[] args) " and "public static void main (String args[])" in java but it executes both... "String args[]" can mean a "string array called args which is an array
string
string  a java program using string function and access modifiers to automate the insurance company which will have to manage the information about policy holders Data Members: 1)Policy No 2)Name of policy holder 3)Address 4
string
string  String helloString = new String(helloArray); System.out.println(helloString); i am unable to understand this. could u plz explain
String
String  how to add spaces with string functions.?   Hi... { public static String addSpaceToRight(String s, int n) { return String.format("%1$-" + n + "s", s); } public static String addSpaceToLeft(String s, int n
String
characters in string?   import java.util.*; class RemoveDuplicateCharatcersFromString { public static String removeDuplicates(String s... < s.length(); i++) { String st = s.substring(i, i + 1
Printing the String value using NSLog function
In this section you will learn how you can print the value of NSString using the NSLog function. In the following code example we will show you how you can easily output the value of NSString variable using the NSLog function
MySQL String Function
the character and given the integer value. This function skipped the NULL...) function returns a value in the range of 1 to N. This function find the String... then this function returns a string that is representation of hexadecimal value
string
string  a java program to input a string and display the string...*; import java.io.*; public class FirstLetter{ public static String capitalizeFirstLetter( String str ) { final StringTokenizer st = new StringTokenizer( str
string
*; class ExtractWords { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.print("Enter String: "); String st=input.nextLine(); String str[]=st.split
string
string   just i want to a program in a short form to the given string in buffered reader for example input string: Suresh Chandra Gupta output: S. C...; public class StringTest { public static void main(String [] args
Error in checking null value in string
Error in checking null value in string  Error in checking null value in string Hi am getting Exception : java.lang.NullPointerException in the following code: String registerno=null; registerno=request.getParameter("registerno
string
string  java prgm to find total occurence of a given string pattern in a sentence
string
string  java prgm to find total occurence of a given string pattern in a sentence
String
String  How to Convert sunnapu gopal to Sunnapu Gopal in java using String
String
String  how to print in between numbers if the question "String s = "1,2,3,4,5,6,8,-25"" out must be 1 2,3,4,5,6,7,8,9,10,upto25
string
and also displaying that contain word? Like I want to find "a" in the string... and a character. It then count the occurrence of that character in the string and display... String_Example { public static void main(String[] args
string
string  program for String reverse and replace in c,c++,java e.g.Input:10,20,hello output:hello;20;10   Hi Friend, Try the following java...(String[] args) { String array[]=new String[5]; Scanner input
String to Int Conversion
; This section illustrates you how to convert a string into its integer equivalent. To demonstrate the conversion of string into an integer we are taking an example that take string as input and converts it into its integer equivalent
how to write to file from string in Java
how to write to file from string in Java  Hi, Please any one help me for how to write to file from string in Java. I am beginner in Java programming. Thanks,   Hi, Are you eager to learn how to write to file from

Ads