Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I need help with this problem, it seems it's not as easy I thought it would be. I've tried some options from early feedbacks but it didn't help.
The thing is though that when userinput is like: i dont know the output should be displayed in screen: Sure, Sir
Or when user-input is like: I dont know (I with upercase) the output should be: Sure, Sir.
Or when user-input is like: idk (i dont know) the output should be: Sure, Sir.
But neither of those things are happening so...Help!
import java.util.Scanner;
public class Something{
static void Some(){
Scanner input = new Scanner(System.in);
String answer;
System.out.println("Hello Sir, what can I do for you?");
answer = input.nextLine();
String [] idk = {"I dont know", "i dont know", "idk"};
if(answer.equals(idk)) {
System.out.println("Sure Sir ");
} else {
System.out.println("Sir, anything?");
}
}
public static void main(String [] args) {
Some();
}
}
input: I dont know or i dont know or idk
output: Sir, anything?
/*
provide the input value and a listing of acceptable values
returns true if acceptableValues contains value
*/
public static boolean anyMatch(String value, String... acceptableValues) {
return Arrays.stream(acceptableValues).anyMatch((String t) -> value.equals(t));
}
It's better to use a collection (Set or List) to store the versions of the acceptable answers and then use method Collection::contains. Also it may be worth to store the strings written in lower case, and convert the input to lower case:
List<String> idk = Arrays.asList("i don't know", "i dont know", "idk");
if (idk.contains(answer.toLowerCase())) {
System.out.println("Sure Sir ");
} else {
System.out.println("Sir, anything?");
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Trying to make it so pasting a 60 character long url, will print only the first 56 characters. I have only ever used Java in high school, so I am very inexperienced.
import java.util.Scanner;
public class CopyOfCopyOfethanrun
{
public static void main()
{
Scanner kb = new Scanner(System.in);
link();
}
static void link(Scanner kb, String[] args)
{
String link;
System.out.print("\n\n Enter Link: ");
link = kb.nextLine();
String input = link;
String firstfiftysix = "";
if (input.length() > 56)
{firstfiftysix = input.substring(0, 56);}
else{firstfiftysix = input;
}
System.out.println(firstfiftysix);
}
}
[Here is an image showing the error I am experiencing][1]
[1]: https://i.stack.imgur.com/T6ux7.png
Your link method has 2 arguments: When invoking link, you need to provide [A] a Scanner instance, and [B] an instance of an array of Strings.
When you call link();, you provide neither of those. Presumably you want link(kb, args); there. Your main must look like:
public static void main(String[] args)
it currently doesn't. Once you fix that, voila, you have your args. But, you should just get rid of that, you don't use it anywhere in link.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I want to get a user input number to print out "Color 1 Color 2..." etc. depending on what number the input is.
I want to do this but in java, and I'm not quite sure where to find it.
How to iterate a for loop for a user input in Java?
That will do the work, using only standard java classes (java.io.Console):
import java.io.Console;
public class Consoler {
public static void main(String[] args) {
final Console console = System.console();
console.printf("How may times?\n");
final String line = console.readLine();
try {
final int quantity = Integer.parseInt(line);
for (int i = 1; i <= quantity; i++) {
System.out.printf("Color %d ",i);
}
System.out.println();
} catch (final NumberFormatException e) {
System.err.println(line + " is not a number.");
}
}
}
java.util.Scanner is probably what you're looking for. As for the for loop, you should google Java for loop and read up on how they work. Then combine the two concepts.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
So I have the questions in the top part, but I want to have all the questions at the top, and when I need to ask the questions, I can just pull them down using a variable defined as the question. Right now however, the code is asking the questions from where the questions are, not using the variable "ask" and asking from System.out.print(ask). Any ideas on how to get it to do that?
import java.util.Scanner;
public class Greetings {
public static void main(String[] args) {
Scanner newscanner = new Scanner(System.in);
String ask = getString(newscanner, "Please enter your first name: ");
// String ask2 = getString(newscanner, "Please enter your last name: ");
// String ask3 = getString(newscanner, "Please enter your year of birth:
// ");
}
public static String getString(Scanner newscanner, String ask) {
System.out.print(ask);
String first = newscanner.next();
String firstletter = first.substring(0, 1).toUpperCase();
return firstletter;
}
}
Perhaps what you are looking to do is have the question be printed, and then the answer typed on the line below it? If so, what you need to do is change the first call in getString from System.out.print to System.out.println, which should add on a newline after the question, moving the input to the next line.
EDIT: This is what it might look like now:
Please enter your first name:John
And here's what it would change to:
Please enter your first name:
John
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have an assignment where I have to attach the letters "un" to any word that the user inputs (unless the inputted word already has "un" in front of it, in which case I just return the inputted word). I'm testing my method but I encountered one problem: my program keeps returning an error if I were to test for an empty input. Here is my code:
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter: ");
String input = keyboard.nextLine();
if(input.substring(0,2).equalsIgnoreCase("un"))
{
System.out.println(input);
}
else if(input.equals(""))
{
System.out.println("un");
}
else
{
System.out.println("un" + input);
}
So I wanted to ask how I can test for an empty input/blank string since, evidently, the "" quotations do not work.
There's nothing wrong with checking input.equals("") per-se. The problem is that you have another test beforehand that throws an exception if input is shorter than 2 characters.
There are several ways to solve this, but I'd just simplify things and use startsWith. An empty string doesn't really need a special case of its own - just slap un before it, and you'll get un:
if (input.toLowerCase().startsWith("un")) {
System.out.println(input);
} else {
System.out.println("un" + input);
}
You are having this problem because you are trying to get the substring of string that doesnt have the required length. Put the empty string check first.
if(input.equals("")||input.length==1)
{
System.out.println("un");
}
else if(input.substring(0,2).equalsIgnoreCase("un"))
{
System.out.println(input);
}
else
{
System.out.println("un" + input);
}
If this weren't homework, and the library could be used for other things ( using it in this single purpose may be overkill ), you could use StringUtils.PrependIfMissing().
It does exactly this and handles nulls as well.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Also, how would I prompt the user to try again until they enter something with only 1 or 0 in it?
I realize I must use a for, or a while loop, but I'm not sure what to put as the condition.
I'm trying to have it so the user is prompted to enter something in binary, and if they don't enter something in binary, to be asked again to enter something in binary, and repeated until they do.
Thanks in advance!
You can do this by a simple regular expression matching:
if (inputString.matches("^[01]+$")) {
// accept this input
}
Simply use Integer.parseInt (str, 2);
it will throw a NumberFormatException if not binary
You can inspect every character of the String like so:
String s;//user input
boolean bad=false;//Starts false-will change to true if the input is bad
for(char c:s.toCharArray())
if(!(c=='0'||c=='1')){//if c isn't 0 or 1
bad=true;
break;//break out of loop because we've already found a problem
}
You may want to use the pattern below. The concept is to provide a "regular expression" that provides the rules for a conforming string, along with the message to prompt the user for input and the source to read the user's input from. The loop continues until a conforming string is found, or the user breaks out with "exit". The function would return the conforming string, or a null if the user wants to exit.
public String getConformingString(Scanner source, String message, String pattern) {
String result = null;
boolean isConformingString = false;
System.out.println(message);
String trialString = source.mextLine();
while (!isConformingString) {
if (trialString.matches(pattern) {
isConformingString = true;
result = isConformingString;
} else if ("exit".equalsIgnoreString(trialString)) {
isConformingString = true;
} else {
System.out.println(message);
trialString = source.nextline();
}
}
return result;
}