Not able to parse data to a class in Java [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to learn object oriented code in Java, and am following a tutorial. I am currently stuck trying to parse a string into my class. It is returning the following error:
Name cannot be resolved to a variable
I have a main file, called start.java, and the class I am trying to call is in a different file, called phone.java. Both are in a folder called src. Below is the start.java code (which is throwing the error)
package src;
public class Start {
public static void main(String[] args){
phone android = new phone(Name:"android 10");
System.out.println(android.getName());
}
}
And here is the class I am trying to call, in phone.java
package src;
public class phone{
private String name;
public phone(String name) {
this.name = name;
}
public String getName(){
return this.name;
}
}
Much thanks for your help

You need to remove the Name from new phone(Name:"android 10") and need to use new phone("android 10").
You just need to pass the value for the name, your constructor will bind it to the name variable.
Refer below code
public class Start {
public static void main(String[] args){
phone android = new phone("android 10");
System.out.println(android.getName());
}
}

Related

Inherit ArrayList to child class [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I would like to inherrit the rooms variable just as foo is being inherited. I have searched around google and stackoverflow but couldn't find relevant posts. It may be a short description of the question but I don't think anything more is needed.
Code
import java.util.ArrayList;
class HomeAbstraction { // HomeAbstraction = Rooms; It's named like this to fit the rest of the project
float area;
public HomeAbstraction(float area) {
this.area = area;
}
}
class Home {
String foo = "bar";
ArrayList < HomeAbstraction > rooms = new ArrayList < HomeAbstraction > ();
// setter
public void setRooms(HomeAbstraction rooms) {
this.rooms.add(rooms);
}
}
class Department extends Home {
// constructor
public Department() {
foo = "foo"; // works fine
rooms; // should be pulled from parent but isn't working
rooms.setRooms(new HomeAbstraction(50)); // Not sure if I can access the setter from this child class
}
}
Error log
idk.java:25: error: cannot find symbol
rooms.setRooms(new HomeAbstraction(50));
^
symbol: method setRooms(HomeAbstraction)
location: variable rooms of type ArrayList<HomeAbstraction>
1 error
You can try:
rooms.add(new HomeAbstraction(50));
or
super.setRooms(new HomeAbstraction(50));
or
super.rooms.add(new HomeAbstraction(50));

Java: Access method of another instance of same class [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm new to this so sorry if i don't explain it great.
I have 2 class Interface and Depot.
I can call depot1.getName() and depot2.getName() in Instance class.
But I can't call them in Depot class as I am trying to check no instance of that class has the entered name:(tempname.equals(depot1.getName()) || tempname.equals(depot2.getName()))
Could it be because depot1 and depot2 haven't been created yet?
Here is getName
public String getName(){
return name;
}
I think it may have to do with the fact that depot2 may not exist so i tried:
(depot2 != null && tempname.equals(depot2.getName()))
but that still gives more erors and won't let me compile
I am getting the following error "cannot find symbol - variable depot1"
Can I use isInstance? https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#isInstance%28java.lang.Object%29
Any help would be greatly appreciated, Thanks
Looks like your code don't have instance of depot1 create a instance using new like following snippet: This is not exact answer but it will help you.
package com.test;
class Depot
{
private String name;
public String getName()
{
return name;
}
public void setName( String name )
{
this.name = name;
}
}
public class Test
{
public static void main( String[] args )
{
Depot depot1 = new Depot();
depot1.setName( "depot1" );
Depot depot2 = new Depot();
depot2.setName( "depot2" );
// Rest of code
//(tempname.equals(depot1.getName()) || tempname.equals(depot2.getName()))
}
}

A good way to proof to my teacher that java streams can have worse performance than a simple for [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
My teacher is convinced that it is convenient to use streams when looping over a list because operations can be parallelized.
I understand that this is true in some ways, but I think that we could always implement a faster code writing it by ourselves.
We are talking here of use cases where we want to optimize as much as we can.
Suppose that we start with the following code:
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;
public class HelloWorld{
public static void main (String args[]) {
List<Something> ls = new ArrayList<Something>();
ls.add(new Something(true));
ls.add(new Something(false));
System.out.println("Active things: " + getActive(ls).size());
}
public static List<Something> getActive(List<Something> listOfThings){
return listOfThings != null? listOfThings.stream().filter(t -> t.isActive()).collect(Collectors.toList()): null;
}
public static class Something {
public Something(boolean active) {
this.active = active;
}
private boolean active;
public boolean isActive() {
return active;
}
public void justDoIt() {
System.out.println("done");
}
}
}
Isn't true that, the method getActive() can be optimized avoiding the use of streams ?
I understand that it is easier to use streams, but because they have to be general purpose, they will never be faster than well written and optimized code.
For example, if the list is very big and we know that it would be convenient to parallelize the loop on three cores, couldn't we just execute in three different threads the loop with a standard iterator?

Java singleton - null static variable (can't explain) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
Can someone please explain how is possible, that method obtain(..) throws IllegalStateException for input ConfiguratorType.SKODA (the variable configurators contains {SKODA=null})? How can it be null, I do not understand why SkodaConfigurator.INSTANCE returns null. It should never be null or am i mistaken? The code is executed in servlet environment, Java 7.
Thank you
public class CarConfigurators {
private static Map<ConfiguratorType, CarConfigurator> configurators
= new EnumMap<ConfiguratorType, CarConfigurator>(ConfiguratorType.class);
static {
configurators.put(ConfiguratorType.SKODA, SkodaConfigurator.INSTANCE);
// ..
}
public static CarConfigurator obtain(ConfiguratorType type) {
CarConfigurator configurator = configurators.get(type);
if (configurator == null)
throw new IllegalStateException("Car configurator of type " + type + " is not registered.");
return configurator;
}
...
}
public class SkodaConfigurator extends CarConfigurator {
public static final SkodaConfigurator INSTANCE = new SkodaConfigurator();
...
}
public enum ConfiguratorType {
SKODA,
// ..
}
Static code cannot all run simultaneously, the various bits of static initialization going on have to happen in a given order. Clearly in this case, your static block which does configurations.put(...) is running before the static variable in SkodaConfiguration is initialized.
This is related to static initialization order.
I found this from another answer
public class Main {
{
System.out.printf("NON-STATIC BLOCK\n");
}
static{
System.out.printf("STATIC BLOCK\n");
}
public static Main m = new Main();
public Main(){
System.out.printf("MAIN CONSTRUCTOR\n");
}
public static void main(String... args) {
//Main m = new Main();
System.out.printf("MAIN METHOD\n");
}
}
Output :
STATIC BLOCK
NON-STATIC BLOCK
MAIN CONSTRUCTOR
MAIN METHOD
Please go through this : Java Static Initialization Order

What to do with classes within classes? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am currently creating a events recorder GUI program. Yet I am encountering a very difficult problem.
How should I store objects within an object within an object within an object?
For example,
I have an event.
This event has 4 category.
In the first category (Category A), there are 30 exhibition shows.
Within each show, there are 20 - 30 representatives. (Let's say 30 reps for the first show).
...
How can I store all these information in an arraylist? OR is there any other better idea?
Should I also apply Polymorphism to this one too?
Event --> Category A (first one out of the four) --> First Show out of 30 --> 1 rep out of 30 reps --> ... etc.
Thanks.
My confusion is that I would like to treat every single of these as an object. For example, category is an object. The show is an object. The reps is an object. My question is how can I store an object within an object within an object and so on? Thanks.
try this
Test.java
import java.util.List;
public class Test {
public List<Category> category;
}
import java.util.List;
Category.java
public class Category {
public List<Exhibition> exhibitionShow;
public void setExhibitionShow(List<Exhibition> exhibitionShow) {
this.exhibitionShow = exhibitionShow;
}
public List<Exhibition> getExhibitionShow() {
return exhibitionShow;
}
}
Exhibition.java
import java.util.List;
public class Exhibition {
public List<Representative> representative;
public void setRepresentative(List<Representative> representative) {
this.representative = representative;
}
public List<Representative> getRepresentative() {
return representative;
}
}
Representative .java
public class Representative {
//add method
}
I am not sure what is your confusion, you need to do something like this
class Event{
Category[] categories;
}
class Category{
ArrayList<Show> shows;
}
class Show{
ArrayList<Representative> reps;
}
//.. and so on.
I think you have the idea. My idea is create one public method.

Categories