Why can't I read the newline characters of a text with an anonymous instance of Scanner(System.in)? - buffer

before complaining about "repeated question" read the whole asking, this is different.
It's not the same old question about why nextLine() after nextInt() doesn´t work.
I got this text on a file:
BEAR
PARROT
FROG
HORSE
When I copy the text on my clipboard I expect the nextLine() to read my clipboard line by line.
When I try it with an anonymous Scanner object I got this.
while (true) {
String animal = new Scanner(System.in).nextLine(); //I copy
//the whole text on my clipboard
System.out.println("animal = " + animal);
}
output:
BEAR
PARROT
FROG
HORSE
animal = BEAR
So it just read the first newline character, BEAR.
So I tried with a local Scanner(System.in) variable.
Scanner input = new Scanner(System.in);
while (true) {
String animal = input.nextLine(); // I copy
//the whole text on my clipboard
System.out.println("animal = " + animal);
}
output:
BEAR
PARROT
FROG
HORSE
animal = BEAR
animal = PARROT
animal = FROG
animal = HORSE
It read all of the newline characters in the clipboard.
Can someone explain, what's going on? Maybe the question is more about the buffer than an anonymous intance of Scanner.
psdta: So sorry for so many edits, I really struggled formatting the text.

Related

How to print text form a particular part of a string?

I want to know how to get text form a particular part of a sentence this is what i mean. Hello (new text)., in the brackets it says new text and i want to find the text in the brackets and print only the text in the brackets.
With the ( and ) surrounding the text, you can use string.match:
str = "Hello (new text)"
print(str:match("%((.+)%)"))
%((.+)%)") is a pattern that captures every character between the ( and ).
Resources:
Lua 5.3 Reference Manual - 6.4.1 Patterns
The easiest way to do this is just to cancel out the Hello in your string.
local msg = "Hello (obama)"
msg = msg:gsub("Hello %(", ""):gsub("%)", "")
If the first part of the string is dynamic and is not always hello you can try this:
local msg = "Bye (obama)"
local pattern = "%p.+%p"
print(({string.match(msg, pattern):gsub("%)", ""):gsub("%(", "")})[1])
This soloution may not be the best and most efficient but it certainly works. I hope it helped you. Let me know if you have any questions.

Unexpected result stored in attribute populated by DXL script

I'm now the maintainer for a bunch of dxl scripts, and I am in the process of testing them all.
The script in question formats multiple strings into a buffer, then converts back to a string. This string is then concatenated with another string, and then stored in an attribute. The code is below (variable names and number of function parameters have been sanitised FYI):
string formatForField(string s1, string s2){
Buffer buff = create();
buff += s1
buff += "\n";
buff += s2;
string formattedString = tempStringOf(buff);
delete(buff);
return formattedString;
}
void alterField(string inputString1, string inputString2){
string formattedChange = formatForField(inputString1, inputString2);
string oldValue = currentObject."Attribute Name";
if (oldValue != "") {
oldValue = oldValue "\n---\n";
}
string newValue = oldValue formattedChange;
currentObject."Attribute Name" = newValue;
}
The problem is that occasionally instead of the expected result, the current objects's value of "Attribute Name" has □ appended onto the previous value.
If for example, the value of (current object)."Attribute Name is "Lorem Ipsum" and after running the script, the expected value of (current object)."Attribute Name is:
Lorem Ipsum
---
inputString1
inputString2
Then occasionally, the value of (current object)."Attribute Name will actually be:
Lorem Ipsum
---
□
I've not been able to locate any similar reported issues online, so I'm posting here. I'm not sure what is happening here, as 99% of the time the script provides the expected output. Interestingly, if the attribute contains the white square after running the script, running the script again with the same input will result in the expected output...
Edit: This image shows the white square I am on about, as it doesn't seem to show up in the live post view
Extra Edit: Upon further inspection, I've found that sometimes additional random characters are included. The right column is supposed to contain text, not the random characters it does contain.
Forgive me if I state the obvious here, but I'm not really sure where the problem exactly lies. From what I read, alterField has the following characteristica.
Expected input are two strings. Precondition: both of them must have a value.
The function checks whether the attribute "Attribute Name" already has a value. If so, the old value shall be preserved and the two strings shall be added to the old value, separated by a line of dashes. The two strings are concatenated by a carriage return.
So, after starting the following script
Object currentObject = first
currentObject."Attribute Name" = "good morning"
String partOne = "good"
String partTwo = "evening"
alterField (partOne, partTwo)
alterField (partOne, partTwo)
alterField (partOne, partTwo)
the attribute "Attribute Name" of the first object of the Module will have the following content:
good morning
---
good
evening
---
good
evening
---
good
evening
If the precondition is not met and partOne is empty (""), partTwo is "evening", then after three calls of alterField the attribute will look like this:
good morning
---
evening
---
evening
---
evening
The question is: what do you expect to happen?
Realised the error. The buff was being deleted just after calling
tempStringOf and so the memory being referenced was deallocated, causing the weirdness. Silly mistake on my part.

extract data from string in lua - SubStrings and Numbers

I'm trying to phrase a string for a hobby project and I'm self taught from code snips from this site and having a hard time working out this problem. I hope you guys can help.
I have a large string, containing many lines, and each line has a certain format.
I can get each line in the string using this code...
for line in string.gmatch(deckData,'[^\r\n]+') do
print(line) end
Each line looks something like this...
3x Rivendell Minstrel (The Hunt for Gollum)
What I am trying to do is make a table that looks something like this for the above line.
table = {}
table['The Hunt for Gollum'].card = 'Rivendell Minstrel'
table['The Hunt for Gollum'].count = 3
So my thinking was to extract everything inside the parentheses, then extract the numeric vale. Then delete the first 4 chars in the line, as it will always be '1x ', '2x ' or '3x '
I have tried a bunch of things.. like this...
word=str:match("%((%a+)%)")
but it errors if there are spaces...
my test code looks like this at the moment...
line = '3x Rivendell Minstrel (The Hunt for Gollum)'
num = line:gsub('%D+', '')
print(num) -- Prints "3"
card2Fetch = string.sub(line, 5)
print(card2Fetch) -- Prints "Rivendell Minstrel (The Hunt for Gollum)"
key = string.gsub(card2Fetch, "%s+", "") -- Remove all Spaces
key=key:match("%((%a+)%)") -- Fetch between ()s
print(key) -- Prints "TheHuntforGollum"
Any ideas how to get the "The Hunt for Gollum" text out of there including the spaces?
Try a single pattern capturing all fields:
x,y,z=line:match("(%d+)x%s+(.-)%s+%((.*)%)")
t = {}
t[z] = {}
t[z].card = y
t[z].count = x
The pattern reads: capture a run of digits before x, skip whitespace, capture everything until whitespace followed by open parenthesis, and finally capture everything until a close parenthesis.

How do I remove newlines from a String in Dart?

How do I remove the newlines from a string in Dart?
For instance, I want to convert:
"hello\nworld"
to
"hello world"
You can use replaceAll(pattern, replacement):
main() {
var multiline = "hello\nworld";
var singleline = multiline.replaceAll("\n", " ");
print(singleline);
}
#SethLadd's answer is correct, but in a very basic example.
In the case of a multiline input with text like:
Hello, world!
{\n}
I like things:
- cats
- dogs
{\n}
I like cats, alot
{\n}
{\n}
and more cats
{\n}
{\n}
{\n}
. (ignore this dot)
In the case of the above, your string is represented like this:
Hello, world!\n\nI like things:\n- cats\n- dogs\n\nI like cats, alot\n\n\nand more cats\n\n\n\n
Using #SethLadd's solution, I will be left with:
Hello, world!I like things:- cats- dogsI like cats, alotand more cats
which is certainly not the desired outcome. I suggest using the commonly used regex approach of to tackle the problem.
Calling .trim() will remove the last 4 \n (and any \n infront too).
If one wishes, you could limit new lines to a single open line with something like
text.trim().replaceAll(RegExp(r'(\n){3,}'), "\n\n")

Capital in Bibtex

I want to show some letters in Bibliography as capital. For example:
#misc{libsvm,
abstract = {LIBSVM is an implbmentation of Support vector machine (SVM).},
author = {Chang, Chih-Chung},
howpublished = {\url{http://www.csie.ntu.edu.tw/~cjlin/libsvm/}},
keywords = {svm},
posted-at = {2010-04-08 00:05:04},
priority = {2},
title = {LIBSVM.},
url = "http://www.csie.ntu.edu.tw/~cjlin/libsvm/",
year = {2008}
}
But "LIBSVM" is not shown as it is:
[3] Chih-Chung Chang. Libsvm. http://www.csie.ntu.edu.tw/ ̃cjlin/libsvm/,
2008.
How can I make the letters capital? Thanks and regards!
Generally, to keep BibTeX from turning your letters lowercase, enclose them in {}:
title = {A History Of {StudlyCaps}}
will produce "A history of StudlyCaps."
Alceu Costa is correct that all-capital abbreviations should be formatted in small capitals, but that is a different matter than this.
The \textsc is used to format the text in small capitals. You can do this in your .bib file:
title = {\textsc{LIBSVM}}
Place {} brackets around anything you want to keep in CAPS.
For example:
#type{bibkey,
title = "{M}y {B}ibliography is the {B}est!",
author = "{ABCDE}",
}
In the case of IEEEtran LaTeX Class template, one can simply do the following:
title = "{3D Open Source Framework for Something you need}"
A template can be found at the following link
http://www.ieee.org/conferences_events/conferences/publishing/templates.html

Resources