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)      5 years and 10 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.

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...

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.
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
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
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
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
Convert a String into an Integer Data
Convert a String into an Integer Data  ... a string type data into an integer type. The java.lang package provides the functionality to convert the string type data into an integer type data. Code
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
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
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
Converting a String to Integer in Java
Converting a String to Integer in Java In this video tutorial I will show you 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
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 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
Write String in Word document
Write String in Word document  How to read and write strings in word document
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
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
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 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
change value of mutable string
change value of mutable string  How to change value of mutable string
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
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 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
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
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
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
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  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
Tutorials