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..
}
Related
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);
}
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.
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.
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 still new to coding but I get a cannot find symbol
I don't know much about arrays but I have been looking on the internet but I cannot find why
import java.util.Scanner;
public class Alphabet {
public static void main(String[] args) {
String[] alpha = new String[26];
alpha[0] = a;
System.out.println("PLease enter a number from 0-25:");
Scanner input = new Scanner(System.in);
int userInput = input.nextInt();
}
}
alpha[0] = a;
Variable a is not declared, that's why you are getting the error.
Generally Cannot find symbol error occurs when compiler not able to resolve the symbol.
Here a can not be resolved by the compiler in your code at line
alpha[0]= a;
So you are getting error.
Declare it like this
String a = "hello";
alpha[0] = a;
or
alpha[0] = "a";
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();
}
}