[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
Solved

Programming questions

Posted on 2006-05-22
Medium Priority
252 Views
I need to write a program where you enter a decimal number:

1234.1214

and the program returns the numbers in  front of the decimal point and the numbers after the decimal point so the out put would look like this

a= 1234
b= 1214

0
Question by:tkschultz1207

LVL 86

Assisted Solution

CEHJ earned 1000 total points
ID: 16736127
Use String.split
0

Author Comment

ID: 16736971
How do you use String.split?
0

LVL 86

Expert Comment

ID: 16736987
String tokens[] = "1234.1214".split("\\.");
String a = tokens[0];
String b = tokens[1];
0

LVL 92

Expert Comment

ID: 16738263
Use parseInt() to parse the values

int a = Integer.parseInt(tokens[0]);
int b = Integer.parseInt(tokens[1]);

You could also use a StringTokenizer

String s = "1234.5678";
StringTokenizer st = new StringTokenizer(s, ".");
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
0

LVL 9

Accepted Solution

matthew016 earned 1000 total points
ID: 16747981
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split  method of String or the java.util.regex package instead.

The following example illustrates how the String.split method can be used to break up a string into its basic tokens:

String[] result = "this is a test".split("\\s");
for (int x=0; x<result.length; x++)
System.out.println(result[x]);

0

LVL 86

Expert Comment

ID: 16758650
:-)
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month19 days, 9 hours left to enroll