Reputation: 9
I first need to receive an unknown number of string inputs from a user and create a loop that will terminate when "x" is entered I need to keep track of how many inputs are entered I need to test the inputs to get the longest and shortest string and their length I need to find the sum of string lengths I need to find the average string length I need to find the number of strings entered excluding "x"
Upvotes: 0
Views: 979
Reputation: 1277
firstly let me highlight, you should really to try to implement it on your own first, because otherwise you will never understand properly to the logic / syntax,..
Without storing values can be done like follows:
I am just only not sure if average length will work properly, but looks like it should
(check the commented calculation in the code, it's not working this way, print at the end probably works)
public static void getStringsAndGetDetails() {
//https://stackoverflow.com/questions/54689213/i-would-like-to-find-out-how-to-find-the-shortest-and-longest-strings-from-a-lis
System.out.println("Put 'x' or 'X' for exit");
String input ="";
Scanner sc = new Scanner(System.in);
String shortestStr = null;
String longestStr = null;
int counter = 0;
//int averageLength = 0;
//load till x or X is entered
do{
//read
System.out.print("please, enter input: ");
input = sc.nextLine();
//avoid to process when escape is entered
if(!input.toLowerCase().equals("x")) {
counter++;
//get to temp variable, can be reused with avoid repeating reading
int inputLength = input.length();
//if input is shorter then its new shortest, null check for first input
if(shortestStr == null || inputLength < shortestStr.length()) {
shortestStr = input;
}
//similar with longest string
if(longestStr == null || inputLength > longestStr.length()) {
longestStr = input;
}
//!! calculate average length is NOT possible like that, see bellow in printing
/*
averageLength = (averageLength + inputLength) / counter;
System.out.println();
*/
}
}while(!input.toLowerCase().equals("x"));
//not mandatory, but best practice (closing the streams), scanner in this case
sc.close();
System.out.println("Entered " + counter + " values");
System.out.println("Longest: "+ longestStr);
System.out.println("Shortest: "+ shortestStr);
//be sure data are available (in case you enter x in first iteration, will fail otherwise)
if(shortestStr != null && longestStr != null) {
//seems to work
int averageLength= (shortestStr.length() + longestStr.length()) / 2;
System.out.println("Average length: "+ averageLength);
}
}
Output:
Put 'x' or 'X' for exit
please, enter input: kakaka
please, enter input: ka
please, enter input: kaka
please, enter input: x
Entered 3 values
Longest: kakaka
Shortest: ka
Average length: 4
Upvotes: 1
Reputation: 794
Have a look at the code bellow. It contains several good practices that you would hopefully implement in your programs. Notice how the tests are written in the main method? I have started with those, so I could think through the solution before writing the actual method. In the tests I try to capture as many edge cases as possible. If I find a bug in my code, I can then write a new unit test that will make it fail, and then fix the code. Disclaimer: This code requires JUnit 5 added to your classpath.
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class StringsInfo {
public static void main(String[] args) {
StringsInfo sInfo = new StringsInfo();
sInfo.test(new String[] {}, null, null, 0, 0.0D, 0);
sInfo.test(new String[] {null}, null, null, 0, 0.0D, 0);
sInfo.test(new String[] {"one"}, "one", "one", 1, 3.0D, 3);
sInfo.test(new String[] {"one", "two"}, "one", "one", 2, 3.0D, 6);
sInfo.test(new String[] {"one", "two", "three"}, "one", "three", 3, (11/3), 11);
sInfo.test(new String[] {"one", "two", "three", "x", "five"}, "one", "three", 3, (11/3), 11);
}
/**
* Helper to test the class
* @param input
* @param shortest
* @param longest
* @param count
* @param average
* @param sumLength
*/
private void test(String[] input, String shortest, String longest, long count,
double average, long sumLength) {
Result result = processString(input);
assertEquals(shortest, result.shortest);
assertEquals(longest, result.longest);
assertEquals(count, result.count);
assertTrue(average == result.average);
assertEquals(sumLength, result.sumLength);
}
private Result processString(String[] input) {
Result result = new Result();
int shortestLength = Integer.MAX_VALUE;
int longestLength = Integer.MIN_VALUE;
for (String s : input) {
if (s == null) { // Ignore null Strings
continue;
}
if ("x".equals(s)) { // found the end
break;
}
result.count++;
int wordLength = s.length();
result.sumLength += wordLength;
if (wordLength < shortestLength) {
result.shortest = s;
shortestLength = wordLength;
}
if (wordLength > longestLength) {
result.longest = s;
longestLength = wordLength;
}
}
if (result.count > 0) { // avoid division by zero
result.average = result.sumLength / result.count;
}
return result;
}
}
/**
* Helper class to store the result
*/
class Result {
String shortest;
String longest;
long count;
double average;
long sumLength;
}
Upvotes: 0