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
Related
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 working on a program that stores data in an Array from the user and outputs that data.
For example:
An input:
Happy HAPPY#foo.com
The output:
NAME: Happy
EMAIL: HAPPY#foo.com
I was hoping someone could look at what I've got so far and give me a pointer on how to continue. I know I have to use the scanner class and scan.nextLine, I'm not sure what comes next. I understand I don't have much, I'm not looking for someone to complete this, but maybe someone who can give me some pointers or point me in the right direction. I believe I have the correct base to my program.
My Code So Far:
import java.util.ArrayList;
import java.util.Scanner;
public class Program5 {
void loadContacts()
{
Scanner scan = new Scanner(System.in);
System.out.println(scan.nextLine());
scan.close();
}
void printContacts()
{
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Program5 program5 = new Program5();
program5.loadContacts();
program5.printContacts();
}
}
Better name the class "person" or like this, but nevermind for the explanation :
public class Program5 {
private String name;
private String mail;
public Program5(){}
void loadContacts(){
Scanner scan = new Scanner(System.in);
System.out.println("Please enter a name and a mail like this : name email#email.com (separate with ' ')");
String[] line = scan.nextLine().split(" ");
while(line.length!=2){
System.out.println("Again, enter a name and a mail like this : name email#email.com (separate with ' ')");
line = scan.nextLine().split("/");
}
this.setName(line[0]);
this.setMail(line[1]);
scan.close();
}
void printContacts() {
System.out.println("NAME : "+this.name+"\nEMAIL : "+this.mail);
}
public static void main(String[] args) {
Program5 program5 = new Program5();
program5.loadContacts();
program5.printContacts();
}
public void setName(String name) {
this.name = name;
}
public void setMail(String mail) {
this.mail = mail;
}
}
In the loadyou ask the user, check it he enter 2 element separate by '/' and if yes store them as attributes to be able to get them in another method ;)
You should have a global varible to store the name and the email. Try adding these lines on the top of the code. after public class Program5 {.
private String Name, Email;
The you must assing this values to void loadContacts(). Spliting the string you read.
void loadContacts()
{
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
String arr[] = input.split("\\s+");
Name = arr[0];
Email = arr[1];
scan.close();
}
And finally on void printContacts().
void printContacts()
{
System.out.println("NAME: " + Name + "\nEMAIL: " + Email);
}
Here is the code runnig: http://ideone.com/mjyfHK
You can do a variety of things.
You can make loadContacts() and printContacts() static methods, and also change loadContacts() so that it returns an Array or 2D array or however you choose to represent a name-email pair. Then change printContacts()` to take in that type and iterate through that Array to print out each name/email pair. This solution is a bit more work but you won't have to create an object of the same class within the main method of that class.
or
You can keep your method as they are and instead create a new field for the program class, called contacts and it would be of the type that you choose for representing name/email pairs. You would add items to contacts in loadContacts() and iterate through it in printContacts(). Then you don't have to change anything in your main method.
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've been doing a ton of research on this for the past few hours, with no luck. I am pretty sure this is a problem with .next() or .nextLine() (according to my searches). However, nothing has helped me solve my problem.
When I run the code below, I have to type in input twice, and only one of the inputs is subsequently added to the arrayList (which can be seen when you print the contents of the arrayList).
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
public class Tester{
public static void main(String[] args) {
AddStrings();
}
public static void AddStrings() {
Scanner console = new Scanner(System.in);
ArrayList<String> strings = new ArrayList<String>(); //this arraylist will hold the inputs the user types in in the while loop below
while(true) {
System.out.println("Input file name (no spaces) (type done to finish): ");
if(console.next().equals("done")) break;
//console.nextLine(); /*according to my observations, with every use of .next() or .nextLine(), I am required to type in the same input one more time
//* however, all my google/stackoverflow/ reddit searches said to include
//* a .nextLine() */
//String inputs = console.next(); //.next makes me type input twice, .nextLine only makes me do it once, but doesn't add anything to arrayList
strings.add(console.next());
}
System.out.println(strings); //for testing purposes
console.close();
}
}
Problem with your code is that you are doing console.next() two times.
1st Inside if condition and
2nd while adding to ArrayList.
Correct Code :
public class TestClass{
public static void main(String[] args) {
AddStrings();
}
public static void AddStrings() {
Scanner console = new Scanner(System.in);
ArrayList<String> strings = new ArrayList<String>(); //this arraylist will hold the inputs the user types in in the while loop below
while(true) {
System.out.println("Input file name (no spaces) (type done to finish): ");
String input = console.next();
if(input.equals("done")) break;
strings.add(input);
System.out.println(strings);
}
System.out.println(strings); //for testing purposes
console.close();
}
}
In your code, you are asking for two words to be inserted. Just remove one of them.
Use it this way:
String choice = console.next();
if (choince.equals('done')) break;
strings.add(choice);
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.