This question already has answers here:
concatenating string and numbers Java
(7 answers)
Closed 5 years ago.
Hello I'm a beginner in Java and i'm finding issuses to increment the variable age as it is red as a string or it is an integer
This is the Code :
public static void main(String[] args)
{
Scanner name= new Scanner(System.in);
System.out.println("Hello your name is "+name.nextLine());
Scanner in = new Scanner(System.in);
int age = in.nextInt();
System.out.println("In 17 September i will become "+age+1+" years old");
}
Whenever you add a String in a print statement, all further + signs are considered to be the concatenation operator, instead of the addition sign.
To fix, enclose it in brackets, like (age+1).
Related
This question already has answers here:
Java - class name starts with numbers [duplicate]
(2 answers)
Closed 2 years ago.
public class 12.java {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the name of your favorite city: ");
String city = keyboard.nextLine();
keyboard.close();
System.out.println("Number of characters: " + city.length());
System.out.println(city.toUpperCase());
System.out.println(city.toLowerCase());
System.out.println(city.charAt(0));
}
}
“12.java” is not a valid java class name, which must start with a letter followed by either letters or numbers (underscores are considered to be letters).
Change it to something that starts with a letter (normally a capital letter) and does not have a dot, eg:
public class Twelve
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
public static void main(String args[]){
System.out.print("Type new, wil display TRUE:");
Scanner sc = new Scanner(System.in);
String userInput = sc.nextLine();
if(userInput=="new"){
System.out.println("TRUE");
}else {
System.out.println("FALSE");
}
}
I have no idea why new not equals to new.
Please give me some hints :)
Use userInput.equals("new") instead.
This explains everything: How do I compare strings in Java?
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
I'm writing a simple code to test the value that was inputted to my constant value.
I declared this code as my constant value.
String LetMeThrough = "drunk";
String GotAnID = "drunk";
This is the whole code.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner DrunkTest = new Scanner(System.in);
String InputDrunk;
String InputDrunkAgain;
String LetMeThrough = "drunk";
String GotAnID = "drunk";
System.out.print("Type drunk: ");
InputDrunk= DrunkTest.next();
System.out.print("Re Type drunk: ");
InputDrunkAgain = DrunkTest.next();
if(InputDrunk == LetMeThrough & InputDrunkAgain == GotAnID){
System.out.print("You're not DRUNK");
}
else
System.out.print("You're F***** DRUNK");
}}
The problem is that if I type "drunk" on both.
I will get "You're F****** DRUNK" instead of the "You're not DRUNK".
When the inputted values is the same as my constant values.
You must use String::equals method to compare.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I want user to input text while it is not equal to "start".When it is equal to "start" I want to show "Bravo".In my code when I enter "start" it just continue to ask to input a text.What is missing in my code to process the operation i described.
import java.util.Scanner;
public class Application {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String komanda = "a";
do {
System.out.println("Unesi komandu ");
komanda = input.nextLine();
}
while(komanda != "start");
System.out.println("Bravo");
}
}
You have to use the equals method to compare strings in java:
while (!komanda.equals("start"));
or even better
while (!"start".equals(komanda));
this does not crash if komanda is null
See How do I compare strings in Java? for more information.
do it this way
Scanner input = new Scanner(System.in);
String komanda = "a";
do {
System.out.println("Unesi komandu ");
komanda = input.nextLine();
}
while(!"start".equals(komanda));
System.out.println("Bravo");
This question already has answers here:
Scanner issue when using nextLine after nextXXX [duplicate]
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 9 years ago.
I am writing a basic code which will be taking the number of question as input. After that I am using a for loop to Read each question. However, It is not asking for the first question and directly moving to the second question. Here is the code :
import java.util.Scanner;
class quiz
{
static Scanner in = new Scanner(System.in);
public static void main(String args[])
{
int numberofquestion,i;
String[] question;
question = new String[200];
System.out.print("Enter the number of Question : ");
numberofquestion = in.nextInt();
System.out.println("The number of question you entered is : "+numberofquestion);
for(i=0;i<=numberofquestion-1;i++)
{
System.out.print("Enter the Question : ");
question[i] = in.nextLine();
}
for(i=0;i<numberofquestion;i++)
{
System.out.print("Question = "+question[i]);
}
}
}
One solution is to add
in.nextLine();
after the in.nextInt();
Why? Because nextInt() doesn't read the "newline" character in the input, so your nextLine() in the loop was consuming it.