This program works when the int array does not need to count up to 10. My problem is that charat is reading 10 as two different chars, which they are. How am i able to make an exception for 10? for example, the program below, when you type in 5 people, the program prints out from 6-1, as the 1 is reading from the first char of 10. When you type in 6, charat is reading the 0, so it prints out 6-0.
package javaapplication2;
import java.util.*;
public class JavaApplication2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the amount of people in your group, up to 6");
int num = input.nextInt();
if (num > 6) {
System.out.println("You have exceeded the maximum amount of people allowed.");
}
int highest = num - 1;
String available = "";
String booking = " ";
int[] RowA = {0, 0, 1, 1, 1, 0, 0, 0, 0, 0};
for (int i = 0; i < RowA.length; i++) {
if (RowA[i] == 0) {
available = available + (i + 1);
}
if (available.length() > booking.length()) {
booking = available;
System.out.println(booking);
} else if (RowA[i] == 1) {
available = "";
}
}
if (num <= booking.length()) {
char low = booking.charAt(0);
char high = booking.charAt(highest);
System.out.println("There are seats from " + low + " - " + high + ".");
} else {
System.out.println("The desired seat amount is not available. The maximum amount on Row is " + booking.length());
}
}
}
So what you’re doing is a code smell called a Primitive Obsession. You’re using Strings where you should instead be using a more complex data type, such as a collection or array:
Using collections and arrays means that you don’t have to worry about
charAtand the number of decimal places because you’re actually manipulatingintandIntegertypes – which will keep track of their decimals on their own.