2 Answers
New AnswerYou could store your strings as an array: String[] strings = new String[4]; Scanner sc = new Scanner(/* ... */); int index = 0; while (sc.hasNext()) { if (index >= strings.length) break; strings[index] = sc.next(); index++; } // beware: // if this loop terminates because hasNext is false, the remaining strings will be null // Access strings as you normally would access an array String firstString = strings[0]; // ... and such