how to allow spaces in input java [duplicate] - java

This question already has answers here:
Scanner doesn't see after space
(7 answers)
Closed 5 years ago.
I'm new to Java so this is a very basic question. I get and error when entering a name with spaces. What do I need to change please? steering me to info is great too.
how do I allow input with spaces.
import java.util.Scanner;
public class yes {
public static void main(String[] args) {
Name = in.next();
....
}
}

Please use in.nextLine();
import java.util.Scanner;
public class Yes {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String name = in.nextLine();
....
}
}

Here's what you need to change :
import java.util.Scanner;
public class Yes {
public static void main(String[] args) {
String name;
Scanner in = new Scanner(System.in);
name = in.nextLine();
....
}
}
Here's why : The next() method reads the input on current line till a space is detected. It advances to next line only when you press Enter/Return.
On the other hand, nextLine() method advances to the next line and returns the input on the current line.
You can read more about Scanner class here.
Also, it is good to follow Java naming conventions. Thanks!

Use the Scanner.nextLine(); method.
import java.util.Scanner;
public class yes {
public static void main(String[] args) {
String name = in.nextLine();
....
}
}
Hope this helps!

Related

Law of Demeter-Inputting parameters

i was running the PMD tool for my code and checking it for a simple program that inputs two strings for beginning purposes.when i do something like a=in.nextLine(); it shows object not created locally.Can you help?
import java.util.Scanner;
class Test {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
String a, b;
a = in.nextLine();
b = in.nextLine();
in.close();
}
}

Java Scanner how to input string if function

I am very new in programming into Java.
My question is that I have a code (see below) and I want to compare them with if statement. An errors occur at line 9 (string test) and 11(if(test.equals). I completely do not have idea.
I have made a code with int and it works perfect, but that.
package bucky;
import java.util.Scanner;
class apples {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
Scanner sc = new Scanner(System.in);
String test = sc.nextLine();
if (test.equals("YES")) {
System.out.println("YES");
} else {
System.out.println("TIS IS ELSE");
}
}
}
You are almost there... define YES as string and that it
String test = sc.nextLine();
String YES = "yes";
if (test.equals(YES)) {
or even better use equalsIgnoreCase() so you can get rid off the case sensitive input
if (test.equalsIgnorecase(YES))

Java Scanner get char

import java.util.Scanner;
public class myClass {
public static void main(String[] args)
{
String[] exampleString;
char expression;
Scanner getExp = new Scanner(System.in);
while(expression = getExp.next().charAt(0))
{
// myString will added by expression character
}
}
}
I tried it on Eclipse Mars but
expression = getExp.next().charAt(0) part gets error always.
I didn't understand that what is the error .
Previously i think on this link
stackoverflow Scanner question
am i think wrong ?
You probably want
while (getExp.hasNext()) {
expression = getExp.next().charAt(0);
...
}

how to create multiple scanner elements in java [duplicate]

This question already has an answer here:
How to use multiple Scanner objects on System.in?
(1 answer)
Closed 26 days ago.
I have a main function in which I use scanner to read an integer from console.
Inside this main function, we can access another function which also uses scanner to read an integer. So, the program swings between these two functions many times. But, Java.util.scanner throws an exception. Is there any way to overcome this?
import java.util.Scanner;
public class dummy {
public static void main(String[] args) {
int buy;
Scanner sc = new Scanner(System.in);
buy = sc.nextInt();
user = dummy2();
sc.close();
}
static boolean dummy2(){
Scanner sc1 = new Scanner(System.in);
sc1.close();
}
}
First of all, it would make the question much easier to answer if you gave more information, such as the exception and its message, and maybe source code.
If the exception is a NoSuchElementException, the direct problem is that the function is closing the Scanner. When the scanner is closed, it also closes the underlying ImputStream. This makes all other Scanner on that input invalid.
If the exception is InputMismatchException, then the input is not an int.
If the exception is IllegalStateException, then the scanner has been closed, this could happen is the function and the main method are using the scanner, and one closes it.
However, you should not be taking user input in functions. This limits future use, say if you wanted to later add a GUI or make the same calculation based off a number not gotten from the user, then you would need rewrite the function. The function should take a int as a parameter, which the main method should get from the user. Only the main method and other methods directly relating to user input, such as the Scanner's methods, should read user input.
Use the same Scanner object.
import java.util.Scanner;
public class dummy {
private static final Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int buy;
buy = sc.nextInt();
user = dummy2();
//Do more stuff with the same scanner
//close it when done
}
static boolean dummy2(){
//Scan stuff
int nbr = sc.nextInt();
}
I would suggest something like that:
import java.util.Scanner;
public class dummy {
Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int buy;
buy = sc.nextInt();
user = dummy2();
sc.close();
}
static boolean dummy2(){
//lets scan a string.
sc.nextLine();
}
}
Reusable objects! Isn't that nice?

How to convert an array to regular method

I am trying to write this code for a class, but I don't want to use an array (String word[]). How do I change it so I use a regular method with parentheses?
Also, one of my friends helped and I am trying to learn, and I forgot what the alright(s); thing does. I tied to figure it out, but have failed. I think it creates and object for the scan, but I don't really know.
import java.util.Scanner;
public class WordLines{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
alright(s);
}
public static void alright(String s){
String word[]=s.split(" ");
for(int j=0;j<word.length; j++){
System.out.println(word[j]);
}
}
}
Thank you so much for the help!!! :)
One way to achieve similar results without the array is to use an additional instance of the Scanner class to parse the string:
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
Scanner parse = new Scanner(s);
while (parse.hasNext()) {
System.out.println(parse.next());
}
Link: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

Categories

Resources