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.
Related
My exercise is to create a String variable called "Hallo". I should check, if the String is called "Hallo" with a scanner. If its true, my code should answer with "Hallo". If not, the code should answer with "Tschüss".
Here is the code:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
String hallo = "Hallo.";
Scanner input = new Scanner(System.in);
{
System.out.println(input);
input.nextLine();
if (hallo.equalsIgnoreCase(hallo)) {
System.out.println(hallo);
} else {
System.out.println("Tschüss.");
}
}
}
}
input.nextLine(); returns a String. You need to store the result into a variable like String userResponse = input.nextLine(); then in your condition do if(userResponse.equalsIgnoreCase(hello)). Because in your code you test if your variable hello is equals to your variable hello. It can only be true. You need to store the response of the user and then test if it's equal to your variable hello
I am trying to make this program That uses the scanner method. A user would type their name, then the ScannerToolclass would store that information into the guess string varible. An Object is created in the portation class as ScannerTool cool = new ScannerTool(); for both the justPoints() and post() methods. The portation class takes the objects and store what the user types into a String variable called hope as String hope = cool.scannerT() it then takes what the user types and executes an if statement. I create an object for the portation class and ScannerTool inside the MainTest and then I run it from MainTest class.
My problem is that when I run this program it throws an exception error:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at ScannerTool.scannerT(ScannerTool.java:7)
at portation.justPoints(portation.java:12)
at MainTest.main(MainTest.java:20)
However, when i dont use the portation class and just go from ScannerTool to MainTest with the same code from portation it works. The weird part is that when I do it the original way there are no errors presented at the lines, only when I execute the whole program
What I tried: I tried to change the return types in the methods in portation from int and string to void, that didn't work. I tried looking at the lines that said where the error occured, but that didn't help becuase everything looked correct. Since it's not throwing actual errors on the IDE before running i'm at a loss.
the code:
The code that works:
MainTest
public class MainTest {
public static void main(String [] args) {
ScannerTool scan = new ScannerTool();
//portation damn = new portation ();
System.out.print("What your name my guy? ");
String hope = scan.scannerT();
int points = 0; // taken from the portation class
if (hope.equals("chris")){
points = points + 1;
}else {
points = 0;
}
System.out.println("the name " + hope + " is cool");
if(hope.equals("chris")) {
System.out.println("for your name being chris I award you one point!");
}else {
System.out.println("but, you get no points for that name");
} //taken from the portation class
System.out.println(points);
//damn.post();
//damn.justPoints();
}
}
portation: not in use
ScannerTool:
import java.util.*;
public class ScannerTool {
public String scannerT() {
Scanner message = new Scanner(System.in); //User input
String guess = message.nextLine(); // storing what the user inputted in a string variable
message.close(); //closed the scanner object
return guess; // returned user input
}
}
the code that doesn't work
MaintTest:
public class MainTest {
public static void main(String [] args) {
ScannerTool scan = new ScannerTool();
portation damn = new portation ();
System.out.print("What your name my guy? ");
String hope = scan.scannerT();
damn.post();
damn.justPoints();
}
}
portation:
public class portation {
public void justPoints() {
ScannerTool cool = new ScannerTool(); // created an object from the ScannerTool
String hope = cool.scannerT(); //stored the ScannerTool answer the user inputed from the guess string
int points = 0; //setup the points variable
if (hope.equals("chris")){
points = points + 1;
}else {
points = 0;
}
System.out.println(points);
}
public void post() {
ScannerTool cool = new ScannerTool(); // created an object from the ScannerTool
String hope = cool.scannerT(); //stored the ScannerTool answer the user inputed from the guess string
System.out.println("the name " + hope + " is cool"); //printed what the user typed
if(hope.equals("chris")) {
System.out.println("for your name being chris I award you one point!");
}else {
System.out.println("but, you get no points for that name");
}
}
}
ScannerTool:
import java.util.*;
public class ScannerTool {
public String scannerT() {
Scanner message = new Scanner(System.in); //User input
String guess = message.nextLine(); // storing what the user inputted in a string variable
message.close(); //closed the scanner object
return guess; // returned user input
}
}
when I use the way that doesn't work this is what runs:
What your name my guy? chris
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at ScannerTool.scannerT(ScannerTool.java:7)
at portation.post(portation.java:21)
at MainTest.main(MainTest.java:9)
What would be the answer I'm looking for? I would like to know, why am I getting this error and what do I need to do to fix it?
The problem comes from the fact that System.in is a specific InputStream - it is static and only created once. In your case, you read from it once and immediately close it (when you close the Scanner). When you try to read from a closed stream, it will throw an exception, NoSuchElementException in this case. When working with files it usually isn't a problem because you could always create a new InputStream.
What you need to do here is to make sure you will close the InputStream only when you're done with reading everything. You could make the Scanner variable static, create a new method for closing it and call it only when you're done with reading.
public class House extends Device {
static final int ADD = 'a';
static final int SHOW = 's';
static final int ONOFF = 'o';
static final int QUIT = 'q';
public void main() {
Scanner in = new Scanner(System.in);
Device theDevice = new Device();
while (true) {
System.out.print("(a)dd, (s)how, (o)n/off, (q)uit: ");
char input = in.next().toLowerCase().charAt(0);
if (input == ADD) {
System.out.print("Device name: ");
String deviceName = in.nextLine();
theDevice.addDevice(deviceName);
}
}
}
}
class Device {
List<String> deviceName = new ArrayList<String>();
List<Boolean> deviceStatus = new ArrayList<Boolean>();
List<Long> deviceOnTime = new ArrayList<Long>();
void addDevice(String deviceName) {
this.deviceName.add(deviceName);
this.deviceStatus.add(false);
this.deviceOnTime.add(0L);
}
}
I put my code like this and when executed it was shown like this
(a)dd, (s)how, (o)n/off, (q)uit: a
Device name: (a)dd, (s)how, (o)n/off, (q)uit:
it's not waiting for me to input something like it suppose to be. How can I fix it.
Thx a lot.
When you call in.next() it takes the next value ignoring the return character '\n'. Then you call in.nextLine() and it takes the already existing line and skips. To solve this use in.nextLine() instead of in.next() and trim the result, or in a dirty way call in.nextLine() two consecutive times.
Your main method does not have the correct format: in Java, a main method must be declared like this:
public static void main(String[] args) {
}
If you change that, it will work.
Another thing is -indeed- that you should use
in.nextLine()
to consume the line break.
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))
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);
...
}