Could you please explain each line of code. I'm having
a hard time understanding this part. This is a String
calculator.
What is minIndex supposed to mean? Is putting
MAX_VALUE really necessary? Please explain each code. I really need
to understand this. Thanks
boolean done = false;
while (!done)
{
int minIndex = Integer.MAX_VALUE;
String minIndexDelimiter = null;
for (String s : delimiters)
{
if (numbers.contains(s))
{
int index = numbers.indexOf(s);
if (index < minIndex)
{
minIndex = index; minIndexDelimiter = s;
}
}
}
int x;
if (minIndex != Integer.MAX_VALUE)
{
x = Integer.parseInt(numbers.substring(0, minIndex));
numbers = numbers.substring(minIndex +
minIndexDelimiter.length());
}
else
{
x = Integer.parseInt(numbers);
done = true;
}
if (x < 0)
{
throw new IllegalArgumentException("Negatives not allowed: " +
x);
}
if (x <= 1000)
{
sum += x;
}
}
return sum;
}
}
boolean done = false; //initialise a true/false variable done to
help in looping
while (!done) //start an iteration process provided done=true
{
int minIndex = Integer.MAX_VALUE; //initialise the minimum value of
the counter to the maximum possible value for integer. Yes it is
necessary. This is usually done to ensure all values of a list are
considered. usually the max counter is set to min, and min counter
set to max
String minIndexDelimiter = null;
for (String s : delimiters) //range based for loop to looping over
all contents of delimiters
{
if (numbers.contains(s)) // check in the list "numbers" whether
there is is a delimiter represented by s
{
int index = numbers.indexOf(s); //in the numbers, get the position
of a delimiter represented by s
if (index < minIndex) // check whether the position of the
delimiter (index) is in the correct range of integers
{
minIndex = index; minIndexDelimiter = s; //update the counter
needed to traverse the string (numbers) with the current position
(index) and also update the minimum position of delimiter with
position of current delimiter (s)
}
}
}
int x; //declare x as integer
if (minIndex != Integer.MAX_VALUE) // check whether the counter has
been updated with the current index and also ensure it's still in
the range of integers
{
x = Integer.parseInt(numbers.substring(0, minIndex)); // get the
part of the numbers list starting from the beginning to the current
index then convert it to integer and store in x
numbers = numbers.substring(minIndex + minIndexDelimiter.length());
//update the list "numbers" with part of the string starting from
the current position combined with the length of the delimiters to
the end of list "numbers"
}
else //in case minIndex == Integer.MAX_VALUE, all contents of
numbers have been considered
{
x = Integer.parseInt(numbers); // convert all contents of list
"numbers" and store in x
done = true; //stop the while loop, and go to return the sum
}
if (x < 0)//ensure negative numbers are not allowed in the
calculation
{
throw new IllegalArgumentException("Negatives not allowed: " + x);
//inform the user with error in case of negatives
}
if (x <= 1000) // check whether x is not greater than 1000, so x
should be 0-1000 inclusive
{
sum += x; // update the sum by adding x to the previous sum which
was supposed to be set initialised to 0 (sum =0) before the while
loop started
}
}
return sum; // the current function should carry the sum to the
calling function
}
}
PLEASE PLEASE GIVE A THUMBS UP
COMMENT DOWN FOR ANY QUERY RELATED TO THIS ANSWER
Could you please explain each line of code. I'm having a hard time understanding this part....