Scanner a = new Scanner(System.in);
String i = "11,111";
System.out.print("Enter first number a:");
String b = a.next();
I want to display number without comma when user provide
You can simply use String::replace
System.out.println (i.replace (",", ""));
Replace All will do the trick..
String i = "11,000";
System.out.println (i.replaceAll(",", ""));
Related
I am working through a piece of self study, Essentially I am to ask the User for a string input such as "John, Doe" IF the string doesnt have a comma, I am to display an error, and prompt the user until the string does indeed have a comma (Fixed.). Once this is achieved I need to parse the string from the comma, and any combination of comma that can occur (i.e. John, doe or John , doe or John ,doe) then using the Scanner class I need to grab John doe, and split them up to be separately printed later.
So far I know how to use the scanner class to grab certain amounts of string up to a whitespace, however what I want is to grab the "," but I haven't found a way to do this yet, I figured using the .next(pattern) of the scanner class would be what I need, as the way it was written should do exactly that. however im getting an exception InputMismatchException doing so.
Here is the code im working with:
while (!userInput.contains(",")) {
System.out.print("Enter a string seperated by a comma: ");
userInput = scnr.nextLine();
if (!userInput.contains(",")) {
System.out.println("Error, no comma present");
}
else {
String string1;
String string2;
Scanner inSS = new Scanner(userInput);
String commaHold;
commaHold = inSS. //FIXME this is where the problem is
string1 = inSS.next();
string2 = inSS.next();
System.out.println(string1 + " " + string2);
}
}
This can be achieved simply by splitting and checking that the result is an array of two Strings
String input = scnr.nextLine();
String [] names = input.split (",");
while (names.length != 2) {
System.out.println ("Enter with one comma");
input = scnr.nextLine();
names = input.split (",");
}
// now you can use names[0] and names[1]
edit
As you can see the code for inputting the data is duplicated and so could be refactored
How to take multiline strings input (separated by enter (\n) and ends with (\n\n)) from console.
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
String[] tokens = scanner.nextLine().split("\\n");
System.out.println(Arrays.toString(tokens));
}
Example:-
my name is xyz
i am from india
thats it
\n
\n
(done after double enter)
output :
my name is xyz
i am from india
thats it
You can set the delimiter to that object...
Scanner in = new Scanner(System.in);
in.useDelimiter("\n\n");
use Delimiter
eg: in.useDelimiter("\n\n");
hope this works
I want the user to only enter his age. So I did this program :
Scanner keyb = new Scanner(System.in);
int age;
while(!keyb.hasNextInt())
{
keyb.next();
System.out.println("How old are you ?");
}
age = keyb.nextInt();
System.out.println("you are" + age + "years old");
I found how to prevent user from using string by using the while loop with keyb.hasNextInt(), but how to prevent him from using the whitespace or from entering more input than his age ?
For example I want to prevent this kind of typing "12 m" or "12 12"
Also, how can I clear all existing data in the buffer ? I'm facing an infinite loop when I try to use this :
while(keyb.hasNext())
keyb.next();
You want to get the whole line. Use nextLine and check that for digits e.g.
String possibleAge = "";
do {
System.out.println("How old are you ?");
possibleAge = keyb.nextLine();
} while (!possibleAge.matches("\\d+"))
Your problem is that the default behaviour of Scanner is to use any whitespace as the delimiter. This includes spaces. This means that a 3 a is in fact three tokens, not one. You can change the delimiter to a new line so that a 3 a becomes a single token, which will then return false for hasNextInt.
I've also added an initial question, because in your example the first input was taken before asking any questions.
Scanner keyb = new Scanner(System.in);
keyb.useDelimiter("\n"); // You can try System.lineSeparator() but it didn't work in IDEA
int age;
System.out.println("How old are you?");
while(!keyb.hasNextInt())
{
keyb.next();
System.out.println("No really. How old are you?");
}
age = keyb.nextInt();
System.out.println("You are " + age + " years old");
String age = "11";
if (age.matches(".*[^0-9].*")) {
System.out.println("Invalid age");
} else {
System.out.println("valid age");
}
If age contains other then digits then it will print invalid age.
This code is only showing output as
Enter Name of the item you want to add Enter the price of item
While it should take the name before it takes the double as input.
newMenu = new Scanner(System.in);
System.out.println("Please select on of the menuItems \n 1. Premium \n 2. Discount \n 3. Standard ");
FileWriter file = new FileWriter(f,true);
//Create an instance of the PrintWriter class using output as the argument
PrintWriter abc = new PrintWriter(file);
if(newMenu.nextInt()==1){
abc.println("Premium");
System.out.println("Enter Name of the item you want to add");
String name=newMenu.nextLine();
abc.println(m1.getNoOfItems()+1+" "+name);
System.out.println("Enter the price of item");
double price=newMenu.nextDouble();
abc.println(price);
m1.setNoOfItems(m1.getNoOfItems()+1);
}
Instead of :
if(newMenu.nextInt() == 1){
You can use newMenu.nextLine() instead like this :
if (Integer.parseInt(newMenu.nextLine()) == 1) {
While it should take the name before it take the double as input.
You don't get this scenario because you don't consume the all the line.
You need to consume the \n after .nextInt() Do
if(newMenu.nextInt()==1){
newMenu.nextLine();
//..rest of code
I am doing a project with using arraylist. I want a alphabet and a number in order from user .
char karakter = klavye.next().charAt(0);
int sayi = klavye.nextInt();
When i write that after first input cursor pass to the bottom line
like:
A
7
But i want like that A 7 Why do not they side by side? How can i do this?
If I am understanding you correctly you just want the user to be able to enter two tokens on the same line. One approach is to just get the whole line and then split it into tokens.
Scanner klavye = new Scanner(System.in);
String tokens[] = klavye.nextLine().split(" ");
while(tokens.length < 2) {
System.out.println("Bad line, enter again:");
tokens = klavye.nextLine().split(" ");
}
char karakter = tokens[0].charAt(0);
System.out.println("karakter = " + karakter);
int sayi = Integer.valueOf(tokens[1]);
System.out.println("sayi = " + sayi);
The user should type A 7 then press the enter key only once at the end.