Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am new to Java and I am trying to make a Java app where it asks you to spell "Java" and if you spelled it correctly it will type "yes", however, it is typing "no", what am I doing wrong:
package quiz;
import java.util.Scanner;
public class quiz {
public static void main(String[] args) {
Scanner kirill = new Scanner(System.in);
System.out.println(kirill.next());
String kirill2 = "Java";
if (kirill.equals(kirill2)){
System.out.println("yes");
}else{
System.out.println("no");
}
System.out.println(kirill);
kirill.close();
}
}
Running code:
Java
Java
no
java.util.Scanner[delimiters=\p{javaWhitespace}+][position=4][match valid=true][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q?\E][infinity string=\Q?\E]
if (kirill.equals(kirill2)){
kirill is the Scanner object, not the string. Try something like this:
Scanner kirill = new Scanner(System.in);
String userInput = kirill.next();
if (userInput.equals("Java")){
...
Also, note that your code will print "yes" if the user types "Java is a programming langauge." If you only want it to validate with just "Java," replace next with nextLine.
package quiz;
import java.util.Scanner;
public class quiz {
public static void main(String[] args) {
String kirill;
String kirill2 = "Java";
Scanner input = new Scanner(System.in);
kirill = input.next();
if (kirill.equals(kirill2)){
System.out.println("yes");
}else{
System.out.println("no");
}
System.out.println(kirill);
input.close();
}
}
Minor issue with your Scanner. You were trying to match a Scanner to a String. You can't to that silly!
Save what you're reading into a String instead of comparing the Scanner object with a String. Your main method should look something like
public static void main(String[] args) {
Scanner kirill = new Scanner(System.in);
String input = kirill.nextLine();
System.out.println(input);
String kirill2 = "Java";
if (input.equals(kirill2)){
System.out.println("yes");
}else{
System.out.println("no");
}
System.out.println(kirill);
kirill.close();
}
Also, note that .next() will only scan to the first delimiter (which is by default any whitespace), so if you want to make sure that the user only types "Java", then you should probably use .nextLine() instead of .next().
Let's take a quick look on your code, inside main():
Scanner kirill = new Scanner(System.in);
creates a scanner and assigns it to a variable, OK.
System.out.println(kirill.next());
Prints what the user types, but doesn't assign it to anything.
String kirill2 = "Java";
Just a String variable... OK.
if (input.equals(kirill2)){
If the scanner equals some text, then proceed. Hold on, you see what I just said? Comparing a Scanner and a String. This wouldn't end up right. Imagine a robot, and you give it a cup of water and a paper with "water" written on it, and ask if they are equal. Obviously they're not, and they can't be. You are comparing a set value with another set value, instead of the user's input. The following would be correct:
package quiz;
import java.util.Scanner;
public class quiz {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in); //creates a scanner
String text = "Java"; //creates the text to be compared
String input = scan.nextLine(); //read some arbitrary text the user types
if (input.equals(text)){ //checks if user's input is equal to text
System.out.println("yes");
}else{
System.out.println("no");
}
scan.close(); //closes the Scanner
}
}
Although not required, it's a good practice to name variables after what they do or represent, or you will get confused very quickly...
So here, an easier method would be:
package quiz;
import java.util.Scanner;
public static void main(String[] args) {
String userInput;
String word = "Java":
Scanner in1 = new Scanner(System.in);
userInput = in1.next();
System.out.println( userInput );
if (word.equals(userInput)) {
System.out.println("Yes!");
}else{
System.out.println("No.");
}
System.out.println( userInput );
userInput.close();
}
Related
We have to make a program on printing initials, which seems pretty easy ok, but I don't know how to cut the string when the input is all on one line using the scanner class in.nextline();. I cant seem to find a way to cut the string using only string methods. Also, another problem arose when I have to also be able to adjust if there isn't a middle name either. if anyone can help me or lead me in the right direction that would be nice.
If you can use split function as you can see below:
String inputString=s.nextLine();
String [] str = inputString.split(" ");
If you want to have extract only first letter then -
import java.util.Scanner;
public class TestStringInput {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Please Enter the Name");
String input = scan.nextLine();
int value = input.indexOf(" ",input.indexOf(" "));
String result = input.substring(0,value);
System.out.println(result);
}
}
But if you want to Extract Starting 2 Initials then change this line-
int value = input.indexOf(" ",input.indexOf(" ")+1);
As Stephen mentioned, your question is about Java, consider re-tagging.
You can use a for loop to iterate through the string. Remember a string is an object. It would help a lot if you posted your code.
import java.util.Scanner;
public class HW03 {
public static void main (String args[])
{
Scanner in = new Scanner(System.in);
String str = "";
System.out.println("What are your first, middle and last names?");
str = in.nextLine();
}
}
Hey there I got into some trouble with my java Code.
I try to code a bit around with java for a few hours and I dont know much thats why im asking. I learn best by trying but I get into so many problems.
So: I want the scanner to scan the next Statement and if its "ja" it should do the if thing etc.
The problem is, when i try to compile it it has an error with the = s.nextInt thing. In the console it says: "cannot find symbole". I tried so many things I dont know what to do. Allready tried so much.
import java.util.Scanner;
public class Brotcrunsher {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
System.out.println ("Hallo");
System.out.println ("A flag has more then 1 color right?");
String a = s.NextInt();
if (a.equals("ja")) {
System.out.println ("You arent dumb, nice.");
} // end of if
else {
System.out.println ("You arentn a genie");
} // end of if-else
}
}
thanks in advance.
EDIT: Problem solved. Thank you for every awnser. I try my best to Tag my posts better and to format my code better
Here:
String a = s.NextInt();
You want a to be String (which makes sense, as you want to compare it against other Strings later on); so you better use:
String a = s.nextLine();
instead!
The other method a) does not exist and b) nextInt() ... returns a number, not a string
I can see two errors, firstly you are taking a string input from the command line user so your scanner must be "scanner.nextLine()" which takes a string, as it stands you are expecting an integer value.
Second your "s.scanner" is not calling anything, you have declared your scanner with the name "scan", so you need to change that to "scan".
import java.util.Scanner;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("A flag has more than one colour?");
String input = scan.nextLine();
if (input.equals("yes")) {
System.out.println("well done");
} else {
System.out.println("wrong answer");
}
}
Try:
import java.util.Scanner;
public class Brotcrunsher {
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
System.out.println ("Hallo");
System.out.println ("A flag has more then 1 color right?");
String a = scan.nextLine();
if (a.equals("ja")) {
System.out.println ("You arent dumb, nice.");
} // end of if
else {
System.out.println ("You arentn a genie");
} // end of if-else
}
}
You have got a compilation error that should be
String a = scan.next();
Since scan is your scanner object where you are using String a = s.NextInt(); which is not at all an object of scanner.
Two issues, one is a is a String not an int and the second is Scanner.nextLine() (or nextInt() or next()). And, the local reference is scan (not s). Like,
String a = scan.nextLine();
You can use like this.
import java.util.Scanner;
class ScannerTest{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter your rollno");
int rollno=sc.nextInt();
System.out.println("Enter your name");
String name=sc.next();
System.out.println("Enter your fee");
double fee=sc.nextDouble();
System.out.println("Rollno:"+rollno+" name:"+name+" fee:"+fee);
sc.close();
}
}
refrence: http://www.javatpoint.com/Scanner-class
I want to take input form user, i am sure my code is right but it don't work at all. Please help is there any thing that i am doing wrong?
`public void edit() throws IOException {
sll.insertAfter();
System.out.println("Enter text: ");
String sen;
sen = keyboard.next();
Object obj = sen;
sll.put(obj);
}
when i execute this an error appears at this line
sen = keyboard.next();
import java.util.*;
public class Example
{
public static void main(String[] args)
{
Edit();
}
public static void Edit()
{
Scanner scan = new Scanner(System.in);
String random;
System.out.print("Please input some text: ");
random = scan.nextLine();
System.out.println("You entered: " + random);
}
}
I don't know what your main method looks like so I can only assume it's empty That being said I can tell you why your current code doesn't work based off of the information you've given us.
Your edit method is not static, and in this situation assuming you've laid out your program simillar to this it must be static as it is in my example.
You've not setup a scanner, or maybe you did outside of your edit method but failed to make it static?
Scanner scan = new Scanner(System.in);
Why are you using Object, if you want to edit the string just use a for loop and substring.
Object
If you provide us with more information, your full code and the error you're getting we can better help you!
I use this code to input something in the String variable n, after I type what is asked, I don't want the program to print what I typed.
The code:
String n= Scanner.nextLine();
I feel is something you type after Scanner. , but I don't know what is it.
EDITED:
Thanks for answer, but I don't know how to Close the Scanner.
Here is the program...:
My program:
import java.util.Scanner;
public class A{
public static void main(String args[]){
String n;
Scanner bananas=new Scanner(System.in);
System.out.println("First Digit: ");
n=bananas.nextLine();
}
}
When I run the program, on the screen appears: "First Digit:",
then type, let's say I type "apple", the String n will be equals to
"apple", it's ok, that is what I want, what I don't like is that the
program prints on the screen the String n, I didn't want the program
to do that, this is what happens:
First Digit:
apples
What I want I the program to print is just:
"First Digit: ", without showing what I typed, just keep it.
Thanks.
First, you have to instantiate the Scanner, like so:
Scanner scanner = new Scanner(System.in); //if you want to read from the console
Then, you can receive your input.
String n = scanner.nextLine();
Make sure you close your scanner when you don't need it anymore:
scanner.close();
There is nothing tricky about it. You can do it like below and still your variable will hold your desired value.
import java.util.Scanner;
public class A{
public static void main(String args[]){
String n;
Scanner bananas=new Scanner(System.in);
System.out.println("First Digit: ");
n=bananas.nextLine(); // once you are done with input
bananas.close();
}
}
Even if you don't close scanner it won't make any difference as far as your code execution is concerned. Only a warning some where in your class will arise which doesn;t really affect your code. But still it is better to close the scanner once you are done with it. Its a good practice.
This question already has answers here:
Getting Keyboard Input
(10 answers)
Closed 8 years ago.
I'm don't know how to read from the console in Java.
If it's possible I want to do it using a scanner.
This is what i tried while learning Java.
package Scanners;
import java.util.Scanner;
public class ConsoleScanner {
static Scanner input = new Scanner(System.in);
public static void main(String[] args){
if(input.equals("Hello"))
System.out.println("You typed in: Hello ");
if(input.equals("Good Bye"))
System.out.println("You typed in: Good Bye");
else{
System.out.println("You typed in: " + input);
}
}
}
It give's me this error:
You typed in:
java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match
valid=false][need input=false][source
closed=false][skipped=false][group separator=.][decimal
separator=\,][positive prefix=][negative prefix=\Q-\E][positive
suffix=][negative suffix=][NaN string=\Q?\E][infinity string=\Q?\E]
If there is a better way to read from the console then please post it.
- Thanks
You don't want to print the Scanner itself. You want to call the various Scanner functions to get the input. Have a look at the Scanner API and the Scanner tutorial (which are the first and second results for googling "Java Scanner") for more info.
Try this:
package Scanners;
import java.util.Scanner;
public class ConsoleScanner {
static Scanner scanner = new Scanner(System.in); //Creates the scanner
public static void main(String[] args){
String input = scanner.NextLine(); //Sets the string input equal to whatever the user types next
if(input.equals("Hello"))
System.out.println("You typed in: Hello ");
if(input.equals("Good Bye"))
System.out.println("You typed in: Good Bye");
else{
System.out.println("You typed in: " + input);
}
}
}
My friend you have to use
Scanner scanner= new Scanner(System.in);
input = scanner.next();
This method finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern.
import java.util.Scanner;
class ScannerDemo{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter your age");
int age=sc.nextInt();
System.out.println("age:"+age);
}
}