-4

For example, if I have
0]+4

How do I read and eat just the zero?

EDIT: So it looks like I've been really unclear. Let me edit things. All I want to do is to get the first integer that appears in a string of digits and characters. The first component of the text will ALWAYS be an integer. I can have any combination of this. For example, I can have any of these:
43SOMETEXT424 3blahblahblah64blahblahblah 5MoreText

I would return 43, 3, and 5 from reading this text, respectively.

dtgee
  • 1,272
  • 2
  • 15
  • 30
  • Integer.parseInt doesn't work, I get a NumberFormatException – dtgee Jan 26 '13 at 04:44
  • See [this earlier answer](http://stackoverflow.com/questions/9381560/parse-multiple-doubles-from-a-string) for doubles... I'm sure you can make it work for integers. – Floris Jan 26 '13 at 04:44
  • 1
    Is it always like the format you specified? – Bhavik Shah Jan 26 '13 at 04:47
  • As I stated before, the format is not always 0]+4, but the first character is always a string. – dtgee Jan 26 '13 at 04:58
  • 1
    You mean "the first character is always a digit". Do you only want the first character converted, or could there be a sequence of digits at the start of the string. The variety of answers you are getting are a result of lack of clarity in the question... This probably also explains some downvotes. – Floris Jan 26 '13 at 05:20
  • Edited my post for clarity. – dtgee Jan 26 '13 at 20:19

6 Answers6

0

This is a bit of a long shot without knowing the context of what you're doing... but this code would work:

Integer.parseInt("0]+4".charAt(0));

If you truly want only the '0' just read the value you have into a String and call Integer.parseInt() on the first character of the String.

rtcarlson
  • 424
  • 4
  • 13
  • The format won't always be 0]+4, but the format will always have a number as the first character. – dtgee Jan 26 '13 at 04:56
  • @user1831442, either answer posted so far will work for you then. Mine is a little more lightweight than the other, but the difference is negligible. – rtcarlson Jan 26 '13 at 04:59
0

try

    int n = NumberFormat.getInstance().parse("0]+4").intValue();

strange as it may seem there will be no ParseException, this is how NumberFormat / DecimalFormat works

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • I saw the javadocs for NumberFormat it says `Parses text from the beginning of the given string to produce a number`. Will it produce `0` or `4` of `04` or `40` is not specified. Are you sure it just produces 0 as output? – Bhavik Shah Jan 26 '13 at 04:51
  • But it also says "The method may not use the entire text of the given string". Try it – Evgeniy Dorofeev Jan 26 '13 at 04:54
0
public static void main(String[] args) {
    String s = "0]+4";
    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        if (Character.isDigit(c)) {

            System.out.println(c);
        }
    }
}

Try this. I think this is what you want...yes?

Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
0
public char getNumber(final String str){
    final String onlyNumbers = str.replaceAll("[^0-9]", "");
    return (char) (onlyNumbers.length() > 0 ? onlyNumbers.charAt(0) - '0' : -1);
}

Consider using regexs. This won't parse negative numbers, but I doubt you would need to find them here.

0

you can use pattern

  Pattern pattern;
   pattern = Pattern.compile("\\d");
   String st=jTextField1.getText();
   Matcher matcher = pattern.matcher(st);
system.out.prinl(matcher.group());
  • I can't seem to get group to work correctly. There is nothing to group, the compiler complains. – dtgee Jan 26 '13 at 20:19
0
public static void main(String[] args){
    Scanner in=new Scanner(System.in)
    int number=in.nextInt();
}

//put loop for nextInt fun as many time as you want to get the required number
Tunaki
  • 132,869
  • 46
  • 340
  • 423
sibi
  • 59
  • 3