So, I am creating a text-based game where you fill in the blanks on the board with the letters, "S, M, A, R, T". To beat this game each row and column can only contain 1 out of the 5 given letters. (It's kind of like the game Sudoku) The game runs perfectly fine but when the user inputs the the row and column they want the letter to be in, it doesn't appear on the board. Could anyone please help me out? Thank you in advance. (Note: I am still new to java coding)
import java.util.Scanner;
public class SmartPuzzleProgram{
static Scanner keyboard = new Scanner(System.in);
static char[][] table = new char[5][5];
public static void main (String[] args){
Board(table);
int i = 0;
while (i < 5){
if( ((int)table[i][0] + (int)table[i][1] + (int)table[i][2] + (int)table[i][3] + (int)table[i][4]) != 391 ){
Move();
Board(table);
}
else
i++;
}
int j = 0;
while (i < 5){
if( ((int)table[0][j] + (int)table[1][j] + (int)table[2][j] + (int)table[3][j] + (int)table[4][j]) != 391 ){
Move();
Board(table);
}
else
j++;
}
System.out.println("Congratulations! You must be SMART.");
}
public static void Move(){
// Ask for user's input
System.out.print("Enter a row (1-5): ");
int r = keyboard.nextInt();
System.out.print("Enter a column (1-5): ");
int c = keyboard.nextInt();
System.out.print("Enter a letter (S,M,A,R or T): ");
char L = keyboard.next().toUpperCase().charAt(0);
if (L == 'S' || L == 'M' || L == 'A' || L == 'R' || L == 'T') {
table[r-1][c-1] = L;
}
else
// Error check
System.out.println("Invalid letter. Use 'S', M', 'A', 'R' or 'T'");
}
public static void Board(char[][] t){
// Given variables
t[0][0] = 'S';
t[0][1] = 'M';
t[0][2] = 'A';
t[0][3] = 'R';
t[0][4] = 'T';
t[1][0] = ' ';
t[1][1] = 'T';
t[1][2] = 'S';
t[1][3] = 'M';
t[1][4] = ' ';
t[2][0] = ' ';
t[2][1] = ' ';
t[2][2] = 'R';
t[2][3] = ' ';
t[2][4] = 'S';
t[3][0] = ' ';
t[3][1] = 'S';
t[3][2] = 'M';
t[3][3] = ' ';
t[3][4] = ' ';
t[4][0] = ' ';
t[4][1] = ' ';
t[4][2] = 'T';
t[4][3] = 'S';
t[4][4] = ' ';
// Prints out the board
System.out.println(" 1 2 3 4 5 ");
System.out.println(" ---+---+---+---+--- ");
System.out.println("1 | " + t[0][0] + " | " + t[0][1] + " | " + t[0][2] + " | " + t[0][3] + " | " + t[0][4] + " |");
System.out.println(" ---+---+---+---+--- ");
System.out.println("2 | " + t[1][0] + " | " + t[1][1] + " | " + t[1][2] + " | " + t[1][3] + " | " + t[1][4] + " |");
System.out.println(" ---+---+---+---+--- ");
System.out.println("3 | " + t[2][0] + " | " + t[2][1] + " | " + t[2][2] + " | " + t[2][3] + " | " + t[2][4] + " |");
System.out.println(" ---+---+---+---+--- ");
System.out.println("4 | " + t[3][0] + " | " + t[3][1] + " | " + t[3][2] + " | " + t[3][3] + " | " + t[3][4] + " |");
System.out.println(" ---+---+---+---+--- ");
System.out.println("5 | " + t[4][0] + " | " + t[4][1] + " | " + t[4][2] + " | " + t[4][3] + " | " + t[4][4] + " |");
System.out.println(" ---+---+---+---+--- ");
}
}
The problem is that you are assigning the values to table each time you call the Board() method, you may want to initialize them just one time. For this, create a new method, let's say initialize():
public static void initialize()
{
System.out.println("here");
table[0][0] = 'S';
table[0][1] = 'M';
table[0][2] = 'A';
table[0][3] = 'R';
table[0][4] = 'T';
table[1][0] = ' ';
table[1][1] = 'T';
table[1][2] = 'S';
table[1][3] = 'M';
table[1][4] = ' ';
table[2][0] = ' ';
table[2][1] = ' ';
table[2][2] = 'R';
table[2][3] = ' ';
table[2][4] = 'S';
table[3][0] = ' ';
table[3][1] = 'S';
table[3][2] = 'M';
table[3][3] = ' ';
table[3][4] = ' ';
table[4][0] = ' ';
table[4][1] = ' ';
table[4][2] = 'T';
table[4][3] = 'S';
table[4][4] = ' ';
}
And call it at the beginning of the main():
public static void main(String[] args)
{
initialize();
Board(table);
...
}
Related
I am currently up with Java and there is a question which I am not getting a correct approach to. It says to find out the minimum and maximum of two numbers, as well as the numbers which are equal, in if statements (nested ifs are allowed) and It's getting very complex and hard-to-understand. Can you suggest a better way to do it ?
Here's my code :
long num1 = 1 ;
long num2 = 1 ;
long num3 = 1 ;
boolean error = false ;
Scanner sc = new Scanner(System.in) ;
do {
if(error)
{
System.out.println("Sorry, error. Try again") ;
}
error = false ;
System.out.print("Enter the first number : ") ;
num1 = sc.nextLong() ;
System.out.print("Enter the second number : ") ;
num2 = sc.nextLong() ;
System.out.print("Enter the third number : ") ;
num3 = sc.nextLong() ;
if(num1<=0 || num2<=0) error = true ;
// number entered will iterate if error...
while(error == true) ;
// main conditions
if(num1>num2)
{
if(num1>num3)
{
System.out.println(num1 + " is the greatest number") ;
if(num2<num3)
{
System.out.println(num2 + " is the smallest") ;
} else if(num2==num3) {
System.out.println(num2 + " and " + num3 + " are equal") ;
}
}
} else if(num2>num3) {
if(num1!=num2)
{
System.out.println(num2 + " is the greatest number") ;
} else {
System.out.println(num2 + " and " + num1 + " are equal") ;
}
if(num1<num3)
{
System.out.println(num1 + " is the smallest number") ;
} else if(num1==num3) {
System.out.println(num1 + " and " + num3 + " are equal") ;
}
} else if(num3>num2){
if(num3!=num1)
{
System.out.println(num3 + " is the greatest") ;
} else {
System.out.println(num3 + " and " + num1 + " are equal") ;
}
}
}
I know this program does not cover all the possibilities, and sure enough, when i run it, it does not display all the correct results. Can anybody send me an organised program (with comments) please? It will be highly appreciated.
It is very simple, you can use the max and min functions of the Math module. It goes like this
long a, b, c
a = Math.max(num1, Math.max(num2, num3))
//a will be the maximum number
b = Math.min(num1,Math.min(num2, num3))
//b will be the minimum number
//c is the middle number
if(num1 < a & num1 > b)
c = num1
else if(num2 < a & num2 > b)
c = num2
else
c = num3
The way to this question is really easy (can't see why nobody saw it before) - just use the AND operator.
So for finding out the GREATEST number among the three :
if(num1>num2 && num1>num3)
{
System.out.println(num1 + " is the greatest") ;
} else if(num2>num3 && num2>num1) {
System.out.println(num2 + " is the greatest") ;
} else if(num3>num2 && num3>num1) {
System.out.println(num3 + " is the greatest") ;
}
For finding out the SMALLEST number :
if(num1<num2 && num1<num3) {
System.out.println(num1 + " is the smallest") ;
} else if(num2<num3 && num2<num1) {
System.out.println(num2 + " is the smallest") ;
} else if(num3<num2 && num3<num1) {
System.out.println(num3 + " is the smallest") ;
}
for finding out the EQUAL numbers :
if(num1==num2 && num2==num3) {
System.out.println("All the numbers are equal") ;
} else if(num2==num3) {
System.out.println(num2 + " is equal to " + num3) ;
} else if(num1==num3) {
System.out.println(num1 + " is equal to " + num3) ;
} else if(num1==num2) {
System.out.println(num1 + " is equal to " + num2) ;
}
Combining all of them :
public static void main()
{
long num1 = 1 ;
long num2 = 1 ;
long num3 = 1 ;
boolean error = false ;
Scanner sc = new Scanner(System.in) ;
do {
if(error)
{
System.out.println("Sorry, error. Try again") ;
}
error = false ;
System.out.print("Enter the first number : ") ;
num1 = sc.nextLong() ;
System.out.print("Enter the second number : ") ;
num2 = sc.nextLong() ;
System.out.print("Enter the third number : ") ;
num3 = sc.nextLong() ;
if(num1<=0 || num2<=0) error = true ;
// number entered will iterate if error...
} while(error == true) ;
if(num1>num2 && num1>num3)
{
System.out.println(num1 + " is the greatest") ;
} else if(num2>num3 && num2>num1)
{
System.out.println(num2 + " is the greatest") ;
} else if(num3>num2 && num3>num1)
{
System.out.println(num3 + " is the greatest") ;
}
if(num1<num2 && num1<num3)
{
System.out.println(num1 + " is the smallest") ;
} else if(num2<num3 && num2<num1)
{
System.out.println(num2 + " is the smallest") ;
} else if(num3<num2 && num3<num1)
{
System.out.println(num3 + " is the smallest") ;
}
System.out.println() ; // for clarity
if(num1==num2 && num2==num3)
{
System.out.println("All the numbers are equal") ;
} else if(num2==num3)
{
System.out.println(num2 + " is equal to " + num3) ;
} else if(num1==num3)
{
System.out.println(num1 + " is equal to " + num3) ;
} else if(num1==num2)
{
System.out.println(num1 + " is equal to " + num2) ;
}
}
}
[NOTE: the lines are not indented properly-so please overlook that.]
I am designing a dose game that I have encountered several problems:
I do not want the pre-selected house to be re-selected. How do I do this?
And when I use the cleanScreen method, the console does not clear, why?
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int count = 9;
String one = "1";
String two = "2";
String three = "3";
String four = "4";
String five = "5";
String six = "6";
String seven = "7";
String eight = "8";
String nine = "9";
for (int i = 0 ; i < count ; i++)
{
System.out.println(one + " | " + two + " | " + three);
System.out.println("---------");
System.out.println(four + " | " + five + " | " + six);
System.out.println("---------");
System.out.println(seven + " | " + eight + " | " + nine);
int num1 = sc.nextInt();
cleanScreen();
switch (num1)
{
case 1 -> one = "X";
case 2 -> two = "X";
case 3 -> three = "X";
case 4 -> four = "X";
case 5 -> five = "X";
case 6 -> six = "X";
case 7 -> seven = "X";
case 8 -> eight = "X";
case 9 -> nine = "X";
default -> System.out.println("Enter a number from one to nine");
}
System.out.println(one + " | " + two + " | " + three);
System.out.println("---------");
System.out.println(four + " | " + five + " | " + six);
System.out.println("---------");
System.out.println(seven + " | " + eight + " | " + nine);
if (one.equals("X") & two.equals("X") & three.equals("X"))
{
System.out.println("WIN X");
}
else if (four.equals("X") & five.equals("X") & six.equals("X"))
{
System.out.println("WIN X");
}
else if (seven.equals("X") & eight.equals("X") & nine.equals("X"))
{
System.out.println("WIN X");
}
else if (one.equals("X") & four.equals("X") & seven.equals("X"))
{
System.out.println("WIN X");
}
else if (two.equals("X") & five.equals("X") & eight.equals("X"))
{
System.out.println("WIN X");
}
else if (one.equals("X") & five.equals("X") & nine.equals("X"))
{
System.out.println("WIN X");
}
else if (three.equals("X") & five.equals("X") & seven.equals("X"))
{
System.out.println("WIN X");
}
Random Tas = new Random();
int num2 = Tas.nextInt(1,10);
System.out.println(num2);
cleanScreen();
switch (num2)
{
case 1 -> one = "O";
case 2 -> two = "O";
case 3 -> three = "O";
case 4 -> four = "O";
case 5 -> five = "O";
case 6 -> six = "O";
case 7 -> seven = "O";
case 8 -> eight = "O";
case 9 -> nine = "O";
default -> System.out.println("Enter a number from one to nine");
}
if (one.equals("O") & two.equals("O") & three.equals("O"))
{
System.out.println("WIN O");
}
else if (four.equals("O") & five.equals("O") & six.equals("O"))
{
System.out.println("WIN O");
}
else if (seven.equals("O") & eight.equals("O") & nine.equals("O"))
{
System.out.println("WIN O");
}
else if (one.equals("O") & four.equals("O") & seven.equals("O"))
{
System.out.println("WIN O");
}
else if (two.equals("O") & five.equals("O") & eight.equals("O"))
{
System.out.println("WIN O");
}
else if (one.equals("O") & five.equals("O") & nine.equals("O"))
{
System.out.println("WIN O");
}
else if (three.equals("O") & five.equals("O") & seven.equals("O"))
{
System.out.println("WIN O");
}
}
System.out.println("The game did not win");
}
public static void cleanScreen()
{
System.out.print("\033[H\033[2J");
System.out.flush();
}
I'm making a tic tac toe game for my homework and the teacher told me to print a tie when only a tie can come out. (under the assumption that the players are not smart) For example,
x o x
- - -
o x o
can only result in a tie so when this happens we are supposed to end the game with "this is a tie"
I have finished coding the tic tac toe with a char[][] array but I have no idea how to predict a draw.
public class Ttt22 {
private static int spacesLe
private static char[][] board;
public static void main (String[] args) {
// the first move belongs to X.
System.out.println("Welcome to Tic Tac Toe");
board = new char[3][3];
initializeBoard();
firstDraw();
char mark = 'X';
while (true) {
int square = getLegalMove(mark);
move(square, mark);
draw();
if (is3InRow(mark)) {
System.out.println(mark + " wins!");
break;
}
if (isBoardFull()) {
System.out.println("Tie game!");
break;
}
if (mark == 'X') {
mark = 'O';
}
else {
mark = 'X';
}
}
}
public static int getLegalMove (char mark) {
java.util.Scanner console = new java.util.Scanner(System.in);
while (true) {
System.out.println(mark + "'s next move: ");
int square = console.nextInt();
if ((square >= 1) &&
(square <= 9) &&
(isSquareEmpty(square))) {
return square;
}
System.out.println("\nIllegal move, try again\n");
}
}
public static void initializeBoard () {
spacesLeft = 9;
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
board[i][j] = ' ';
}
}
}
public static void firstDraw () {
System.out.println();
System.out.println(" | | ");
System.out.println(" " + 1 + " | " + 2 + " | " + 3);
System.out.println(" | | ");
System.out.println("---+---+---");
System.out.println(" | | ");
System.out.println(" " + 4 + " | " + 5 + " | " + 6);
System.out.println(" | | ");
System.out.println("---+---+---");
System.out.println(" | | ");
System.out.println(" " + 7 + " | " + 8 + " | " + 9);
System.out.println(" | | ");
System.out.println();
}
public static void draw () {
System.out.println();
System.out.println(" | | ");
System.out.println(" " + board[0][0] + " | "
+ board[0][1] + " | " + board[0][2]);
System.out.println(" | | ");
System.out.println("---+---+---");
System.out.println(" | | ");
System.out.println(" " + board[1][0] + " | "
+ board[1][1] + " | " + board[1][2]);
System.out.println(" | | ");
System.out.println("---+---+---");
System.out.println(" | | ");
System.out.println(" " + board[2][0] + " | "
+ board[2][1] + " | " + board[2][2]);
System.out.println(" | | ");
System.out.println();
}
public static void move (int square, char mark) {
if (isSquareEmpty(square)) {
spacesLeft = spacesLeft - 1;
}
int row = (square - 1) / 3;
int column = (square - 1) % 3;
board[row][column] = mark;
}
public static boolean isSquareEmpty (int square) {
int row = (square - 1) / 3;
int column = (square - 1) % 3;
return (board[row][column] == ' ');
}
public static boolean is3InRow (char mark) {
return
(board[0][0] == mark && board[0][1] == mark && board[0][2] == mark) ||
(board[1][0] == mark && board[1][1] == mark && board[1][2] == mark) ||
(board[2][0] == mark && board[2][1] == mark && board[2][2] == mark) ||
(board[0][0] == mark && board[1][0] == mark && board[2][0] == mark) ||
(board[0][1] == mark && board[1][1] == mark && board[2][1] == mark) ||
(board[0][2] == mark && board[1][2] == mark && board[2][2] == mark) ||
(board[0][0] == mark && board[1][1] == mark && board[2][2] == mark) ||
(board[0][2] == mark && board[1][1] == mark && board[2][0] == mark);
}
public static boolean isBoardFull () {
return spacesLeft == 0;
}
}
the expected result is when the game is
x o x
- - -
o x o
It prints "Tie game!" and the game ends
Hmm, simply, after every move of the "players", launch an automatic process that will compute all outcomes of the game starting from that particular position. If all of those end in a draw, then you can safely declare that the final outcome is a draw. Of course, this is the first, not at all optimized solution, that comes to my mind immediately. If you are interested, you can probably work on some optimizations based on that.
There are algorithms for this kind of task, I suggest you read about Minimax
I'm trying to populate a 2d list array using 2 user inputs.
Problem I'm having is that in the code below, the 1st for statement isn't producing the outcome I'm expecting, the 2nd for is doing what is needed. Also, with the code below I'm unable to close scanner.
public static void main(String[] args) {
ArrayList<String> listCon = new ArrayList<String>();
ArrayList<String> listCol = new ArrayList<String>();
Scanner txtInput = new Scanner(System.in);
char addTo = 'y';
do {
System.out.println("\nCurrent list is " + listCon + listCol + "\n");
System.out.println("Would you like to add a country to the list?\n\t"
+ "( y ) = YES\n\t( n ) = NO");
addTo = txtInput.next().toLowerCase().charAt(0);
if (addTo == 'y') {
System.out.println("Enter country name: ");
listCon.add(txtInput.next().toLowerCase());
System.out.println("Enter colour: ");
listCol.add(txtInput.next().toLowerCase());
} else if (addTo == 'n') {
int i = 1;
int countCon = listCon.size();
if(countCon == 0) {
System.out.println("No countries have been entered.");
} else {
String str = "country";
if(countCon > 1) {
str = "countries";
}
System.out.println("Thankyou for your input. We found " + countCon + " " +
str + " in the list.");
System.out.println("Listed " + str + ":\n");
for(String n : listCon) {
char[] conDigit = n.toCharArray();
conDigit[0] = Character.toUpperCase(conDigit[0]);
n = new String(conDigit);
for(String b : listCol) {
char[] colDigit = b.toCharArray();
colDigit[0] = Character.toUpperCase(colDigit[0]);
b = new String(colDigit);
System.out.println("Country " + i + " : " + n + " - \t" + b);
i = i + 1;
}
break;
}
break;
}
} else {
System.out.println("Incorrect input detected. please try again. \n");
}
} while (true);
}
}
You need to remove extra break from the first for loop to iterate. Otherwise, you break after first iteration.
for(String n : listCon) {
....
for(String b : listCol) {
...
}
break; //remove this!
}
break;
EDIT
The result im after is Country 1 : France - Blue Country 2 : UK -
White Country 3 : Ireland - Green
You need to iterate like this:
for (int i = 0; i < listCon.size() && i < listCol.size(); i++) {
String n = listCon.get(i);
char[] conDigit = n.toCharArray();
conDigit[0] = Character.toUpperCase(conDigit[0]);
n = new String(conDigit);
String b = listCol.get(i);
char[] colDigit = b.toCharArray();
colDigit[0] = Character.toUpperCase(colDigit[0]);
b = new String(colDigit);
System.out.println("Country " + i + " : " + n + " - \t" + b);
}
Here is the main problem:
java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at ExamAnalysis.main(ExamAnalysis.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:271)
The program compiles and runs. It's just that I am either getting the java.util.NoSuchElementException along with my five jother errors with (answer.charAt(i) == char) near the bottom. Here is my program:
import java.io.*;
import java.util.Scanner;
class ExamAnalysis
{
public static void main(String [] args) throws FileNotFoundException
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Please type the correct answers to the exam questions, one right after the other: ");
String answers = keyboard.nextLine();
System.out.println("Where is the file with all the student responses? ");
String responses = keyboard.nextLine();
Scanner read = new Scanner(new File(responses));
while (read.hasNextLine())
{
for (int i = 0; i <= 10; i++)
{
responses = read.nextLine();
int p = 1;
p += i;
System.out.println("Student " + p + " responses: " + responses.substring(0,10));
}
System.out.println("Thank you for the data on 9 students. Here's the analysis: ");
resultsByStudents(responses, answers);
analysis(responses);
}
}
public static void resultsByStudents(String responses, String answers)
{
System.out.println ("Student # Correct Incorrect Blank");
System.out.println ("~~~~~~~~~ ~~~~~~~ ~~~~~~~~~ ~~~~~");
int student = 0;
int correct = 0;
int incorrect = 0;
int blank = 0;
for (int i = 0; i <= 9; i++)
{
for (int j = 0; j <= responses.length(); j++)
{
if ((responses.charAt(j)) == answers.charAt(j))
correct++;
else if ((responses.charAt(j)) != answers.charAt(j))
incorrect++;
else
blank++;
}
System.out.println(student + " " + correct + " " + incorrect + " " + blank);
student++;
}
}
public static void analysis(String responses)
{
System.out.println("QUESTION ANALYSIS (* marks the correct response)");
System.out.println("~~~~~~~~~~~~~~~~~");
//stores the percentage of each choice chosen
double A = 0;
double B = 0;
double C = 0;
double D = 0;
double E = 0;
double X = 0;
// tallys every variable chosen per question
for (int i = 0; i <= 10; i++) // go through all the questions
{
for (int j = 0; j <= responses.charAt(i); j++) //go through all the student responses
{
// variable that are being tallied
int chooseA = 0;
int chooseB = 0;
int chooseC = 0;
int chooseD = 0;
int chooseE = 0;
int chooseBlank = 0;
//variables take percentage of choices that have been chosen from each student
A = chooseA/9;
B = chooseB/9;
C = chooseC/9;
D = chooseD/9;
E = chooseE/9;
X = chooseBlank/9;
// variables that will print the asterisk with certain character of correct answer
String a = "A";
String b = "B";
String c = "C";
String d = "D";
String e = "E";
String blank = "blank";
if (responses.charAt(j) == A)
chooseA++;
else if (responses.charAt(j) == B)
chooseB++;
else if (responses.charAt(j) == C)
chooseC++;
else if (responses.charAt(j) == D)
chooseD++;
else if (responses.charAt(j) == E)
chooseE++;
else
chooseBlank++;
System.out.println("Question #" + i);
if (answers.charAt(i) == 'A') a = "A*"; // answers cannot be resolved(I already made it a global variable in my main method.)
else if (answers.charAt(i) == 'B') b = "B*";// answers cannot be resolved
else if (answers.charAt(i) == 'C') c = "C*";// answers cannot be resolved
else if (answers.charAt(i) == 'D') d = "D*";// answers cannot be resolved
else if (answers.charAt(i) == 'E') e = "E*";// answers cannot be resolved
System.out.println(a + " " + b + " " + c + " " + d + " " + e + " " + blank);
System.out.println (chooseA + " " + chooseB + " " + chooseC + " " + chooseD + " " + chooseE + " " + chooseBlank );
System.out.println (A + " " + B + " " + C + " " + D + " " + E + " " + X);
}
}
}
}
while (read.hasNextLine())
{
for (int i = 0; i <= 10; i++)
{
responses = read.nextLine();
int p = 1;
p += i;
System.out.println("Student " + p + " responses: " + responses.substring(0,10));
}
System.out.println("Thank you for the data on 9 students. Here's the analysis: ");
resultsByStudents(responses, answers);
analysis(responses);
}
}
Your logic here is confusing you. read.nextLine(); "Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line."
So you are saying, does it have a line? If so, read the next 10...well...11 lines, which isn't what you want. You don't know if there are 11 lines past this point. Don't know what that text file looks like, but you will want to restructure this part to either say, "While it has a next line", or "Read 11 lines"
Remove the for loop may resolve the issue. You are checking only once by using while(hasNextLine() ) but calling read.nextLine() 10 times in for loop.
for (int i = 0; i <= 10; i++)
{
responses = read.nextLine();
.......
}
int i = 0;
int numberOfStudents = 9;
while (i < numberOfStudents && read.hasNextLine()){
responses = read.nextLine();
i++;
System.out.println("Student " + i + " responses: " + responses.substring(0,10));
}
System.out.println("Thank you for the data on "+ numberOfStudents +" students. Here's the analysis: ");
resultsByStudents(responses, answers);
analysis(responses);
i < numberOfStudents : makes the required number of inserts
read.hasNextLine() : checks if there is input from console. If not the program waits for input.
for (int i = 0; i <= 10; i++)
count from 0 -> 10 = 11 students