Simple Java Problems That Can Drives You Crazy If You Don’t Know How To Deal With
Tags: java, beginners, programming
Published: 2023-06-06 03:43:28

Finally I got a chance to write something again after a while. So, for this time will share you interesting things about Java. Well, not so interesting, for some it can be devastating, perhaps.

After I spent my lifetime surfing in Java World, I’ve found a unique problem with this language. The first time met these, It drove me crazy and the I realize now, those problem are just a simple things you will meet in your stepping stone in your journey in Java World.

So, I will share to you, my moment of ‘horrifying’ journey in Java World in this short article. I will list the moments by a list of problems.

# #1 : How To Input Something From Terminal?
If you have experience in C++, to do input task is an easy thing. you can just do in by using `std::cin << var_name;` or `cin << var_name;` if you already include the standard namespace. In Java, you need to import a library to do so.

```java
import java.util.Scanner;	// Scanner class that is necesserely to import

class Program {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);  // Scanner Object
    System.out.println("Input your name!");

    String userName = input.nextLine();  // Read user input
    System.out.println("Username is: " + userName);  // Output of user input
  }
}
```

So, the first thing you need to do is importing the Scanner class, declare an object, that use it’s function to input something.

# #2 : Why My String Input Only Output the First Word of the Sentence?
This is the next problem that often happening when you learn Java. When you input something like “I Love You”, why the output only shows “I”? Where is the rest?

```bash
Input: I love you
Output: I
```

The most common cause of this, you are mistaken of using function next() instead of `nextLine()`.

```java
import java.util.Scanner;

class Program {
  public static void main(String[] args) {
	  Scanner input = new Scanner(System.in);
    System.out.print("Input : ");
    String s = input.next();  // If you use this you won't get the whole of the sentence
    
    System.out.println("Output : " + s);
  }
}
```

If you use the code above you will mess your task. But yes you can still use the code above to print a sentence. But, you need to set a delimiter for it which we won’t talk about it this time.

You can fix this problem by changing `next()` with `nextLine()`.

```java
import java.util.Scanner;

class Program {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Input :");
    String s = input.nextLine();  // You can use this instead of next()

    System.out.println("Output : " + s);
  }
}
```

# #3 : Why My String Input is Skipped After I input Other Variables?

If you are using the code bellow, you will get this kinda nightmare.

```java
import java.util.Scanner;

public class Program {
  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int i = scan.nextInt();
    Double d = scan.nextDouble();
    String s = scan.nextLine();  // This section will be skipped
    scan.close();
    
    System.out.println("String: " + s);
    System.out.println("Double: " + d);
    System.out.println("Int: " + i);
  }
}
```

This is the simplest way I’ve found to fix this problem.

```java
import java.util.Scanner;

public class Program {
  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int i = scan.nextInt();
    Double d = scan.nextDouble();
    scan.nextLine();  // You just need to add this
    String s = scan.nextLine();
    scan.close();
    
    System.out.println("String: " + s);
    System.out.println("Double: " + d);
    System.out.println("Int: " + i);
  }
}
```

You just need to call `nextLine()` function before you do a string input. Problem solved!

# #4 : What Is Package?
The simplest explanation of what package in Java is the therm “Package” in Java is refer to a sub folder inside your project main directory. Mostly, Java source codes in a single project are stored inside `src/` or `src/java` folder.

For example, in you have a Java class named Example.java inside `src/java/example` folder. If you want to import this class to your main class, you must add this import statement to the main file :

```java
import example.Example;
```

The `.` acts just like slash or backslash in directory. And why you just declare `example` instead of `src.java.example`? Because, usually, it has been declared in the root path of the project, specially if you are using any kind of IDE.

# #5 : What IDE is the Best?

Well, it’s rely on yourself. You can use various kind of IDE depend on your taste. If you want a classic one, you can use Eclipse, but for me it’s a bit boring. If you want the new-fashioned one, you can use IntellIJ. NetBeans is OK too. Or, if you are using Raspberry Pi to develop Java, you can use BlueJ.

It’s a personal reference if we talk about IDE.