How to drop Enter, when I'm using Scanner? - java.util.scanner

I want to write a program where you don't need to press Enter, when I'm using Scanner. Is there any way to avoid that? It supposed to be very simple program, but every time I am write something, I have to press enter.

If you are using a scanner like
Scanner userInputScanner = new Scanner(System.in);
String scanInfo = userInputScanner.nextLine();
Then you need to press enter after because the docs say the method:
Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.
Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present."
Essentially, by pressing enter you are telling the computer your input is done
If you need something that can scan information without pressing enter, you can scan a text file like this:
String input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner(input);
Instead of input, you can use a txt file as well
Note: in the string input example, you would likely want to use a delimiter (for instance separating the String input at instances of white space)
Scanner s = new Scanner(input).useDelimiter(Character.isWhitespace);
Personally, I like using txt files because with each line in the file I can use .hasNext() to get the data.

Related

Is there an end= equivalent for inputs?

So as I'm sure you know there's a specific operator for print() functions called end.
#as an example
print('BOB', end='-')
#would output
BOB-
So is there something like this for inputs? For example, if I wanted to have an input that would look something like this:
Input here
►
-------------------------------------------------------
And have the input at the ► and be inside the dashes using something like
x = input('Input here\n►', end='-------')
Would there be some equivalent?
EDIT:
Just to be clear, everything will be printed at the same time. The input would just be on the line marked with the ►, and the ---- would be printed below it, but at the SAME time. This means that the input would be "enclosed" by the ---.
Also, there has been a comment about curses - can you please clarify on this?
Not exactly what you want, but if the --- (or ___) can also be on the same line, you could use an input prompt with \r:
input("__________\r> ")
This means: print 10 _, then go back \r to the beginning of the line, print > overwriting the first two _, then capture the input, overwriting more _. This shows the input prompt > ________. After typing some chars: > test____. Captured input: 'test'
For more complex input forms, you should consider using curses.
When using basic console IO, once a line has been ended with a newline, it's gone and can't be edited. You can't move the cursor up to do print anything above that last line, only add on a new line below.
That means that without using a specialized "console graphics" library like curses (as tobias_k suggests), you pretty much can't do what you're asking. You can mess around a little with the contents of the last line (overwriting text you've already written there), but you can't write to any line other than the last one.
To understand why console IO works this way, you should know that very early computers didn't have screens. Instead, their console output was directly printed out one line at a time on paper. While some line printers could print several characters on the same spot (to get effects line strikethrough or underline), you couldn't unprint anything once it was on the paper. Furthermore, the paper feed only worked in one direction. Once you had sent a newline character that told the printer to advance the paper, you couldn't go back to an old line again.
I believe this would solve your problem:
print(f">>> {input()} ------")
OR
print(f"{input(">>>")} ------")
F-strings are quite useful when it comes to printing text + variables.

How can I make my plugin copy a line into the clipboard?

This Sublime Text plugin randomly selects a position in a text file and places the cursor there as if the user had clicked on that position using the mouse.
I also want it to copy the line of that random position into the clipboard. Is it possible to do that in the same program?
import sublime, sublime_plugin
import random
class JumpToRandomPositionCommand(sublime_plugin.TextCommand):
"""
When invoked, randomly select a character in the current
file and jump the cursor to that position. Does nothing
if the current file is empty or if the current view does
not represent a file.
"""
def run(self, edit):
view = self.view
if view.size() > 0 and view.settings().get("is_widget", False) == False:
view.sel().clear()
pos = random.randrange(0, view.size())
view.sel().add(sublime.Region(pos, pos))
view.show(pos)
There are a couple of ways that do what you want to do here. So, here are some snippets of code you can add to the plugin you provided in your question. In each case, these bits of code would go into the plugin inside of your final if statement as the last line (i.e. after the call to view.show(). The position is important; is has to only happen if the position was changed, and (in the case of the first snippet) it has to happen after the cursor has moved to the new location.
First, the easiest one. Sublime contains the following setting in the default preferences:
// If true, the copy and cut commands will operate on the current line
// when the selection is empty, rather than doing nothing.
"copy_with_empty_selection": true,
When this is turned on (which as seen here it is by default), if you try to trigger the copy or cut commands when there's no selection, each command will assume you meant to work on the whole line.
If you're working in this way, the following line added to your plugin is enough to do the job:
view.run_command('copy')
That is, ask the view to run the copy command, which is what would happen if you manually pressed the key binding. The plugin sets a single cursor into the line in a random position with an empty selection, so this would operate on that empty selection and copy the line.
If on the other hand you don't use this setting, or you want the command to work regardless of what it is set to, then you need to do a bit more work. That would look something like this:
line = view.full_line(pos)
text = view.substr(line)
sublime.set_clipboard(text)
Here we ask Sublime to tell us what span of text represents the line that the random position is sitting inside of. We then extract that text from the buffer and add it to the clipboard.

Multiline input prompt indentation interfering with output indentation

I have a function that prints the number of pixels found in an image and then asks the user how they would like to proceed. As long as the interpreter hasn't moved on from the function I want all the output to be indented accordingly.
One such 'sub output' (the input prompt) needs to be multiple lines. So I kick off with the 3*quote (''') followed by two spaces to create the indentation. At the end of the question 'how would you like to proceed?' I use a hard return. An extra indentation is assumed by the text editor so I remove it causing the following list of suggestions to line up flush with the input variable command. Here's how it looks:
def returnColors():
#
# lots of code that does stuff...
#
print("The source image contains", lSize, "px.")
print("")
command=input(''' What would you like to do? You can say:
get all
get unique
''')
The problem with this is that the interpreter is acknowledging the indentation that separates the function body from the function statement as actual string contents, causing the output to look like this:
The source image contains 512 px.
What would you like to do? You can say...
get all
get unique
|
The only way to avoid this is by breaking indentation in the interpreter. Although I know it works, it doesn't look very good. So what options do I have?
EDIT: Just because I have the screenshot_
One thing that you should keep in mind is that once you have start a multiline string declaration, all the text until it is closed is taken as is and syntax (ie, indentation) is no longer considered.
You can start your multiline with an explicit new line so that everything in the multiline string can be indented together in code.
IE.
command=input('''
What would you like to do? You can say:
get all
get unique
''')
would print out the prompt with a new line on top, but the formatting of the text is more explicitly shown and should appear as seen.
OR you could use the \n for each new line in the string to get it formatted more correctly and remember to use a single \ after each new line. E.g.
instead of:
''' What would you like to do? You can say:
get all
get unique
'''
Try
' What would you like to do? You can say:\
\n\
\n get all\
\n get unique\
\n'
The indent won't matter, no matter where you use \n at the beginning of new line, the input() will output the same. This is will give the same input() string:
' What would you like to do? You can say:\
\n\
\n get all\
\n get unique\
\n'

VIM does not replace the word after the dot punctuation. How to change it?

I have the following problem
This is text:
printf("sysname %s",ut.sysname);
I want to use vim to replace sysname line by line. I type the command in my gvim:
:s/sysname/version
I want to get the output like this:
printf("version %s",ut.version);
But I get the output like this:
printf("version %s",ut.sysname);
What am I doing wrong?
you're missing the g command that applies to all matches on current line, instead of only the first one:
:s/sysname/version/g
as a bonus:
:%s/sysname/version/g
will replace all occurences in current file, not only on the current line.
To do it on one line
:s/sysname/version/g
You can also use the qq macro recorder before typing that in, and press q after, and then use #q to replay that on any other lines you want to replace that on. Or press : up to select old commands.
Or to do it on every single line:
:%s/sysname/version/g
However with replacing every line you should be careful. If there is a lot of text try making your replacements more specific.
I would do
:%s/\(printf("\)sysname\(.*\)sysname/\1version\2version

Append general buffer to the end of every line in VI

I'm trying to add the contents of the general buffer to the end of every line. I'm sure this is fairly simple, however, an hour of google searches have lead me nowhere.
This is what my file looks like
::Things to bring camping
--matches
--tent
--sleeping bags
--inflatable bed
--firewood
--camping stove
--skillet
I want to add "::Things to bring camping" to the end of every line.
This is i have figured out so far.
/:: -> brings me to the line in question
Y -> yanks the entire line to the general buffer
I tried :%s/$/\p -> this added a "p" to the end of every line.
My problem is with step 3. How do I tell the "search and replace command" to used the "p" (the contents of the general buffer) instead of the "p" the character
Thank you so much for your help.
Just a suggestion: If you try doing it with a macro, you will be able to use 'p' to add the contents of the general buffer.
Sorry, I had to go into vim and find out.
The way to copy your entire line while in command mode, is:
^r "
(that's CTRL and r, then " )
That should paste the entire line you yanked into your search and replace command
For step three, instead of \p, you should use ctrl-R-a. Hold down the control key and type an uppercase "R", continue holding control, and type a lowercase "a".
For a line with multiple words, use ctrl-R-" instead.
I agree with using a macro - they're very powerful.
In this case I took your list example and positioned it at the first colon.
I used y$ to grab the remainder of the line in the buffer.
Then I recorded the macro - I chose 1.
q1
j$pq
Then you can call it for any number of rows in your list. E.g. 10#1
Learned something figuring this one out ...
:%s/$/\=getreg()/
The \= says that what follows is an expression to be evaluated, and the getreg() call gets the contents of the register, by default the "general buffer" as it used to be called by vi.

Resources