This's an exercise from my Java course, I need to print out "Rouge" on the screen but something goes wrong and I can't figure out what it is. Help me, please!
Here is my code:
public class Cercle {
public Couleur couleur;
public static void main(String[] args) {
Cercle cercle = new Cercle();
cercle.couleur.setDescription("Rouge");
System.out.println(cercle.couleur.getDescription());
}
public void Cercle() {
this.couleur = new Couleur();
}
public class Couleur {
String description;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
}
Related
Im creating a Java application, I used enum to create movie category. When I input MovieCategory.WAR I would like to see War movie(My description) instead of WAR. How is it possible? I tried MovieCategory.WAR.getDescription() but does't work.
public enum MovieCategory {
COMEDY("Comedy"), HORROR("Horror"), SCIFI("Sci-Fi"),
ACTION("Action movie"), ROMANTIC("Romantic"),
CLASSIC("Classic"), WAR("War movie");
private final String description;
MovieCategory(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
}
public class MovieManager {
private List<Movie> movieList;
public MovieManager() {
this.movieList = new ArrayList<>();
movieList.add(new Movie("Simple movie", MovieCategory.WAR,"Testing description.",167,12));
(...)
The enum works correctly:
public static void main(String[] args) {
// Displays: Comedy
System.out.println(COMEDY.getDescription());
// Displays: COMEDY
System.out.println(COMEDY);
}
Or, maybe you want the toString method to use description?
public enum MovieCategory {
COMEDY("Comedy");
private final String description;
MovieCategory(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
#Override
public String toString() {
return getDescription();
}
public static void main(String[] args) {
// Displays: Comedy
System.out.println(COMEDY.getDescription());
// Displays: Comedy
System.out.println(COMEDY);
}
}
UPDATE #3: It seems the issue is the JSON response. If you want the description to be returned, you can annotate the getDescription method wtih #JsonValue.
import com.fasterxml.jackson.annotation.JsonValue;
public enum MovieCategory {
WAR("War movie");
private final String description;
MovieCategory(String description) {
this.description = description;
}
#JsonValue
public String getDescription() {
return description;
}
#Override
public String toString() {
return getDescription();
}
public static void main(String[] args) {
// Displays: Comedy
System.out.println(WAR.getDescription());
// Displays: Comedy
System.out.println(WAR);
}
}
This code below keeps throwing the following error:
Java Error Terminal Screen Snapshot
I've tried quite a few online fixes but they don't seem to be working.
My code is below and I've commented the error location(occurs in the driver main function)
NOTE: The code compiles properly if I change public static void main(String args[]) to public void main(String args[]) but when i run it, it throws the error "Change main to static void main". I'm a little stuck here.
import java.util.*;
class Exercise{ //Exercise Begins
class UtilityFunctions{
public String getAuthor(){ return "";};
public String getPublisher(){return "";};
public void display(){};
}
class Book extends UtilityFunctions{
private String title;
private String author;
private String category;
private String datePublished;
private String publisher;
private double price;
public Book(String authorParam, String publisherParam){
author = authorParam;
publisher = publisherParam;
}
//List of Setters
public void setTitle(String title){
this.title = title;
}
public void setCategory(String cat){
this.category = cat;
}
public void setDatePublished(String dp){
this.datePublished = dp;
}
public void setPrice(double p){
this.price = p;
}
//List of Getters
public String getTitle(){
return this.title;
}
#Override
public String getAuthor(){
return this.author;
}
public String getCategory(){
return this.category;
}
public String getDatePublished(){
return this.datePublished;
}
#Override
public String getPublisher(){
return this.publisher;
}
public double getPrice(){
return this.price;
}
#Override
public void display(){
System.out.println("Book Title:" + getTitle());
System.out.println("Author:" + getAuthor());
System.out.println("Category:" + getCategory());
System.out.println("Date Published:" + getDatePublished());
System.out.println("Publisher:" + getPublisher());
System.out.println("Price:$" + getPrice());
}
}
class Author extends UtilityFunctions{
private String authorName;
private String birthDate;
private String publisher;
private String email;
private String gender;
List<Book> bookList;
public Author(String publisherParam){
publisher = publisherParam;
}
public void addBook(Book b){
bookList.add(b);
}
//List of Setters
public void setName(String n){
this.authorName = n;
}
public void setEmail(String em){
this.email = em;
}
public void setGender(String gen){
this.gender = gen;
}
public void setBirthDate(String dob){
this.birthDate = dob;
}
//List of Getters
public String getAuthor(){
return this.authorName;
}
public String getPublisher(){
return this.publisher;
}
public String getEmail(){
return this.email;
}
public String getGender(){
return this.gender;
}
public String getBirthDate(){
return this.birthDate;
}
#Override
public void display(){
System.out.println("Author Name:" + getAuthor());
System.out.println("Email:" + getEmail());
System.out.println("Gender:" + getGender());
System.out.println("BirthDate:" + getBirthDate());
System.out.println("Publisher:" + getPublisher());
System.out.println("BOOKS:");
for(Book b:bookList){
b.display();
System.out.println();
}
}
}
class Publisher extends UtilityFunctions{
private String publisherName;
private String publisherAddress;
private String publisherEmail;
private int publisherPhoneNumber;
List<Author> authorList;
public Publisher(String name, String add, String email,int phone){
publisherName = name;
publisherAddress = add;
publisherEmail = email;
publisherPhoneNumber = phone;
}
public void addAuthor(Author a){
authorList.add(a);
}
//List of Getters
public String getPublisher(){
return this.publisherName;
}
public String getAddress(){
return this.publisherAddress;
}
public String getEmail(){
return this.publisherEmail;
}
public int getPhoneNumber(){
return this.publisherPhoneNumber;
}
#Override
public void display(){
System.out.println("Publisher Name:" + getPublisher());
System.out.println("Publisher Address:" + getAddress());
System.out.println("Publisher Phone Number:" + getPhoneNumber());
System.out.println("Publisher Email:" + getEmail());
System.out.println("AUTHORS:");
for(Author a:authorList){
a.display();
System.out.println("-------------------------------------");
}
System.out.println("--------------------------------------------------------------------------------");
System.out.println("--------------------------------------------------------------------------------");
}
}
public static void main(String args[]){
ArrayList<Publisher> publisherList = new ArrayList<Publisher>();
//1st ERROR HERE
Publisher pub1 = new Publisher("Riverhead Books","8080 Cross Street","riverhead#riverheadbooks.co.uk",784646533);
//2nd ERROR HERE
Author author1 = new Author(pub1.getPublisher());
author1.setName("Khaled Hosseini");
author1.setGender("Male");
author1.setBirthDate("1965-10-09");
author1.setEmail("khaledhosseini#gmail.com");
pub1.addAuthor(author1);
//3rd ERROR HERE
Book book1 = new Book(author1.getAuthor(),author1.getPublisher());
book1.setTitle("Kite Runner");
book1.setCategory("Historical-Fiction|Drama");
book1.setPrice(39.95);
book1.setDatePublished("2003-05-29");
author1.addBook(book1);
publisherList.add(pub1);
for(Publisher p:publisherList){
p.display();
}
}
}//Exercise Ends
All your classes : Book, Author, Publisher, UtilityFunctions ... should be outside the Exercise class
Class Exercise {
public static void main(String args[]) {...}
} // end class exercice
Class UtilityFunctions {...}
Class Book {...}
Class Author {...}
Class Publisher {...}
I am getting a Exception in Thread main
java.lang.NoSuchMethodException: com.laurens.Main.main([Ljava.lang.String;)
at java.lang.Class.getMethod(Class.java:1786)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:125)
Can someone explain where I am going wrong here?
Main
package com.laurens;
public class Main {
private player player;
public Main(com.laurens.player player) {
this.player = player;
}
public com.laurens.player getPlayer() {
return player;
}
public void setPlayer (int performance, String name) {
if (performance < 4) {
boolean injured = true;
}
}
#Override
public String toString() {
return "com.laurens.Main{" +
"player=" + player +
'}';
}
}
player
package com.laurens;
/**
* Created by laurensvanoorschot on 20-01-16.
*/
public class player {
private String name;
private int performance;
private boolean injured;
public player(int performance, boolean injured, String name) {
this.injured = injured;
this.name = name;
this.performance = performance;
}
public boolean isInjured() {
return injured;
}
public void setInjured(boolean injured) {
this.injured = injured;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPerformance() {
return performance;
}
public void setPerformance(int performance) {
this.performance = performance;
}
}
You don't have a method called main, which is what it is looking for to run your program. Notice that when you create a template application for a java console application in intelliJ it has a method:
public static void main(string[] args) {
}
That needs to be there for your program to run.
I am very new to JSON and jackson, currently I have pojo files and I am trying to get the data and store it in an array. for example I want to extract Network name and store it an array and later display or compare it with live site data.
here is the main pojo file -
public class JsonGen{
private String _type;
private List cast;
private List clips;
private Common_sense_data common_sense_data;
private String common_sense_id;
private List crew;
private String description;
private List episodes;
private Number franchise_id;
private List genres;
private String guid;
private Images images;
private boolean is_locked;
private boolean is_mobile;
private boolean is_parental_locked;
private String kind;
private List mobile_networks;
private String most_recent_full_episode_added_date;
private String name;
private List networks;
private List platforms;
private List ratings;
private String release_date;
private List season_filters;
private String slug;
private String tms_id;
public String get_type(){
return this._type;
}
public void set_type(String _type){
this._type = _type;
}
public List getCast(){
return this.cast;
}
public void setCast(List cast){
this.cast = cast;
}
public List getClips(){
return this.clips;
}
public void setClips(List clips){
this.clips = clips;
}
public Common_sense_data getCommon_sense_data(){
return this.common_sense_data;
}
public void setCommon_sense_data(Common_sense_data common_sense_data){
this.common_sense_data = common_sense_data;
}
public String getCommon_sense_id(){
return this.common_sense_id;
}
public void setCommon_sense_id(String common_sense_id){
this.common_sense_id = common_sense_id;
}
public List getCrew(){
return this.crew;
}
public void setCrew(List crew){
this.crew = crew;
}
public String getDescription(){
return this.description;
}
public void setDescription(String description){
this.description = description;
}
public List getEpisodes(){
return this.episodes;
}
public void setEpisodes(List episodes){
this.episodes = episodes;
}
public Number getFranchise_id(){
return this.franchise_id;
}
public void setFranchise_id(Number franchise_id){
this.franchise_id = franchise_id;
}
public List getGenres(){
return this.genres;
}
public void setGenres(List genres){
this.genres = genres;
}
public String getGuid(){
return this.guid;
}
public void setGuid(String guid){
this.guid = guid;
}
public Images getImages(){
return this.images;
}
public void setImages(Images images){
this.images = images;
}
public boolean getIs_locked(){
return this.is_locked;
}
public void setIs_locked(boolean is_locked){
this.is_locked = is_locked;
}
public boolean getIs_mobile(){
return this.is_mobile;
}
public void setIs_mobile(boolean is_mobile){
this.is_mobile = is_mobile;
}
public boolean getIs_parental_locked(){
return this.is_parental_locked;
}
public void setIs_parental_locked(boolean is_parental_locked){
this.is_parental_locked = is_parental_locked;
}
public String getKind(){
return this.kind;
}
public void setKind(String kind){
this.kind = kind;
}
public List getMobile_networks(){
return this.mobile_networks;
}
public void setMobile_networks(List mobile_networks){
this.mobile_networks = mobile_networks;
}
public String getMost_recent_full_episode_added_date(){
return this.most_recent_full_episode_added_date;
}
public void setMost_recent_full_episode_added_date(String most_recent_full_episode_added_date){
this.most_recent_full_episode_added_date = most_recent_full_episode_added_date;
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public List getNetworks(){
return this.networks;
}
public void setNetworks(List networks){
this.networks = networks;
}
public List getPlatforms(){
return this.platforms;
}
public void setPlatforms(List platforms){
this.platforms = platforms;
}
public List getRatings(){
return this.ratings;
}
public void setRatings(List ratings){
this.ratings = ratings;
}
public String getRelease_date(){
return this.release_date;
}
public void setRelease_date(String release_date){
this.release_date = release_date;
}
public List getSeason_filters(){
return this.season_filters;
}
public void setSeason_filters(List season_filters){
this.season_filters = season_filters;
}
public String getSlug(){
return this.slug;
}
public void setSlug(String slug){
this.slug = slug;
}
public String getTms_id(){
return this.tms_id;
}
public void setTms_id(String tms_id){
this.tms_id = tms_id;
}
}
here is the Network Pojo class -
public class Networks{
private String banner;
private String description;
private boolean is_locked;
private String logo;
private String name;
private String network_analytics;
private Number network_id;
private String slug;
private String thumbnail_url;
private String url;
public String getBanner(){
return this.banner;
}
public void setBanner(String banner){
this.banner = banner;
}
public String getDescription(){
return this.description;
}
public void setDescription(String description){
this.description = description;
}
public boolean getIs_locked(){
return this.is_locked;
}
public void setIs_locked(boolean is_locked){
this.is_locked = is_locked;
}
public String getLogo(){
return this.logo;
}
public void setLogo(String logo){
this.logo = logo;
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public String getNetwork_analytics(){
return this.network_analytics;
}
public void setNetwork_analytics(String network_analytics){
this.network_analytics = network_analytics;
}
public Number getNetwork_id(){
return this.network_id;
}
public void setNetwork_id(Number network_id){
this.network_id = network_id;
}
public String getSlug(){
return this.slug;
}
public void setSlug(String slug){
this.slug = slug;
}
public String getThumbnail_url(){
return this.thumbnail_url;
}
public void setThumbnail_url(String thumbnail_url){
this.thumbnail_url = thumbnail_url;
}
public String getUrl(){
return this.url;
}
public void setUrl(String url){
this.url = url;
}
}
and here is my code through which I am trying to extract the network names -
public class util {
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
List<JsonGen> jsongenShow = null;
String url1 = "http://www.dishanywhere.com/radish/v20/dol/home/carousels/shows.json";
getShowNWGopherParser(nwork, url1);
}
public static String[] getShowNWGopherParser (List<Networks> nwork, String url ) throws JsonParseException, JsonMappingException, IOException
{
URL jsonUrl = new URL(url);
ObjectMapper objmapper = new ObjectMapper();
//objmapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
nwork = objmapper.readValue(jsonUrl, new TypeReference<List<Networks>>() {});
String [] shows = new String [nwork.size()];
int i = 0;
for(Networks element : nwork) {
shows[i++]=element.getUrl();
}
for(int j =0; j<shows.length;j++)
{
System.out.println(shows[j]);
}
return shows;
}
}
and here is the error -
Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "networks" (class featureshows.Networks), not marked as ignorable (10 known properties: , "logo", "slug", "name", "banner", "network_id", "url", "network_analytics", "description", "thumbnail_url", "is_locked"])
at [Source: http://www.dishanywhere.com/radish/v20/dol/home/carousels/shows.json; line: 1, column: 15] (through reference chain: featureshows.Networks["networks"])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:79)
at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:568)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:650)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:830)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:310)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:112)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:226)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:203)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:23)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2563)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1789)
at functions.util.getShowNWGopherParser(util.java:77)
at functions.util.main(util.java:31)
The reason is that you're using a regular List interface for your list of networks instead of the generic version with the bounds for the expected type.
Try changing the field declaration from:
private List networks;
to
private List<Networks> networks;
While you're at it, it looks like you're using the regular List interface pretty much everywhere. You'll probably run into more issues if you don't convert them all to include the type you expect in the list.
Essentially, you're not providing enough information about the type of object you expect in the list for jackson to figure out what to populate it with. You can read more about generics here: http://docs.oracle.com/javase/tutorial/extra/generics/intro.html
EDIT:
It looks (from your comment) that you've already tried disabling checks for unknown properties, but try adding this annotation to your Networks class:
#JsonIgnoreProperties(ignoreUnknown = true)
I have two classes: profesor and subject
public class Profesor {
private int numbClassroom;
public Profesor(int numbClassroom) {
this.numbClassroom = numbClassroom;
}
public int getNumbClassroom() {
return numbClassroom;
}
public void setNumbClassroom(int numbClassroom) {
this.numbClassroom = numbClassroom;
}
public String ToString(){
return "Number of classroom: "+numbClassroom;
} }
The second class is:
public class Subject{
String name;
Profesor lecturer = new Profesor();
Date yearOfStudy;
public void Dodeli(Profesor p){
??????
}}
I do not know how to add professor like a lecturer to a current subject
Like this? I don't see any problem.
public void Dodeli(Profesor p){
lecturer = p;
}
Profesor lecturer = new Profesor();
No need to instantiate lecturer. Just declare it. Then have getter/setter methods for it
Then you can assign Professor to Subject
Subject subj = new Subject("OOP"); //assuming you have corresponding constructor
subj.setLecturer(new Professor()); //or if you have existing prof object
Maybe require something like this : try to encapsulate your code
public class Professor {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Subject{
private String name;
private Professor professor;
private int numbClassroom;
private Date yearOfStudy;
public int getNumbClassroom() {
return numbClassroom;
}
public void setNumbClassroom(int numbClassroom) {
this.numbClassroom = numbClassroom;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Professor getProfesor() {
return professor;
}
public void setProfesor(Professor profesor) {
this.professor = profesor;
}
public void Dodeli(){
System.out.println("Pofessor "+getProfesor().getName()+" is teaching "+getName()+" in Room NO :"+getNumbClassroom());
}
}
public class TestImpl {
public static void main(String arr[])
{
Subject subject = new Subject();
Professor professor = new Professor();
subject.setName("Biology");
professor.setName("MR.X");
subject.setNumbClassroom(1111);
subject.setProfesor(professor);
subject.Dodeli();
}
}