Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Convert String to integer

Posted on 2002-04-02
2
Medium Priority
?
435 Views
Last Modified: 2010-03-31
have a String.. something like "abc123", need to convert it into another String which contains only numeric characters. ie. the result should be "123"
0
Comment
Question by:whdev1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 6913795
StringBuffer sb = new StringBuffer("abc123");

for (int i=0;i<sb.length();i++) {
   if (sb.charAt(i) < 30 || sb.charAt(i) > 39) {
       sb = sb.deleteCharAt(i);
   }
}

System.out.println(sb.toString());


0
 
LVL 18

Accepted Solution

by:
bobbit31 earned 200 total points
ID: 6913822
actually, try this (i tested this one ;))

String s = new String("abc1234567890");
String strReturn = "";

for (int i=0;i<s.length();i++) {
  if ((int) s.charAt(i) >= 48 && (int) s.charAt(i) <= 57) {
      strReturn += s.charAt(i);
  }
}

System.out.println(strReturn);
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question