How to vertically-align scanner input in Java? - java

I'm having trouble figuring this out. What am I missing in my code such that my expected result on the terminal looks like this? (first image). I hope to vertically allign the user input ( 10,20,30 ).
I am using the scanner for user input and it prompts for input after a formatted string is displayed on the terminal.
However, mine looks like this.
Here is a portion of the code.
System.out.println("Please place your order");
System.out.println("-----------------------");
System.out.printf("No. of %s: " , item_1);
order_QTY_1 = input.nextInt();
System.out.printf("No. of %s: " , item_2);
order_QTY_2 = input.nextInt();
System.out.printf("No. of %s: " , item_3);
order_QTY_3 = input.nextInt();
Thank you so much and any help is much appreciated.

Related

How to write in between characters in console using Scanner

I want to be able to write in between characters in the console using Scanner. The code Im using:
Scanner sc = new Scanner(System.in);
System.out.print("[customer ID: \t]");
int id = sc.nextInt();
the current output would be:
[customer ID: ]
and if i tried typing in between the brackets this would happen:
[customer ID: ]123
is there any way to make the text appear in between the brackets?
intended output:
[customer ID: 123]
The answer is no. You need to hit the enter key for sc.nextInt() to read the input. Any output would be in the next line and not on the line where you enter the input.
System.out.print("[Customer ID: " + sc.nextInt() + "]";
24
[Customer ID: 524]
The closest I can think of this is something like this:
System.out.print("Customer ID: ");
int id = sc.nextInt();
which produce the output below:
Customer ID: 1234
The answer to the question is no. Once the output is sent to the console (or some other output device) it's a done deal. You cannot retroactively modify the output that has already been consumed and modify it. You have to create a new output and send it to the output device. It is the same as printing a piece of paper and then make a correction directly to the paper from your program once it is printed out.
Alternatively, you can do something like this
Scanner sc = new Scanner(System.in);
System.out.print("Enter your customer ID: ")
int id = sc.nextInt();
System.out.print("[customer ID: " + id + "]");
Unfortunately, it is impossible to do what you asked.

How do I put characters in front of userInput on a console? [duplicate]

This question already has answers here:
How to keep my user input on the same line after an output?
(4 answers)
Closed 1 year ago.
I am making a console game on codiva.io.. and I would like to put some characters in front of the text of a user's input. Basically, I want to know how to force the user to type in some characters right before they hit enter.
Example code:
import java.util.Scanner;
class test {
public static void main(String[] args) {
System.out.println(" | Welcome to the [...] Game!\n");
Scanner useri = new Scanner(System.in);
String userin = useri.nextLine();
if (userin.equals("devcom")){
System.out.println("\nCommands| Communication with developer channel [ACTIVATED]\n");
Scanner useris = new Scanner(System.in);
String userins = useris.nextLine();
if (userins.matches("^~sg\\h+(\\S+(?:\\h+\\S+)*)$")){
System.out.println("\n>>>>>>>>|");
System.out.println("Commands| Test command successful. Communication to developer channel [DEACTIVATED]");
System.out.println(">>>>>>>>|\n");
}
}
}
}
Output:
For normal console output, I use:
" | "
And for commands I use:
">>>>>>>>| "
"Commands| "
">>>>>>>>| "
My question is, how do I input the normal console output:
" | "
before the text inputted by the user?
For example; where it says "devcom", I inputted devcom, (in photo 1), I want it to say:
" | devcom"
even though the user only types the "devcom" part.
In order to achieve your desired results, you have to put:
System.out.print(" | ");
in front of the line:
String userins = useris.nextLine();
This way, before the console can check for the nextLine, you printed the text you want to be before the userInput. Therefore inputting text before the userInput.

Why am I getting an ArrayIndexOutOfBoundsException when I try to tokenize the input from the console (Java)?

I'm trying to separate the input from the console using the split method, and then putting each of these values into separate containers, and I keep on getting this error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1. I assume the issue is that it ignores the input after the first space, but I am clueless as to why and how to solve that.
Could someone tell me what I'm doing wrong and advise what I should do so that the text after the space is stored in my container? Thank you in advance.
Scanner s = new Scanner(System.in);
System.out.println("Input the name and phone no: ");
String text = s.next();
String[] temp = text.split(" ");
String name = temp[0];
String phoneNoTemp = temp[1];
System.out.println(name + ": name");
System.out.println(phoneNoTemp + ": phoneNoTemp");
The input I tried it with was:
Input the name and phone no:
kate 99912222
Sidenote: Yes, I did import the scanner
Try to use s.nextLine() instead of s.next() because the next() method only consumes until the next delimiter, which defaults to any whitespace.

How to make it so the user can replace a sentence they typed can be replaced in Java

So I am trying to make the program ask the user the following and get the following answer:
For example:
Type a 2nd Sentence Below:
This is a book (user inputs this)
Choose what string of characters do you want to replace:
book (user inputs this)
Choose what new string will be used in the replacement:
car (user inputs this)
The text after the replacement is: This is a car.
import java.util.*;
import java.util.Scanner;
public class StringManipulation {
public static void main(String[]args) {
/*This is where the user can type a second sentence*/
System.out.println("Type a 2nd Sentence Below:");
Scanner sd = new Scanner(System.in);
String typingtwo = sd.nextLine();
String sending;
/*Here the program will tell the user a message*/
System.out.println("This is your sentence in capital letters:");
sending = typingtwo.toUpperCase();
System.out.println(sending);
/*Here the program will tell the user another message*/
System.out.println("This is your sentence in lower letters:");
sending = typingtwo.toLowerCase();
System.out.println(sending);
System.out.print("Your Token Count:");
int FrequencyTwo = new StringTokenizer(sending, " ").countTokens();
System.out.println(FrequencyTwo);
String charactertwo = new String(typingtwo);
System.out.print("Your Character Length:");
System.out.println(charactertwo.length());
String repWords;
String newWord;
String nwords;
String twords;
System.out.println("Choose what string of characters do you want to
replace");
repWords = sd.next();
System.out.println("Choose what new string will be used in the replacement");
nwords = sc.next();
twords = typingtwo.replace(repWords,nwords);
System.out.printf("The text after the replacement is: %s \n",nwords);
}
}
I have tried everything but for some reason I keep getting the word that they chose at the end only. Pleas help!
try using Scanner.nextLine instead of Scanner.next
refer to the Java API documentation to understand the difference between the two
Here is another problem:
twords = typingtwo.replace(repWords,nwords);
System.out.printf("The text after the replacement is: %s \n",nwords);
You are printing nwords instead of twords.
Two errors i could see.
nwords = sc.next(); here it should give compilation error as scanner instance name is sd.
You are trying to print nwords at the end. it should be "twords".

Basic console input and output

I'm looking for a way to basically give back to the user some console output that looks identical to what they type and then prompt again for more input. The problem is I have a method that modifies each String discovered that does not consist of white space.
At the end of each sentence given by the user, I am trying to figure out a way to get a newline to occur and then for the prompt to output to the console again saying "Please type in a sentence: ". Here is what I have so far...
System.out.print("Please type in a sentence: ");
while(in.hasNext()) {
strInput = in.next();
System.out.print(scramble(strInput) + " ");
if(strInput.equals("q")) {
System.out.println("Exiting program... ");
break;
}
}
Here is what is displaying as console output:
Please type in a sentence: Hello this is a test
Hello tihs is a tset
And the cursor stops on the same line as "tset" in the above example.
What I want to happen is:
Please type in a sentence: Hello this is a test
Hello tihs is a tset
Please type in a sentence:
With the cursor appearing on the same line as and directly after "sentence:"
Hopefully that helps clear up my question.
Try this, I didn't test it but it should do what you want. Comments in the code explain each line I added:
while (true) {
System.out.print("Please type in a sentence: ");
String input = in.nextLine(); //get the input
if (input.equals("quit")) {
System.out.println("Exiting program... ");
break;
}
String[] inputLineWords = input.split(" "); //split the string into an array of words
for (String word : inputLineWords) { //for each word
System.out.print(scramble(word) + " "); //print the scramble followed by a space
}
System.out.println(); //after printing the whole line, go to a new line
}
How about the following?
while (true) {
System.out.print("Please type in a sentence: ");
while (in.hasNext()) {
strInput = in.next();
if (strInput.equals("q")) {
System.out.println("Exiting program... ");
break;
}
System.out.println(scramble(strInput) + " ");
}
break;
}
The changes are:
You should be printing "Please type in a sentence: " inside a loop to have it print again.
I think you want to check if the strInput is "q" and exit BEFORE printing it, i.e. no need to print the "q", or is there?
Use println to print the scrambled strInput so that the next "Please type in a sentence: " appears in the next line, and the cursor appears on the same line as " ... sentence: " because that is output by System.out.print (no ln)

Categories

Resources