Java Program find string length from user input [closed] - java

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

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
}

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

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

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.

Categories