1
how to convert ascii value of 49 to actual char in java

my code is below and i am trying to store elements from list which is not start with prefix a or g

 void display(){
    String[] inpArray={"apple","orange","grapes"};
    LinkedList<String> listOne = new LinkedList<String>(Arrays.asList(inpArray));
    LinkedList<String> listTwo = new LinkedList<String>();
    listTwo.add("melon");
    listTwo.add("apple");
    listTwo.add("mango");
    String[] result1 = {};
    for(String res : listOne){

    if(res.startsWith("a")||res.startsWith("g")){
        System.out.println("--> "+res);
    }else{
        System.out.println("** "+res);
        //result1 = res;//unable to store in string array or to list
        // here i have to add all strings how to do that please help me
    }
xingbin
  • 27,410
  • 9
  • 53
  • 103
Rocket_03
  • 45
  • 8

2 Answers2

2
Please find the code for storing the element in String array using index i;

package com.test.stackoverflow;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TestClass {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        display();
    }

    public static void display() {
        String[] inpArray = { "apple", "orange", "grapes" };
        List<String> listOne = new ArrayList<String>(Arrays.asList(inpArray));
        List<String> listTwo = new ArrayList<String>();
        listTwo.add("melon");
        listTwo.add("apple");
        listTwo.add("mango");
        String[] result1 = new String[listOne.size()];
        int i =0;
        for (String res : listOne) {

            if (res.startsWith("a") || res.startsWith("g")) {
                System.out.println("--> " + res);
            } else {
                System.out.println("** " + res);
                result1[i++] = res;
            }
        }

        for(String val : result1){
            System.out.println("Value"+val);
        }
    }

}
  • nice bro instead i am done after fetched listone elements i have used add function to merge two list any how you did it even simpler thanks for new logic learned from you...... – Rocket_03 Jun 24 '18 at 15:59
0

If you want to store the elements into an array, you can put them into a List first, then convert the List to array by List.toArray:

LinkedList<String> linkedList = new LinkedList<>();
for (String res : listOne) {
    if (res.startsWith("a") || res.startsWith("g")) {
        System.out.println("--> " + res);
    } else {
        System.out.println("** " + res);
        linkedList.add(res);
    }
}
String[] result1 = new String[linkedList.size()];
linkedList.toArray(result1);
xingbin
  • 27,410
  • 9
  • 53
  • 103
  • Hi John thanks for your time i have updated my code accordingly but getting below error LinkedList linkedList = null;//i have declared new array result1 = linkedList.toArray(new String[0]);//this line throwing null pointer exception – Rocket_03 Jun 23 '18 at 10:08
  • `return linkedList.toArray(new String[linkedList.size()])`. – Boris the Spider Jun 23 '18 at 10:10
  • John let me try again one moment appreciat your time – Rocket_03 Jun 23 '18 at 10:12
  • @BoristheSpider `void display()`, seems the OP does not want to return the array... – xingbin Jun 23 '18 at 10:13
  • getting below out put for result1 system.out.println(result1) displays below result [Ljava.lang.String;@15db9742 – Rocket_03 Jun 23 '18 at 10:14
  • @user2926683 See this https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array – xingbin Jun 23 '18 at 10:15
  • Hi BoristheSpider i just need to store elements from list i am not sure which methos suits ... – Rocket_03 Jun 23 '18 at 10:15
  • I did john but still the output is [Ljava.lang.String;@15db9742 LinkedList linkedList = new LinkedList();//i have declared new array for(String res : listOne){ if(res.startsWith("a")||res.startsWith("g")){ //System.out.println("--> "+res); }else{ //System.out.println("** "+res); result1 = linkedList.toArray(new String[0]);//this line throwing null //pointer exception System.out.println(result1); } – Rocket_03 Jun 23 '18 at 10:17
  • @user2926683 Try this `System.out.println(Arrays.toString(result1));` – xingbin Jun 23 '18 at 10:18
  • Now out put displays only square brackets as below [] – Rocket_03 Jun 23 '18 at 10:21
  • Hi john thanks much all i need to do is to store unmatched elements from list as a begginner i tried to store it into array but storing it into new list easiest way found from you any how i have learned two way of storing from you. thanks a lot. – Rocket_03 Jun 23 '18 at 10:33