Java getString method [closed] - java

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

Related

How to print several values stored in String? [closed]

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?");
}

How do you create a program that will remove from the 2nd character to the space? [closed]

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 2 years ago.
Improve this question
I am trying to find a way to create a program where users input their full name(first last) and that can remove multiple characters from the second character to another specific character which is the space with StringBuilder. Meaning it will print out the first initial and the entire last name.
Example:
Input:
Barrack Obama
Output:
BObama
You can either use two substrings, which will create two intermediate String objects, or you can use a StringBuilder object as follows:
String input = "Hello everyone, I'm Cho.";
String output = new StringBuilder(input).delete(5, 14).toString(); // "Hello, I'm Cho."
The code below will delete from the second character until the first space detected. For example from Dong Cho to DCho
Scanner scanner = new Scanner(System.in);
String userName;
System.out.println("Enter username");
userName = scanner.nextLine();
int spaceIndex = userName.indexOf(" ")+1;
String firstPartOfString = userName.substring(0, 1);
String lastPartOfString = userName.substring(spaceIndex, userName.length());
userName = firstPartOfString +lastPartOfString;
System.out.println(userName);

How to correct this text processing programme? [closed]

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 2 years ago.
Improve this question
import java.util.Scanner;
public class TextProcessing
{
public static void main(String[] args)
{
String sentence, wordToBeTargeted, wordToBeReplaced, output;
boolean wordToCheck;
Scanner myInput = new Scanner(System.in);
sentence = myInput.nextLine();
do
{
wordToCheck = true;
System.out.println("Please enter the word for replacement:");
wordToBeTargeted = myInput.nextLine;
if(sentence.toLowerCase().indexOf(wordToBeTargeted.toLowerCase()) == -1)
{
wordToCheck = false;
System.out.println("This word cannot be found in the sentence!")
}
else
{
System.out.println("Please enter the word you would like to replace with:");
wordToBeReplaced = myInput.nextLine();
}
}while(wordToCheck = false);
}
}
}
Write a file named TextProcessing.java that will replace all occurrences of a word in a string with another word
The expected outcome is like this:
I won't write the code out - you should still get something out of the exercise, but will give some direction. The first approach that comes to mind:
Split the first input on ' ' into a list
Iterate through the list, conditionally changing values based based on two input strings
As you're iterating you can either output into a new string / stringbuilder, or directly write to console depending on the requirements

trying to split Strings in Java that are scanned from user [closed]

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 2 years ago.
Improve this question
I'm building a project where I will take three inputs from the users: name,ID,GPA. the users should enter them in one line separated by a semicolumn";" and I want to be able to receive them as one line and be able to save them in three variables.
I'm applying a method where I will take three variables from the user. for example : the user will enter the name,Id and GPA like this:
1;Sally;90.5; //in one line separated by ";"
I want to be able to save each info from the user in different variable.
Can someone tell me how will I be able to implement that?
Here is the method:
private static void addNewStudent() {
System.out.println("enter ID;Name;Gpa; ");
String info = scanner.nextLine();
Note: I'm trying the apply the CSV in my project.
You just need read one line and then split it into string array.The input order must be ID -> NAME -> GPA:
private static void addNewStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("enter ID;Name;Gpa; ");
String info = scanner.nextLine();
if (info != null) {
String[] infoArray = info.split(",");
if (infoArray.length == 3) {
String id = infoArray[0];
String name = infoArray[1];
String gpa = infoArray[2];
}
}
}
This should do to split the input by ";":
String[] input = GPA.split[";"];
Before trying to get the values, check if the input array has the expected size.

How to iterate a for loop for a user input amount of times Java [closed]

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.

Categories

Resources