How do I cast Stream.empty() in Java 8? [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 7 years ago.
Improve this question
All the examples I've found use other type information to infer the type of Stream.empty(). It seems like there should be syntax to allow me to cast it directly. This example works:
import java.util.*;
import java.util.stream.*;
class OptionalBasics {
static void test(Optional<String> optString) {
if(optString.isPresent())
System.out.println(optString.get());
else
System.out.println("Nothing inside!");
}
public static void main(String[] args) {
Stream<String> s = Stream.empty();
test(s.findFirst());
test(Stream.of("Epithets").findFirst());
}
}
/* Output:
Nothing inside!
Epithets
*/
But notice that I have to create s separately in order to provide type information for Stream.empty(). I'd like to just create it in the call to
test(), something like this (which doesn't work):
test(Stream<String>.empty().findFirst());
Is there a syntax for setting the type on the call to Stream.empty()?

You just have the brackets in the wrong spot. Try Stream.<String> empty().findFirst())

Related

in Java I get an error, that says close parenthesis is expected [closed]

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
my Main class look like the following:
public class Main {
public static void main(String[] args)
{
Account account=new Account();
account.deposit(amount:10);
System.out.println(account.getBalance());
}
}
and Account class as below:
public class Account {
private float balance;
public void deposit(float amount){
if(amount>0)
balance+=amount;
}
}
I get an java ) expected error, which refers to the line "account.deposit(amount:10);" in Main class and hints, that amounts can not be resolved, but i dont understand why, could you give me some hint.
Change
account.deposit(amount:10);
to
account.deposit(10);
or
account.deposit(10.0f);
You can't label parameters that way at the caller's site. Also, don't forget to implement getBalance(). And use brackets ({}) even when they're optional.

JavaFX function takes any javaFX component? [closed]

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 wanna make a function that takes any javaFX element and sets an onMouseEntered event.
I tried to use generic type and extended it with only javaFX textfield but when i try to access the setOnMouseEntered method it says "Cannot resolve method 'setOnMouseEntered' in 'T'". How can i do that ?
public class sceneHandler<T extends javafx.scene.control.TextField> {
public <T> void onMouseUI(T obj){
obj.setOnMouseEntered(e->{
// Do something
});
}
}
Use the Node class, which is the base class for all JavaFX components:
public void onMouseUI(Node node){
node.setOnMouseEntered(e -> {
// Do something
});
}

Workaround for stupid class names [closed]

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 7 years ago.
Improve this question
In our company, we have a rule which says that all files must have a 8 letters prefix in their names.
For instance, Blarghhh_MyFile.java.
Because of Java's limitation that a public class must have the same name of its file, our classes have stupid names. For instance:
Blarghhh_MyClass myObject = new Blarghhh_MyClass();
Instead
MyClass myObject = new MyClass();
Does someone know a workaround to solve that?
You cannot have an alias for classes in Java. But you could simple create a new class and let it extend your old. Something like:
MyLongNameClass.java
public class MyLongNameClass{
//stuff
}
WorkingClass.java
public class WorkingClass {
private void coolFunc(){
MyClass mc = new MyClass();
}
private class MyClass extends MyLongNameClass{}
}
But i would rather type the whole name then do it that way.

NoSuchMethodException in Intellij in basic project [closed]

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 7 years ago.
Improve this question
I just started to learn Java and I want to use Intellij however I'm not able to run any project(even a simple Hello World). I always get this Exception.
Please follow proper syntax for the main method . .
public static void main(String[] args) {
//Your code
}
You have used method name as Main change it to main it will work.
You have a typo in the method name. It must be 'main' always because Java is case sensitive and JVM always looks for a 'main' method to run.
Try this:
public static void main(String[] args) {...}

no arg method compiler error not generated [closed]

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 8 years ago.
Improve this question
Here is my code:
public class XYZ {
public static void overLoaded(Object a) {
System.out.println("IN OBJECT");
}
public static void main(String[] args) {
overLoaded();
}
}
I expected compile time error as there is no arg method but instead the output is coming out to be IN OBJECT.
Please explain the output.
I just tried your code and am getting:
method overLoaded in class XYZ cannot be applied to given types;
overLoaded();
^
required: Object
found: no arguments
reason: actual and formal argument lists differ in length
1 error
So, if you are getting the behaviour you describe, either your compiler is seriously broken or the code you've given us is not the code you have.

Categories

Resources