In: Computer Science
JAVA
Write a test program that prompts the user to enter a series of integers and displays whether the series contains runLength consecutive same-valued elements. Your program’s main() must prompt the user to enter the input size - i.e., the number of values in the series, the number of consecutive same-valued elements to match, and the sequence of integer values to check. The return value indicates whether at least one run of runLength elements exists in values. Implement the test program as part of the NConsecutive class (the main() method is already in place at the end of the class).
I am looking for a fix of my current code for this question. My code executes the expected output, but it messes up with other outputs. For example, if I input this data:
Enter The Number of Values: 5
How many consecutive, same-valued elements should I look for?:
2
Enter 5 Numbers: 1 1 2 2 1 1
The list has no sequence of 2 consecutive same-valued
elements
The output I should get is "The list has at least 1 sequence of 2 consecutive same-valued elements"
EXPECTED OUTPUT:
Enter the number of values: 3
How many consecutive, same-valued elements should I look for?
3
Enter 3 numbers: 5 5 5
The list has at least one sequence of 3 consecutive same-valued
elements
Enter the number of values: 3
How many consecutive, same-valued elements should I look for?
4
Enter 3 numbers: 5 5 5
The list has no sequences of 4 consecutive same-valued elements
CURRENT CODE:
public static boolean hasNConsecutive( int[] values, int
runLength ) {
int count = 0;
for (int i = 0; i <
(values.length-1);i++)
{
if (values[i] ==
values[i + 1]) {
count++;
if(count == runLength)
break;
}
else {
count = 0;
}
}
if (count >= (runLength - 1))
return true;
else
return false ;
} // end hasNConsecutive()
public static void main( String[] args ) {
int x, runLength;
Scanner input = new
Scanner(System.in);
System.out.printf("Enter The Number
of Values: ");
x = input.nextInt();
int values[] = new int[x];
System.out.printf("How many
consecutive, same-valued elements should I look for?: ");
runLength = input.nextInt();
System.out.printf("Enter "+ x + "
Numbers: ");
for(int i = 0; i < x; i++)
{
values[i] =
input.nextInt();
}
input.close();
if(hasNConsecutive(values,runLength))
System.out.println("The list has at least one sequence of " +
runLength + " consecutive same-valued elements");
else
System.out.println("The list has no sequence of " + runLength + "
consecutive same-valued elements");
}
}
If you have any doubts, please give me comment...
import java.util.Scanner;
public class NConsecutive {
public static boolean hasNConsecutive(int[] values, int runLength) {
int count = 0;
for (int i = 0; i < (values.length - 1); i++) {
if (values[i] == values[i + 1]) {
count++;
if (count == runLength-1)
break;
} else {
count = 0;
}
}
if (count >= (runLength - 1))
return true;
else
return false;
} // end hasNConsecutive()
public static void main(String[] args) {
int x, runLength;
Scanner input = new Scanner(System.in);
System.out.printf("Enter The Number of Values: ");
x = input.nextInt();
int values[] = new int[x];
System.out.printf("How many consecutive, same-valued elements should I look for?: ");
runLength = input.nextInt();
System.out.printf("Enter " + x + " Numbers: ");
for (int i = 0; i < x; i++) {
values[i] = input.nextInt();
}
input.close();
if (hasNConsecutive(values, runLength))
System.out.println(
"The list has at least one sequence of " + runLength + " consecutive same-valued elements");
else
System.out.println("The list has no sequence of " + runLength + " consecutive same-valued elements");
}
}