Java Scanner how to input string if function - java

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

Related

equalsIgnoreCase is called on itself. Error

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

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

Entering commands into a arraylist

So I have this assignment, and it says to create a arraylist using standard input, and constantly prompt the user for commands. Now, I understand how to add commands, WITHIN the code, but I dont understand how to take what the user puts in, and turn that into a command in the arraylist. Here's what I got so far:
import java.util.*;
import java.util.Scanner;
public class assignment216{
public static void main (String args[]){
ArrayList<String> names = new ArrayList<String>();
Scanner s = new Scanner(System.in);
Scanner n = new Scanner(System.in);
System.out.println("Enter a command:");
String command = s.nextLine();
System.out.println("If you entered 'add', then enter a name:");
String input = n.nextLine();
}
}
EDIT New Code:
import java.util.*;
import java.util.Scanner;
public class assignment216{
public static void main (String args[]){
ArrayList<String> names = new ArrayList<String>();
Scanner e = new Scanner(System.in);
System.out.println("Enter command, or type 'quit' to exit");
String quit = e.next();
boolean exit = quit;
if(exit = true){
System.exit();
}
}
}
You can request that the user types in his commands and then check using the method from the String class equalsIgnoreCase() and compare it and see if it is the same as add, remove, etc. (whatever commands you need). Use if-else statements to implement the logic. For example if user input resolves to add, then you add an element. If user input is not any of the common functions, then print message that there was an error and so on. I cannot type out the answer in code as this is an assignment.
import java.util.*;
import java.util.Scanner;
public class assignment216{
public static void main (String args[]){
ArrayList<String> names = new ArrayList<String>();
Scanner e = new Scanner(System.in);
System.out.println("Enter command, or type 'quit' to exit \n");
while (!e.nextLine().equalsIgnoreCase("quit")) { // As long as input not equal to quit
if (e.nextLine().equalsIgnoreCase("add") {
System.out.println("Enter name to add:\n");
String name = e.nextLine();
names.add(name);
}
if (e.nextLine().equalsIgnoreCase("remove") {
System.out.println("Enter name to remove:\n");
String name = e.nextLine();
names.remove(name);
}
}
if(e.nextLine().equalsIgnoreCase("quit"){
scanner.close();
System.exit();
}
}
}
Check out the add() method in the java.util.List interface. Might help.

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

Select a function depending upon the input in java

If I have the two functions below.
How can I select the function that will be chosen?
I imagine there is either some form of statement to determine the content of the scanner and therefore only have one function. Or it would be something that is passed to the function.
public static int questionAsk(String question)
{
Scanner scan = new Scanner (System.in);
System.out.print (question+"\n");
System.out.print ("Answer: ");
return scan.nextInt();
}
public static String questionAsk(String question)
{
Scanner scan = new Scanner (System.in);
System.out.print (question+"\n");
System.out.print ("Answer: ");
return scan.nextLine();
}
There is no way for the compiler to know which one of those methods you are calling. You could make it type safe by changing the String Types to something else, like this:
public static int questionAsk(IntQuestion question)
{
Scanner scan = new Scanner (System.in);
System.out.print (question.toString() +"\n");
System.out.print ("Answer: ");
return scan.nextInt();
}
public static String questionAsk(StringQuestion question)
{
Scanner scan = new Scanner (System.in);
System.out.print (question.toString() +"\n");
System.out.print ("Answer: ");
return scan.nextLine();
}
And adding two new classes:
public class IntQuestion extends String{
public IntQuestion(String question){
super(question);
}
}
public class StringQuestion extends String{
public StringQuestion(String question){
super(question);
}
}
When you construct an IntQuestion of StringQuestion, you can simply construct them the same way you would construct a String if you called the constructor:
IntQuestion intQuestion = new IntQuestion("Some String Here");
This is just a little bit of syntactic sugar to get the compiler to play nice and select the correct method based on the type.
I hope this helps.
You should first scan, parse and then call the required function using if else or switch case.
What you are doing right now is using the same code in two functions, and not reusing the code. Just use a single scan instead

Categories

Resources