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'm making a textbook indexer and I'm formatting the output file. there are periods between the word and its frequency.
desired textfile output:
word..............25:
word...............3:
word...............2:
I'm trying to get the lines to be aligned to the : but I can't get it under the semicolon if the numbers are more than 1 digit.
Does anybody have any ideas for java string formatting for the desired output?
for reference the biggest word is 23 characters and it's frequency is 1.
Since in the comment, the OP attempted to use String.format(), here is an approach to consider. Rather than trying to get the number to align right with the "%23d", align the word and the count separately.
String.format("%-23s%2d:", getWord(), count);
The %-23d will format the getWord() in 23 spaces, left aligned, then the %2d will right align the "count".
Example output:
Hello...................9:
Goodbye................42:
whatever...............17:
Note I just used the same .replace() approach as the OP for quickness sake.
See this attempt here at ideone.com
Related
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 11 months ago.
Improve this question
If I had a .txt file like this:
frames: 312
speed: 100 fps
2
Circle
r: 20
x: 30
y: 35
and I wanted to get the useful information from the file, how would I do that? I'm using java.util.Scanner.
I want to get all the integers, and I want to read when it skips a line and the word "Circle"
split it by line / loop over the lines.
If the line contains a ':' split the line by ':', this gives you an array of length 2.
Create a Map<String, Int>.
Use array[0] for the map key, use array[1] for map value, check that the value can be an int ("100 fps" will fail) before adding it to the Map. Also remember to trim the whitespace.
If the line doesn't contain a ':' you can add it to a List<String>.
Lots of other ways to do it, depends on the file format consistency and what you want to use the data for etc.
You end up with:
Map:
[["frames":42], ["speed":100], ["r":20], ["x":30], ["y":35]]
List:
["2","","Circle"]
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 5 years ago.
Improve this question
Write a java code to check if the given string is even or not? Eg. aabbcc, aacbbc is even string.
I was asked this program in one interview. Actually i did not understand what is frequency here.
For a string s of length n, consider s[0] XOR s[1] ... XOR s[n - 1] where [i] is the (i)th letter of the string. Use java.lang.String#charAt(int) in java to extract a character.
If that is zero you have an even string, else you have an odd string.
Test n % 2 first for an immediate pay rise: If that is not zero then there must be at least 1 occurrence of a character that appears an odd number of times.
Normally folk who wrote computer games in machine code as kids in the 1980s will ask this question as it seems obvious to them. I doubt it is any more: XOR was a very fast way of writing sprite images.
Depending on what the interviewer was asking, string frequency is either,
how many times a string is found in another string.
how many times a character is found in a string.
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 6 years ago.
Improve this question
For example, if I have a string (24/25), how would I go about being left with two double values of 24 and 25. My objective to to eventually, given a set of number of that form, divide each of them, and add to get the average. I'm fairly new to Java and I honestly am so confused as where to even begin. Thank you!
As Andreas said. First you need to take the substring between brackets. Use String.substr
Now split 24/25 to 24 and 25. You can use String.split
Then you can parse them with Double.parse
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 7 years ago.
Improve this question
I am trying to validate a String that represents a list of ranges as known from the print dialog where you indicate which pages to print. Examples:
1
1-10
1,20
1-5,10
1-3,9-11
I guess anything that is not "finished" is invalid, such as:
1-
1,
,3
1-4,
1,2,4-
As you can guess - I am failing at it ;)
Any help is highly appreciated
You can try the regex (\d+(\-\d+)?(,(?!$))?)+, like:
String str = "1-1,12";
System.out.println(str.matches("(\\d+(\\-\\d+)?(,(?!$))?)+"));
Where:
\d+ 1 or more digit
(\\-\\d+)? followed or not by - and 1 or more digit
(,(?!$))? and all the above is followed or not by ',' (but not as the last position of the string - (?!$) is negative look-ahead of the end of the string)
+ says, that all above must be presented at least one time
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 8 years ago.
Improve this question
I don't know a single bit about regex. I want to find certain occurences in my java project using eclipse. The words could be
SEQ_NUM
SEQNUM
SEQNUMBER
SEQ_NUMBER
searching strings based on word "seq" would generate lots of results. What regex should suffice my problem?
These four cases can be combined to the following regex:
SEQ_?NUM(BER)?
SEQ_?NUM(BER)?
SEQ - must exist
_? - '_' can appear 0 or 1 time
NUM - NUM must also exist
(BER)? - 'BER' can appear 0 or 1 time
This should work for you.
SEQ[_NUMBER]+