#Can someone help me with this strings method loop question?
1 messages · Page 1 of 1 (latest)
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
what did you already try
maybe share some code
and add details where you are stuck etc
basic improvements I can see already:
- delete the average variable and just calculate the average after the for loop
- I think you can just remove the if and only leave the
else ifpart
and I think you forgot to print number of characters for each city
[...] and the program will display how many characters are in that city name [...]
you can also remove the Enter the name of a city and nextLine from outside the for loop
and move the name variable into the for loop
as its not needed outside
And I would add this to the Extension 2:
Instead of just printing the length of the longest string, I would instead print something like this:
The longest city name is {} with {} characters.
and {} are just placeholders for variables
but for this you would also store the longest city name
show it :D
yeah but then the String also makes no sense, for example the highestString would be 17 then the output would look like this:
The longest city name is: 17
I think you should be able to figure that out quickly
but you figured out how to store the highestString length?
should I explain it to you?
if you look at you current code (a bit modified):
import java.util.Scanner;
public class LoopsStringsExercises1 {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
String name;
int average, sum = 0 , highestString = 0;
for (int i = 0; i<10; i++) {
System.out.println("Enter the name of a city: ");
name = input.nextLine();
sum+= name.length();
if (name.length()> highestString) { <-
highestString = name.length();
// this runs if the current name is longer so you can update the name inside of this if
} <-
}
average = sum/10;
System.out.println("average length: " + average);
System.out.println("The longest city name is: " + highestString);
}
}
You store the highestString and update it if there is a longer name, so the marked if block will only run on a longer name and inside this you can update a new variable which stores the longest string
thats you part
you would want to create a String variable which is outside the for loop
and update it if there is a longer string variable
after that print it at the end
neither
you use the already exisiting code
you just need a new variable which stores the longest string
hm you actually shouldnt, but I might confused you with some sentences
sorry
I would change the whole code to this
import java.util.Scanner;
public class LoopsStringsExercises1 {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
int sum = 0;
String longestCity = ""; // you can maybe set this to some placeholder or smth
for (int i = 0; i<10; i++) {
System.out.println("Enter the name of a city: ");
String name = input.nextLine();
sum += name.length();
if (name.length() > longestCity.length()) {
longestCity = name;
}
}
double average = sum/10.0; // important that you change this to double for correct output and the 10 to 10.0 to get correct calculation, check integer division for more about that
System.out.println("average length: " + average);
System.out.println("The longest city name is " + longestCity + " with " + longestCity.length() + " characters.");
}
}
yeah because it can be inside the for loop
as you dont use it outside of it
not really, but there is this concept where you should always create variables at the lowest level possible
why put it outside if its only used inside
its not that important
but its just a thing to get cleaner code
if you want to learn more about it (source: Effective Java, 3rd Edition)
not needed, how will sum change overtime in the application?
sum += name.length();
and String#length method always return an int
and int + int is always int so no need for double here
another thing to improve this would be to add a new constant variable to the application which stores the 10 and replaces the i<10 with i<variableName and the sum/10.0 with sum/variableName, this way its easy to change the number of inputs
yeah ofc
it was just a placeholder by me
I can look through again if you are finished
ok 