Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I wanna achieve the following result while using printf, so letters are stored in arrayList of characters and doubles are stored in ArrayList of doubles.


a1,c3,e2.5,f3,c2,B0.5,D3,b1.5,C2,f3

What I have tried:

package sample.com;
    
    import java.lang.reflect.Array;
    import java.util.*;
    import java.util.ArrayList;
    public class Calculation {
        Random ran = new Random();
        ArrayList<Character> randomCharactersArray = new ArrayList<>();
        ArrayList<Character> charNotUsedArray = new ArrayList<>();
        ArrayList<Double> randomNumberArray = new ArrayList<>();
        char[] allowableCharacter = {'A', 'B', 'C', 'D', 'E', 'F', 'a', 'b', 'c', 'd', 'e', 'f'};
    
        //method to generate 10 characters.
        public void randomCharacters(int num) {
            for (int i = 0; i < num; i++) {
                char randomizedLowerCharacters = (char) (ran.nextInt(num) + 'a');
                char randomizedUpperCharacters = (char) (ran.nextInt(num) + 'A');
                randomCharactersArray.add(randomizedLowerCharacters);
                randomCharactersArray.add(randomizedUpperCharacters);
            }
    
            System.out.println(" Random Values are " + Arrays.toString(randomCharactersArray.toArray()));
        }
    
        public double randomNumbers(){
            double number = 0;
            double max = 3.0;
            double min = 0.5;
    
            for(int i =0; i < randomCharactersArray.size(); i++){
    //           number = ((Math.random() * 3.0 - 0.5) + 0.5);
                number = ran.nextDouble(max -min) + min;
                randomNumberArray.add(number);
    //            System.out.printf("%.1f the number is \n",number);
            }
            return number;
        };
    
    
        public void displayMessage(){
    //        randomNumberArray.forEach(number->{
    //            System.out.printf("%.1f %.1d The number is " +  Arrays.toString(randomNumberArray.toArray()));
    //        });
            for(int i = 0; i < randomNumberArray.size(); i++){
                if(randomCharactersArray.get(i) == randomCharactersArray.get(randomCharactersArray.size()-1)){
    //                System.out.printf("'%s'",randomCharactersArray.get(i));
                    System.out.print(randomCharactersArray.get(i));
                }else{
                    System.out.print(randomCharactersArray.get(i) + " ,");
    //                System.out.printf("%s,",randomCharactersArray.get(i));
                }
    
            }
    //        System.out.print("%.1f %.1d The number is " +  Arrays.toString(randomNumberArray.toArray()));
    
        }
    
        public void charactersNotUsed(){
            char charNotUsed = 0;
            for(int i = 0; i < randomCharactersArray.size(); i++){
                if(!randomCharactersArray.contains(allowableCharacter[i])){
                    charNotUsed = allowableCharacter[i];
                    charNotUsedArray.add(charNotUsed);
                    System.out.println("\nchar Not Used " + charNotUsed);
                }
            }
            System.out.printf("\n%s,Letters not used:",  Arrays.toString(charNotUsedArray.toArray()));
    //        System.out.println(" Characters not used" + Arrays.toString(charNotUsedArray.toArray()));
        }
    
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    //        for(int i=0; i < randomCharactersArray.size(); i++){
    //            Double myDouble = Double.valueOf(randomCharactersArray.get(i));
    //            doubleRandomArr.add(myDouble);
    ////            System.out.printf("%.1f\n",RANDOM_VALUE);
    //        }
Posted
Comments
OriginalGriff 19-Jan-22 0:56am    
And?
What does it do that you didn't expect, or not do that you did?
What have you tried to do to find out why?
Are there any error messages, and if so, where and when? What did you do to make them happen?
What help do you need?

Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900