Java Scanner Input Errors [closed] - java

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 making a simple Java game called 21 sticks. In the beginning, I want to ask the user his/her name, so I can refer to them by their name for the rest of the game. However, when I use my code(below), first input has the eclipse red squiggly line under it, meaning error. This is not compiling. I have tried to Google the way to fix this, or if there is a better way to do it, but I have fond nothing. I have tried changing input to inputMap, but then the nextLine doesn't work. I would like to know why this is showing up as an error, and if there is a better way to ask for the user's name.
my code:
import java.util.Scanner;
public class TwentyOneSticks {
public static void main(String[] args) {
System.out.println("What is your name?")
String userName = input.nextLine()
int numofSticks = 21;
Scanner input = new Scanner(System.in);
Scanner take = new Scanner(System.in);
System.out.println("Would you like to go first? (Y/N)");
String goFirst = input.nextLine();

try
import java.util.Scanner;
public class TwentyOneSticks {
public static void main(String[] args) {
System.out.println("What is your name?");
Scanner input = new Scanner(System.in); <--- moved it here
String userName = input.nextLine();
int numofSticks = 21;
//Scanner take = new Scanner(System.in); <-- what is use of this?
System.out.println("Would you like to go first? (Y/N)");
String goFirst = input.nextLine();
You have to declare (and initialise) a variable before using it. Also some semicolons were missing.

Related

Java Homework Assignment — java.util.NoSuchElementException: No line found

I get an error from automated JUnit 4 unit testing software that my course instructor uses to automatically check assignments.
We upload our draft code to a cloud program tester which automatically checks and grades the assignments.
This is week 1 of first semester. I've never coded in Java before. And the lessons haven't taught if/else, while, loops, switch etc. so I don't think it's okay to use those (they appear next module.)
I followed a class video tutorial, which doesn't throw any exceptions in the tutorial — so I don't know what's going on.
The unit was on variables, input, printing out, Booleans, operators, and a few things like contains, replace, equals etc.
Note: I am allowed to ask questions on StackOverflow per instructor.
The error is: java.util.NoSuchElementException: No line found
No line number is specified in the testing software where the exception occurs. And since it's not happening in my IDE, I don't know where it is.
Other solutions I've found require while and other methods I'm not supposed to use.
My code (below) runs okay in the VSCODE IDE cloud the school provided.
import java.util.Scanner;
public class ContainsAnyCase {
public static void main(String[] args) {
System.out.println("Type a word:\n");
Scanner userInputWord = new Scanner(System.in);
String word = userInputWord.nextLine();
String wordlc = word.toLowerCase();
System.out.println("Type a sentence:\n");
Scanner userInputSentence = new Scanner(System.in);
String sentence = userInputSentence.nextLine();
String sentencelc = sentence.toLowerCase();
boolean isContains = sentencelc.contains(wordlc);
System.out.println(isContains);
}
}
I'm wondering if anyone can explain why there is an error and if there are ways to fix it or avoid the error without going into the territory of future modules? Also, I'm still learning how to use Stack Overflow appropriately. If I asked my question wrong, please advise how I can improve my question or post. Thank you.
I expected for the program to tell me true/false if the word was in the sentence. In my IDE, it worked. When I uploaded it to the JUnit4, it said java.util.NoSuchElementException: No line found
Here's an example of using just ONE SCANNER as suggested in the comments.
I've also eliminated the second set of variables for lower case; just convert the input directly to lower case and store it in the original variable:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Type a word:\n");
String word = sc.nextLine().toLowerCase();
System.out.println("Type a sentence:\n");
String sentence = sc.nextLine().toLowerCase();
boolean isContains = sentence.contains(word);
System.out.println(isContains);
}
If you need to preserve the exact user input for later, then you can convert to lower case only when you call contains() like this:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Type a word:\n");
String word = sc.nextLine();
System.out.println("Type a sentence:\n");
String sentence = sc.nextLine();
boolean isContains = sentence.toLowerCase().contains(word.toLowerCase());
System.out.println(isContains);
}

how to take input in Java from user? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am new to Java but quite good at C. I want to take the input from user in Java. Could anyone please suggest the basic concept with the exact code? Thanks
import java.util.Scanner;
public class Main {
public static void main (String [] args)
{
Scanner sc = new Scanner(System.in);
String inputText = sc.nextLine();
}
}
import java.util.Scanner;
This imports the java library that will allow you to read data from the specified input source.
Scanner sc = new Scanner(System.in);
Gets a working instance of the Scanner library in sc which will go get the input for you from the standard input of the system, System.in, in our case, which is the keyboard.
String inputText = sc.nextLine();
This code asks the sc instance to get the line of text typed by the user and set it into the inputText variable.
Note that when sc.nextLine() is called, it halts any execution and waits for user input and proceeds only when the user presses the Enter on the keyboard.
Here is a very simple idea using a Scanner
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter something searched.. ");
String input = scan.next();
System.out.println("your input is " + input);
}

simple java user input, where did i go wrong?? [closed]

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
Hi guys im working on a basic java program to get somebodys name, it comes up with a lot of errors could you correct me where i went wrong. Sorry and thanks. I also need to basicly set the user input to a string variable which i can then later store into a text file.
import java.io.Console;
import java.util.Scanner;
public class HelloJello {
public static void main(String[] args) {
System.out.print("Hi using this program i will find out"
+ "what your name is and store it in document");
Scanner scanner = new Scanner (System.in);
System.out.print("Enter your name");
name = scanner.next(); // Get what the user types.
}
}
what is name ?
You need to declare the variable reference properly
You need to declare data type of name. It should be a String.
public static void main(String[] args) {
String name=""; // always good to initialize
System.out.print("Hi using this program i will find out"
+ "what your name is and store it in document");
Scanner scanner = new Scanner (System.in);
System.out.println("Enter your name");
name = scanner.nextLine(); // Get what the user types. Reads entire line..
}

Make Scanner read a String in java [closed]

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 converting command line java application to swing GUI. I have a Parser class that reads input from Scanner
Scanner in = new Scanner(System.in);
command = in.nextInt();
Is it possible to pass String instead of (System.in) ? Or, is there a better way to deal?
You can use nextLine() of Scanner class to read string value entered in console.
Below is an example:
public class Parser {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Please enter command:");
String command = in.nextLine();
JOptionPane.showMessageDialog(null, command);
}
}
Eneter string value in console:
Please enter command:
Java Command
If you want pass Strings in Scanner just use Java's next() method
Scanner in = new Scanner(System.in);
String command = in.next();
I hope this is what you were looking for.

Java Program find string length from user input [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
public class Strings {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String word1 = sc.next();
System.out.println(word1.length());
sc.close();
}
}
So i want to make a very simple program that takes in a user string and returns the length, but the program does not even run. I am using eclipse. Any tips?
Your program works fine.
Are you forgetting to type something into the console? It might appear to not be running but it actually is.
ALSO: If you are new to java/programming I would suggest using Netbeans over Eclipse. Netbeans offers a bit more support, although it is less flexible (something you shouldn't need to worry about right now).
Make sure that your are providing an input to calculate the length of word.
This is working fine if you providing an input, it is better to change your code as follows:
public class Strings {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your word: \n");
String word1 = sc.next();
System.out.println(word1.length());
sc.close();
}
}

Categories