Java: input from Scanner behaves weirdly - java.util.scanner

Just for testing, I have written this very short program:
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
if (input == "y" ) {
System.out.println("Test");
}
}
}
When I enter y, it does not print anything and just normally finishes with exit code 0.
Is this just the case on my machine? If so, why might this be the case?
Or am I making a mistake here? If so, what do I need to do to fix it?
Thank you very much in advance.

Your comparison of strings is where the issue is. Use input.equals("y") instead. See here for details

Related

Scanners method "nextLine" doesn`t allow to input line

import java.io.IOException;
import java.util.Scanner;
public class MainTraining {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
int countOfStrangers = scanner.nextInt();
String[] name = new String[countOfStrangers];
System.out.println(scanner.nextLine());
}
}
It will output nothing.
Why?
that is only first part of my programm, so that separately this code has no sense
As i undersood, there are some troubles if you use nextLine after nextInt. To solve this problem you can read int with nextLine and after that parse it to int. Also you can create two scanners: one for ints and one for Strings
Full answer is here: Why can't I enter a string in Scanner(System.in), when calling nextLine()-method?

How can I use a "String" to compare an "if" and "if else" statement?

The below code works just fine. But how can I get the user to input a string instead of and int to bring up either the "shop" or "inn" method?
ie. When "int a = navigate.nextInt();" is changed to "String a = navigate.nextString();" and the "if" and "if else" conditions are changed to "a == "shop" or "inn". The next line to run the correct method does nothing.
(I hope that made sense, see below)
import java.util.*;
public class ShopTest {
public static Scanner navigate = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Where to?\n Shop (1)\n Inn (2)");
int a = navigate.nextInt();
if (a == 1)
Shop();
else if (a == 2)
Inn();
}
public static void Shop() {
System.out.println("Welcome to my shop\nWould you like to see my wares?");
}
public static void Inn() {
System.out.println("**You enter the inn and approach the barkeep**\nHow can I help you?");
}
}
Try this
System.out.println("Where to?\n Shop \n Inn ");
String a = navigate.nextLine();
if (a.equals("Shop"))
Shop();
else if (a.equals("Inn"))
Inn();
}
Use .equal
== tests for reference equality (whether they are the same object).
.equal tests for value equality (whether they are logically
"equal").
More: How do I compare strings in Java?

Calling Overload Methods

I can't seem to make this work let alone compile and I am at loss at how to fix it. My teacher gave us the following code (simplified for question's sake):
public static void doing1(String s) {
// add code here
}
public static void doing2(char start, char end) {
// add code here
}
public static int doing3(int num) {
// add code here
}
public static void doing4(Scanner keyboard) {
// add code here
}
I know what needs to go in each method (the work I mean) I just don't know how to print it out in the main method. We cannot change the code given to us, only add to it.
Thank you!
Overloading a method means having the same method name but the method signature (the parameters passed in) is different. So, what you have isn't actually an overload, it is for unique methods because they all have different names. As far as not compiling... what you posted looks fine - perhaps you have an error above or below that code. I believe this is what you are looking for:
public static void doing(String s) {
// add code here
}
public static void doing(char start, char end) {
// add code here
}
public static int doing(int num) {
// add code here
}
public static void doing(Scanner keyboard) {
// add code here
}
Here's a reference on overloads from the almighty John Skeet

Why cannot we do (void)10?

I know it is a foolish question to ask but after reading the very first answer of Mr. Matteo Italia If void() does not return a value, why do we use it?, I have performed this small experiment in C# 4.0
class Program
{
static void Main(string[] args)
{
var x = (int)5; // worked as expected
var x1 = (void)10; // Error 'void' cannot be used in this context
}
}
Why?
Void is not a data type and hence we cannot cast anything to void type. I believe you are probably coming from a C world where we could have a void* which is very different.

Java (Eclipse): Trying to access a class.main with (String args[])

I've got a problem: I can go fine between classes in my Elipse java project, if they are like this
public class Comienzo {
public static void main() {}
}
But not if they're like this
public class Principal {
public static void main(String args[]) {}
}
I'm yet noobish programmer, so I'm not sure if this is about String args[], but if I delete them Eclipse tell me something's wrong and string args are needed.
So I'm with my class Principal, click a button and
Comienzo.main();
moves from Principal to Comienzo, but if I press a cancel button on Comienzo, I'd like to go back to Principal, but that's the problem, I can't do
Principal.main(String args[]);
because in Comienzo class string cannot be resolved into a variable, and well, I'm not sure what path should I take to go back to Principal class.
Thanks for the comments, the answer is inside the comments.
If you really want to call main( String args[] ) you can do it as follows main(new String[]{});

Resources