Home > database >  Java, extract integer values within defined characters from long string
Java, extract integer values within defined characters from long string

Time:11-04

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();
}

CodePudding user response:

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));

CodePudding user response:

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]))
}


CodePudding user response:

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

  •  Tags:  
  • java
  • Related