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

 
 


Mallesham
Write a function that reads a string of digits, possibly preceded by + or -, and converts this string to the integer value it represents.
2 Answer(s)      a year and 11 months ago
Posted in : Java Beginners

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 Pages:
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
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
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.  
Convert an Integer into a String
converts an integer value into a String.  Here is the code of this program... an integer number at  the console and it converts it into a string format... Convert an Integer into a String   
String
String  write a program using string it should replace 'c'char to integer number as 1 in whole source
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
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
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
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
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
MySQL String Function
BIN (N) The BIN string function return a string value... and given the integer value. This function skipped the NULL values. Here... a value in the range of 1 to N. This function find the String str
Convert a String into an Integer Data
into an integer type using the parseInt() method. This method converts the string type... Convert a String into an Integer Data   ... the functionality to convert the string type data into an integer type data. Code Description
A program which converts feet and inches to centimeters.
Java: Exercise - InchesToCm Write a program which converts feet and inches to centimeters. In this application we will write code for converting feet... and the "=" button Text field to display the converted value in centimeters
Convert Hexadecimal number into Integer
extracted from the specified String. The result is an integer that represents the integer value specified by the string . Here is full code of this program... (using the keyboard) and converts the hexadecimal data into an integer type data
string to double
, parseDouble() method takes a String as input and converts it into a double when...; } return x; } public static double parseDouble(String value...string to double   So I have an assignment where we have to convert
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
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
convertig biginteger value into integer
convertig biginteger value into integer  I want to parse a BigInteger class variable into integer like BigInteger p = BigInteger.valueOf(10); now i want to convert p value into int . I searched so many times on google to solve
Why and how to use Integer.parseInt() in java
is used to convert an string value into integer. JVM reads each element as String and hence we have to convert an entered String value into respective data type... into the Integer. In  java , we know that everything treated as  String
Convert Boolean to String
type data into a string. The toString() method reads the Boolean type object and it converts into a string format. This method returns a string object and represents the boolean?s value. The Boolean object represents either ?true
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
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 http
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
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
Summary - String
for Converting to String s = String.valueOf(x) Converts x to String, where x is any type value (primitive or object). s = ...Java: Summary - String String Concatenation Following table shows how
String Array - Java Beginners
each value one by one. if it is string keep it in a string variable... value is an integer better to multiply present integer by 10 and then add the next...String Array  From where can I get, all the functions that are needed
Programming: To Uppercase
________________________________________ Write a complete program that reads a string from the user with an input dialog, converts it to uppercase, and displays the string in another dialog box. You can use the String methods toUpperCase
get integer at run time
get integer at run time  how to get integer value at run time in j2ee using servlets   Hi, You can get the value from request parameter... is the example code: String s =request.getParameter("myvariable"); Integer i
Conversion from long to string
value from console. The line String mystring = Long.toString(mylong); converts... a long type value to String type value. Description: This program will take a long type value from console and provide the conversion to String type
Conversion from float to String
); converts the myfloat float type value to mystring String type value. Code... a float type value to String type value. Description: This program will take a float value from console and provides a conversion to String type data
Conversion from double to String
(mydouble); converts the mydouble double type value to mystring String type data...(String[] args) { try { // Reads the value from console BufferedReader... to convert a double type value to String type value. Description: This program
Conversion from byte to String
a byte type value to String type value. Description: This program will take a byte value from console and provides a conversion to String type data... data from console. The line String mystring = Byte.toString(mybyte); converts
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
Convert Integer to Double
;IntegerToDouble" for java component. Program takes an integer value at the console and it converts an integer type data into a double using the toString.... It converts a double into a string and also vice-versa. Here is the code
Convert String To Double
numeric type string value:" message and it converts into a double...; In this section, we are going to convert a numeric type string value into a double. The double supports big-integer value.  Code Description
Convert Decimal to Integer
an integer value as a string. This integer class wraps a value of the primitive... type. The intValue() method converts an int to a string and string to an int... Convert Decimal to Integer      
Integer exception in java
;    The integer class is a wrapper for integer value... a value other than integer, exception will caught in the catch block and println...;} }   Output of the program Enter the integer value
Could someone Possibly help me with this
Could someone Possibly help me with this   think of a number... { public static void main (String args[]) { //generate number in range 1... NewClass1{ public static void main (String args[]){ int randomNumber = new
String slice method example
is that slice() can take negative integer parameters. If passing parameters value...; <![CDATA[ private function init():void{ var str:String... .style1 { font-size: medium; } String slice() method example
how to search for string using LIKE operator instead of integer?
how to search for string using LIKE operator instead of integer?  ... that. but as you see, first query needs integer and second one needs string. i...;input type="text" name="id"> <input type="submit" value="Search">
String Validation
String Validation  Hi. I have a HTML coding in that I have to check whether the value given in the 2 password fields are same or not. Its...;script> function validate(){ if(document.f.new.value!=document.f.confirm.value
String Validation
String password Validation  Hi. I have a HTML coding in that I have to check whether the value given in the 2 password fields are same or not. Its...; <script> function validate(){ if(document.f.new.value
String Validation
String Validation changing password  Hi. I have a HTML coding in that I have to check whether the value given in the 2 password fields are same...;html> <script> function validate(){ if(document.f.new.value
String Validation
String Validation  Hi. I have a HTML coding in that I have to check whether the value given in the 2 password fields are same or not. Its...;script> function validate(){ if(document.f.new.value!=document.f.confirm.value
Write String in Word document
Write String in Word document  How to read and write strings in word document
Convert Integer to Float
() method. This method specified the string value signed an integer which is converted into a float format. toString():  This method converts an integer... a integer value! 24 Float:=24.0 C:\corejava>
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
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
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

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.