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);
...
}
Related
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!
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();
}
}
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))
I am trying to get an input from console, assign it to a string variable. Then I'd like to concatinate it with another variable. Provided if the user enters the right character each time, soon it'll make up a word. Once this word matches the desired one the loop stops.
Need your help though.
public class expl {
public static void main(String[] args) {
String consatinate = "a";
String needed = apple;
while (!consatinate.equals(needed)) {
System.out.println("Enter a letter");
String input = System.console().readLine();
consatinate = consatinate.concat(input);
System.out.println(consatinate);
}
}
}
Error message:
Exception in thread "main" java.lang.Error: Unresolved compilation
problem: apple cannot be resolved to a variable at
expl.main(expl.java:6)
Apple is a literal string, so it should have quotations around it:
public class expl {
public static void main(String[] args) {
Scanner inputScanner = new Scanner(System.in);
String consatinate = "a";
String needed = "apple";
while (!consatinate.equals(needed)) {
System.out.println("Enter a letter");
String input = inputScanner.nextLine();
consatinate = consatinate.concat(input);
System.out.println(consatinate);
}
inputScanner.close();
}
}
I would also asume that "consatinate" should be named concatenate, but that's just a guess.
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