1

I have a program where I receive a long string in the format

characters$xxx,characters$xx,characters$xx, (....)

x is some digit of some integer with an arbitrary number of digits. The integer values are always contained within $ and ,.

I need to extract the integers into an integer array then print that array. The second part is easy, but how to extract those integers?

an example string: adsdfsh$1234,khjdfd$356,hsgadfsd$98,ghsdsk$4623, the arraay should contain 1234, 356, 98, 4623

below is my basic logic

    import java.util.Scanner;

class RandomStuff {
    
    
    public static void main (String[]args){
       
     Scanner keyboard = new Scanner(System.in);
 
     String input = keyboard.next();
     int count =0;
     // counts number of $, because $ will always preceed an int in my string
     
     for(int i=0;i<input.length();i++ ){
         if (input.charAt(i)=='$')
             count++;}
     /* also I'm traversing the string twice here so my complexity is at least 
     o(2n) if someone knows how to reduce that, please tell me*/

     int [] intlist = new int[count];
     
     // fill the array
     int arrayindex =0;
     for (int i=0; i<input.length();i++){
         
     if (input.charAt(i)=='$'){
         /*insert all following characters as a single integer in intlist[arrayindex]
     until we hit the character ','*/}
     
      if (input.charAt(i)==','){
      arrayindex++;
      /*stop recording characters*/}
         
     }
     
     // i can print an array so I'll just omit the rest    
keyboard.close();
}
raxnocafi
  • 79
  • 7

3 Answers3

2

You can use a regular expression with a positive lookbehind to find all consecutive sequences of digits preceded by a $ symbol. Matcher#results can be used to get all of the matches.

String str = "adsdfsh$1234,khjdfd$356,hsgadfsd$98,ghsdsk$4623";
int[] nums = Pattern.compile("(?<=\\$)\\d+").matcher(str).results()
                    .map(MatchResult::group)
                    .mapToInt(Integer::parseInt).toArray();
System.out.println(Arrays.toString(nums));
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
  • Pattern MatchResult and Arrays seem to have dependencies. do I need imports? pasting this my IDE won't compile it – raxnocafi Nov 03 '21 at 17:21
  • @raxnocafi `import java.util.regex.*; import java.util.*;` and then organize imports. – Unmitigated Nov 03 '21 at 17:21
  • everything works, and I think this traverses the string only once? – raxnocafi Nov 03 '21 at 17:24
  • @raxnocafi Ideally that would be the case, but it might backtrack. See https://stackoverflow.com/questions/9011592/in-regular-expressions-what-is-a-backtracking-back-referencing – Unmitigated Nov 03 '21 at 17:25
0

It can done like this

var digitStarts = new ArrayList<Integer>()
var digitsEnds = new ArrayList<Integer>()

// Get the start and end of each digit
for (int i=0; i<input.length();i++){
   if(input[i] == '$' ) digitsStarts.add(i)
   if(input[i] == ',') digitEnds.add(i)
}

// Get the digits as strings
var digitStrings = new ArrayList<String>()
for(int i=0;i<digitsStart.length; i++ ) {
    digitsString.add(input.substring(digitsStarts[i]+1,digitEnds[i]))
}

// Convert to Int
var digits = new ArrayList<Int>
for(int i=0;i<digitString;i++) {
  digits.add(Integer.valueOf(digitStrings[i]))
}


Arthur Klezovich
  • 2,595
  • 1
  • 13
  • 17
0

In a very simple way:

     public static void main(String[] args) {

        String str = "adsdfsh$1234,khjdfd$356,hsgadfsd$98,ghsdsk$4623";
        
        String strArray[] = str.split(",");
        
        int numbers[] = new int[strArray.length];
        int j = 0;
        for(String s : strArray) {
            numbers[j++] = Integer.parseInt(s.substring(s.indexOf('$')+1)); 
        }
        for(j=0;j<numbers.length;j++)
            System.out.print(numbers[j]+"  ");
    }

OUTPUT: 1234 356 98 4623

Jimmy
  • 995
  • 9
  • 18