-2

I want to make an arraylist, and then make a new one in which all the numbers are changed to hex. Here is the code I have, but when it is supposed to print the hex version, its just blank.

import java.util.Arrays;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Collections;


public class exp2{
  public static void main(String[] args){
    ArrayList<Integer> digits = new ArrayList<Integer>();
    digits.add(3);
    digits.add(1,8); //at index 1, add an 8 
    System.out.println(digits);

    ArrayList<Integer> dubs = new ArrayList<Integer>();
    Scanner s = new Scanner(System.in);

    while (s.hasNextInt()){
      dubs.add(s.nextInt());
    }
    while(!s.hasNextInt()){
      Collections.sort(dubs);
      System.out.println(dubs);
      break;
      }




  ArrayList<String> dubstoHex = new ArrayList<String>();
  int i;
  for (i = 0; i < dubstoHex.size(); i++){
    String.format("0x%08X", i);
  }
  System.out.println(dubstoHex);
  }

}

I tried referencing "dubs" in the arens after but that whacked out the compiler. I also tried having dubs.get(i) as the second argument in that String.format... but that didn't do either.

user311302
  • 97
  • 5

1 Answers1

1

Here is a much simpler version of what I think you are trying to do:

public class FunctionalHex {
    public static void main(String[] args) {
        List<Integer> l = Arrays.asList(3, 1, 8);
        List<String> hex = l.stream().map(i -> String.format("0x%08X", i)).collect(Collectors.toList());

        System.out.println(hex);
    }
}
clay
  • 18,138
  • 28
  • 107
  • 192