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...
Ads
Related Tutorials/Questions & Answers:
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
Advertisements
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
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 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
 
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
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
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
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
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
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
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
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
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..... 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