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

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);
}

Related

overwriting an old String input

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 4 hours ago.
Improve this question
I'm trying to make my program work with a new input instead of the old one when the user clicks an option from the options menu
I don't what the problem that is not causing the program to continue with the new input instead of the old one. thanks in advance.
public static String optionSix (String input)
{
String newInput;
Scanner scnr = new Scanner (System.in);
System.out.println("Enter List of Words Seperated by Spaces: ");
newInput = scnr.nextLine();
input = newInput;
return input;
}
Java is pass-by-value, so you can't override a string's value like that. You'll have to assign the new value to it:
public static void main(String args[]) {
String someOldString = "some old value";
someOldString = optionSix(); // No need to pass the original value either
}

I would like to know how to insert my name into a message. eg:“xxx is taking the course CSCP1014" [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 8 years ago.
Improve this question
//This program prints welcome to April Semester 2014!
public class April {
public static void main (String [] args) {
System.out.println("Welcome to April Semester 2014!");
}
}
Not sure what your intention is, but maybe this is what you were aiming for:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
String name;
Scanner kb = new Scanner(System.in);
System.out.println("Please enter your name: ");
name = kb.next();
System.out.println(name + " is now taking the course CSP1014");
}
}
You need to take in an input and then process that input into your output.
One way to take in the input would be the Scanner.
Scanner in = new Scanner(System.in);
String name = in.nextLine();

Java Scanner Input Errors [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 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.

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