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 2 years ago.
Improve this question
I am trying to learn java but I am lost with the challenge below
what it does:
0. prompt user for a String
1. read a String from the user
2. print the character in the string with the lowest Unicode
3. continue until the user enters the string 0
Example:
enter a string (end with 0): ksjdhfasksjsh
first: a
enter a string (end with 0): ksdjdhfgKKFHDFGASDJSDHsjsdhjasjhdsj
first: A
You need to:
Set a char variable min to Integer.MAX_VALUE;
iterate thru the String and get each character
Compare to min. If less than min assign char to min
when done the min value will be the lowest value character
so print it.
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 2 years ago.
Improve this question
I have a text file that looks something like this:
1 Song one
2 Song two
3 Song three
...
How do I read a line by line and extract number and String separately from a single line, and let's say print them to the console with:
String title; // extracted from current line
int num; // extracted from current line
System.out.println("Number: " + num + "Title: " + title);
How do I read a line by line
Use a BufferedReader and its readLine() method.
How do I extract number and String separately
Use line.indexOf(' ') then substring(...) to get the 2 parts.
Then use Integer.parseInt(...) on the first part to get number.
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 3 years ago.
Improve this question
From the following value
[AAMP-PLAYER]aamp pos: [14..187..202..2196879032]
I want to get the last number 2196879032
Some times value will be -1
[AAMP-PLAYER]aamp pos: [14..187..202..-1]
Instead of split method how can i extract last digit using regex method
You could match the last (possibly negative) number at the end of the string, just before the closing ]:
(\-?\d+)\]$
A number is last if it is not followed (following it anywhere, not just immediately) by any other number.
(\-?\d+)(?!.*\d)
We could try using a String#replaceAll option here:
String input = "[AAMP-PLAYER]aamp pos: [14..187..202..2196879032]";
String num = input.replaceAll("^.*\\.\\.(-?\\d+).*$", "$1");
System.out.println(num);
This prints:
2196879032
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
Few Java questions:
How to prompt user for input?
How to save input from user in an array?
How to average numbers in an array?
How to compare numbers?
How to print information back to the user?
The scenario is as follows: let the user to input 10 numbers into an array. Then loop through the array and average it. Then loop through the array again and compare each value against the average and print the number (and/or loop index) if it is lower than the average of the 10 numbers user entered.
Read the array.
Calculate the average.
Loop through the array
check if it has value less than average
if yes print the index
if no check for the next
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 8 years ago.
Improve this question
Suppose you have a string as input which has names separated by the underscore character. Write a program that outputs the same string after removing any duplicate names in it (retain first occurrence when multiple occurrences are present), and adds the number of times the name occurs, immediately after the name. For example, Tom_Dick_Harry_Joe_Dick_Chris becomes Tom1_Dick2_Harry1_Joe1_Chris1.
I will provide no code but the following might help :
Split the array with '_'
Maintain a Map<String,Integer> occurrences for counting the occurrences of each name and a List<String> apparitionOrder
For each word of the array :
if the word is in occurrences, increment the value associated to the key
else, put this name in the map associated with 0 and append it to apparitionOrder
Instantiate a new array of the size of apparitionOrder
Fill the new array accordingly to apparitionOrder and occurrences
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 9 years ago.
Improve this question
I have a table with an "identifier" attribute.
I need this attribute to be unique and auto-incremented by one (length of the attribute must be six digits).
For example the first time I persist an entity, the identifier should be 000001 and the second one 000002 and so on.
Could you please tell me how to implement this requirement?
Thanks in advance.
You will have to convert your number as a String.
To do so, you can use String.format.
String.format("%06d", num)
'0' The gaps are filled with 0.
'6' The result has a size of 6.
'd' The result is formatted as a decimal integer.
For example:
int num = 8;
String var = String.format("%06d", num);
will return:
var = "000008"
EDIT: The syntax of String.format can be found here: http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax
If you want to retrieve an ID from your base, just parse the String as a Number:
String identifier = "000008"; // Returned ID from database
Number num = Integer.parseInt(identifier); // num = 8