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
Related
This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
how to determine if a String starts with a specific character?
(2 answers)
Closed last month.
I've given this task:
Ask user to enter 5 City name.
Only 1 output outside loop "Please enter city name"
Then Story each city into the array
then using for loop find the cities start with Letter "D"
Print the Cities name only in output.
NOTE: Do not append anything to output text. Test will fail if you append text. NOTE: Do not include "Please enter city name" inside first loop. (Test will fail)
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] cityname = new String [5];
System.out.println("Please enter city name");
sc.next();
for(int i = 0; i < cityname.length; i++){
cityname[i] += sc.next();
}
System.out.println(cityname[]);
}
}
I wrote this and don't know what to do next :(
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).
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:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I was wondering how I can fix this:
import java.util.*;
public class HelloWorld {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("What is your name?");
String name = input.next();
if (name == Donald)
System.out.println("Welcome back Admin");
else
System.out.println("Go Away");
}
}
I want to make it so that if the user inputs a specific name, then it will say something specific, anything else and it says go away.
I am a new student of Java and was messing around to see if this is possible
If I understand correctly strings are immutable and are frequently reused, to improve efficiency and save memory, the JVM uses a unique instance for string literals with the same character sequence. That means you can't just ask if string1 == string2 because they might be separate instances. So you want to check with string.equals(string2) to 'see if the content is the same'.
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("What is your name?");
String name = input.next();
if (name.equals("Donald"))
System.out.println("Welcome back Admin");
else
System.out.println("Go Away");
}
}