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.
Related
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);
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
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 1 year ago.
Improve this question
I know i have to use scanner and if condition but i don't know how to connect them
Example:
Are you willing to convert weight or height?
(user input= weight)
Will it be for imperial unit or metric unit?
(user input=metric)
Please provide the weight information in kg
(user input=75)
Your weight in kgs 75.00 is equal to 165.38 in pounds
Scanner scanner = new Scanner(System.in);
System.out.println("Enter an input");
String input= scanner.next();
You can start by putting the user input into a variable. Depending on the flow of your program, you might need to parse the input into either integer or float.
int integerinput = Integer.parseInt(input);
float floatInput = Float.parseFloat(input);
From there, you can start doing the comparison. You have to be careful when parsing information, if the user entered a word and you try to convert that into either integer or float, you will get an exception.
You should get user’s input just once:
String answer = ask.nextLine();
And then inspect the input with if():
if (answer.equals("heght")){
// heght conversion goes here
}
else {
// weght conversion goes here
}
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 4 years ago.
Improve this question
I want to use a Class to hold user inputs. For example "example_name";"example_value";"example_city" is the form of the input and I want to make objects from an input stream of theese three inputs line by line from a file. So whenever I enter Adam;27; London it should be saved into an Object(Adam,27,London) and ask for the next input.
Thank you for your help, I know this might be a stupid question but I'm new to OO programming and I have a C background and don't want to use two dimensional arrays.
Let's break this problem into 3 section. First, we will have to design the class. Second, we will take the user input and create an object of that class until the user press n(no). Third, we will display the user information. Here I have used a LinkedList data structure for storing the inputs.
Section 1:
class example{
String example_name;
int example_value;
String example_city;
example(String name,int value,String city)
{
this.example_name = name;
this.example_value = value;
this.example_city = city;
}
}
section 2:
LinkedList<example> ob = new LinkedList<>();
Scanner input = new Scanner(System.in);
System.out.println("Press y to Continue n to Exit");
char i = input.next().charAt(0);
while(i!='n')
{
input.nextLine();
System.out.println("Enter name");
String name = input.nextLine();
System.out.println("Enter value");
int value = input.nextInt();
input.nextLine();
System.out.println("Enter city");
String city = input.nextLine();
ob.addLast(new example(name,value,city));
System.out.println("Press y to Continue n to Exit");
i = input.next().charAt(0);
}
Section 3:
for(example x:ob)
System.out.println(x.example_name + " " + x.example_value + " "+x.example_city);
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