how to check if the variable is null? [duplicate] - java

This question already has answers here:
Check whether a String is not Null and not Empty
(35 answers)
Closed 5 years ago.
I know this doesnt work but how can I know if the user added even one char?
public class Program
import java.util.Scanner;
{
public static void main(String[] args) {
Scanner a = new Scanner(System.in);
String b = a.nextLine();
//I know this doesnt work but how can I know if the user added even one char?
if (b!=null){
System.out.println(b);
}
}
}

you can use .equals("") or .isEmpty()
check check if the variable is null

You can do this
if (!b.isEmpty()) {
System.out.println(b);
}

Related

Printing 2 different variables in the same line Java VM [duplicate]

This question already has an answer here:
How to print both bool and double in same line? [duplicate]
(1 answer)
Closed 2 years ago.
I am new to java and i have 2 different Variables one is a double and one is a boolean. I am having difficulty printing them into the same line, this is my code.
class Main {
public static void main(String[] args) {
boolean isTrue;
isTrue = false;
double money;
money = 99999.99;
System.out.println(isTrue, double);
}
}
Thanks a lot if you answer
Many ways to do this and if you visited the javadocs you would see that there is also System.out.print which does not do a line feed, so you could use that as
System.out.print(isTrue);
System.out.println(money);
Or you could use System.out.printf, or convert values into a String first
There are few methods to do this, one is mentioned in the above answer and the other is below, that you can do this with a professional approach by using printf.
public static void main(String[] args) {
boolean isTrue;
isTrue = false;
double money;
money = 99999.99;
System.out.printf("%b%.2f%n", isTrue, money);
}

Print all strings of array [duplicate]

This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 2 years ago.
Very new to Java and programming in general. trying to figure out how to print the contents of my array
public class GameClass {
public static void main(String args[]) {
String [] gameList = new String [] {"Call of Duty", "Skyrim", "Overwatch", "GTA", "Castlevania", "Resident Evil", "HALO", "Battlefield", "Gears of War", "Fallout"};
System.out.println(gameList[]);
}
}
You can use the util package for one line printing
System.out.println(java.util.Arrays.toString(gameList));
Should do the trick.
for(int i = 0 ; i <gameList.length; i ++)
System.out.print(gameList[i]+" ");
Just iterate each element and print:
for(String s : gameList){
System.out.println(s);
}
You can print out your array of games by using the following loop:
for(String game : gameList){
System.out.println(game);
}
This runs through each item in your gameList array and prints it.

Iterating through attributes of DIFFERENT types in a class and printing each [duplicate]

This question already has answers here:
Printing all variables value from a class
(9 answers)
Closed 6 years ago.
I have this simple class:
public class Events{
Boolean wentToGym;
String eventsToday;
public Events(Boolean wentToGym, String eventsToday){
this.wentToGym = wentToGym;
this.eventsToday = eventsToday;
}
Now for some reason I need to iterate through these fields and print them. As they are from different types, I thought I would use the Object declaration in the for-loop:
public static void main(String[] args){
Events events = new Events(true, "first trial");
for(Object e: events.getClass().getDeclaredFields){
System.out.println(e.toString);
}
}
Unfortunately not working because the object has no toString method. It gives me the same result you get when you try printing an array. And I dont know how to cast them inside the loop as they are declared as objects. Whats the best way to iterate through the attributes of a class and print them?
Edit: the question some guys rushed into referring to as duplicated is about attributes all of the same type (Strings), so there is no need for using an object nor the problem is the same. Its always a good idea to try helping by even reading the whole thing even if its harder than feeling important by simply flagging a question.
Is this what you're looking for?
import java.util.Arrays;
public class Events {
Boolean wentToGym;
String eventsToday;
public Events(Boolean wentToGym, String eventsToday) {
this.wentToGym = wentToGym;
this.eventsToday = eventsToday;
}
public static void main(String[] args) {
Events events = new Events(true, "first trial");
Arrays.stream(events.getClass().getDeclaredFields()).forEach(field -> {
try {
System.out.println(field.getName() + ": " + field.get(events));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
});
}
}
Output:
wentToGym: true
eventsToday: first trial

Reading keystrokes from a keyboard and using if statement in Java [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
I am learning how to program in Java. I wrote the following code but, I can't get the if statements to work when the variable inputName is compared to a string and the statement is true. Does anyone have any tips on how to fix this?
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
String x = ("Hello type in your first name.");
System.out.println(x);
Scanner keyboard = new Scanner(System.in);
String inputName = keyboard.next();
if (inputName == "erick") {
System.out.println("Hi");
}
String v = "victor";
if (inputName == v) {
System.out.println("Hi");
}
String c = "christy";
if (inputName == c) {
System.out.println("Hi");
}
keyboard.close();
}
}
don't use == instead use equals() for string comparison.
== will check whether 2 references are referring the same object, while equals() will check the value being referred to.

Strings from input are never equal? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
import java.util.Scanner;
public class Michal {
public static void main(String[] args) {
reply();
}
public static void reply() {
Scanner input=new Scanner(System.in);
String name=input.nextLine();
if(name=="john"){
System.out.println("bear!");
}else if(name=="mary")
{
System.out.println("lovely lady!");
}else{
System.out.println("I don't know that person.");
}
System.out.println(name);
input.close();
}
}
I consider myself still a beginner to Java ,so please don't be harsh in your answers. I was trying to create a program that returns an answer every time it gets a certain input , however it seems to return "I don't know that person" all the time.
if(name=="john")
is not the right way to compare strings. Use equals() instead:
if(name.equals("john")){
System.out.println("bear!");
} else if(name.equals("mary")){

Categories