-5

We have a class named as Show.

Can anybody tell me how to convert data which is String into generic type? For example if we have

String sn=null;

and we want to cast it into Show then how we can do it?

Pshemo
  • 122,468
  • 25
  • 185
  • 269
S1234
  • 1
  • 3
  • "*I am new in java so the question may be little unclear*" yes it is unclear. It also looks like [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) so consider adding more informations about what you really want to achieve (to add more informations to your question simply use [edit] option). – Pshemo Jul 01 '15 at 11:06
  • why do you think it is possible to cast a null String to an Object? it is not. – Sharon Ben Asher Jul 01 '15 at 11:17
  • @Pshemo We have a class named as show which have String member as ShowName i.e. sn what we have to do is to store that variable in list but list is of type show then how to store show names to the list – S1234 Jul 01 '15 at 11:25
  • @sharonbn sn holds the values of shownames – S1234 Jul 01 '15 at 11:26
  • then give a complete example, not one with null. show the value of sn and what is expected to become of them – Sharon Ben Asher Jul 01 '15 at 11:27
  • @sharonbn we have a class as Show. If I want to create a list of Showname where showname is of type string and list type is Show i.e. List – S1234 Jul 01 '15 at 11:33
  • still not clear, and you dont need to post the comment on every answer. like i said, post the relevant code (Show class, value of sn and what is the expected contents of the list) but since you accpeted an answer - let the writer of that answer cotinue the discussion – Sharon Ben Asher Jul 01 '15 at 11:37
  • You can't store `String` in `List`. What you need is `List`. If `List` already contains some `Show` objects you need to iterate over them, read its show names, and store in separate list of Strings. You can do it simply in Java 8 using streams like `List showNames = listOfShows.stream().map(show -> show.getShowName()).collect(Collectors.toList());`. – Pshemo Jul 01 '15 at 11:39
  • @Pshemo But List does not hold shownames as shownames is type of String and not of show – S1234 Jul 01 '15 at 11:44
  • Yes, `List` doesn't hold names of shows explicitly, but since it holds instances of `Show` which holds `ShowName` you should be able to first grab each instance and read its showname. Try this approach and if you will have problem post your code and error message. – Pshemo Jul 01 '15 at 11:48
  • @Pshemo okay I will try it – S1234 Jul 01 '15 at 11:50
  • @Psehmo I have one code for (Iterator iter = list.listIterator(); iter.hasNext();) { Show a = iter.next(); if(a.getShowName().equals(showname)) { chk=showname; } } if(chk.equals(showname)) { System.out.println("Valid"); } else { System.out.println("Invalid"); } showname is name of show which we are acception from user but whenever we print invalid show name it does not print "Invalid" rather it gives nullpointerexception – S1234 Jul 02 '15 at 04:33
  • Your code doesn't look very related to your current question. If you have new problem [create new question](http://stackoverflow.com/questions/ask). Also provide [minimal but full code example which will let us reproduce your problem](http://sscce.org/). Also before that read ["What is a Null Pointer Exception, and how do I fix it?"](http://stackoverflow.com/q/218384/1393766). – Pshemo Jul 02 '15 at 11:09

1 Answers1

0

your question is really very unclear. but if you are talking about converting data in String format to primitive data types then you can use parser. like if you want to convert "9" which is basically a string to int then you can use

int i = Integer.parseInt("9");
  • we have a class as Show. If I want to create a list of Showname where showname is of type string and list type is Show i.e. List – S1234 Jul 01 '15 at 11:33
  • I have one code for (Iterator iter = list.listIterator(); iter.hasNext();) { Show a = iter.next(); if(a.getShowName().equals(showname)) { chk=showname; } } if(chk.equals(showname)) { System.out.println("Valid"); } else { System.out.println("Invalid"); } showname is name of show which we are acception from user but whenever we print invalid show name it does not print "Invalid" rather it gives nullpointerexception – S1234 Jul 02 '15 at 04:33