I am getting this error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at JavaProject.main(JavaProject.java:70)
Here is the code:
try
{
PrintWriter writer = new PrintWriter("Gamer Report Data.txt");
writer.println("Player: " + gamerName);
writer.println();
writer.println("-------------------------------");
String[] report = gamerReport.split(gamerReport, ':');
writer.println("Game:" + ", score=" + report[1] + ", minutes played=" + report[2] + report[3]);
writer.close();
} catch (IOException e)
{
System.err.println("File does not exist!");
}
I believed it to be something related to my for loop, but I have had no luck changing it around.
import java.util.Scanner;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.io.PrintWriter;
public class JavaProject {
private static char[] input;
#SuppressWarnings("null")
public static void main(String[] args) {
for (int b = 1; b < 100; b++) {
// this is making the code loop 100 times
int hrs, mins;
int[] gameCount;
int[] minutesPlayed = new int[100];
String gamerName, gamerReport;
// Main data storage arrays
String[] gameNames = new String[100];
int[] highScores = new int[100];
Scanner Scan = new Scanner(System.in);
// formatting for output and input
System.out.println("////// Game Score Report Generator \\\\\\\\\\\\");
System.out.println(" ");
// user enters name and then moves to next line
System.out.println("Enter Your Name");
gamerName = Scan.nextLine();
// user is given an example of input format
System.out.println("Input Gamer Information " + "Using Format --> Game : Achievement Score : Minutes Played");
System.out.println(" ");
System.out.println("Game : Achievement Score : Minutes Played");
gamerReport = Scan.nextLine();
String[] splitUpReport; // an array of string
splitUpReport = gamerReport.split(":"); // split the text up on the colon
int i = 0;
// copy data from split text into main data storage arrays
gameNames[i] = splitUpReport[0];
highScores[i] = Integer.parseInt(splitUpReport[1].trim());
minutesPlayed[i] = Integer.parseInt(splitUpReport[2].trim());
// output to file
try
{
PrintWriter writer = new PrintWriter("Gamer Report Data.txt");
writer.println("Player: " + gamerName);
writer.println();
writer.println("-------------------------------");
String[] report = gamerReport.split(gamerReport, ':');
writer.println("Game:" + ", score=" + report[1] + ", minutes played=" + report[2] + report[3]);
writer.close();
} catch (IOException e)
{
System.err.println("File does not exist!");
}
}
}
public static char[] getInput() {
return input;
}
public static void setInput(char[] input) {
JavaProject.input = input;
}
}
There are two problems with your code:
1)
String[] report = gamerReport.split(gamerReport, ':');
should be
String[] report = gamerReport.split(":");
as you did with splitUpReport (not sure why you're splitting again actually).
2) Arrays are zero-indexed, so the print statement should look like this:
writer.println("Game:" + ", score=" +report[0] +", minutes played="+ report[1] + report[2]);
The error in your code is being caused by this code in the try block:
String[] report = gamerReport.split(gamerReport, ':');
You are actually trying to split the gamerReport string using itself as a regex, with a limit of 58, which is the numerical value of the colon.
The result of this split is 2 empty String elements, which correspond to the match happening before and after the regex, which is the string itself.
The ArrayIndexOutOfBounds exception is happening when you try to access the third element from this array:
To fix your problem, just define the report array as follows:
String[] report = gamerReport.split(':');
As #shmosel pointed out, you might also want to change your array indices here as well:
writer.println("Game:" + ", score=" + report[0] +", minutes played="+ report[1] + report[2]);
Related
I'm using an arraylist to append inputs and send the arraylist elements to file. However, everytime I exit the program and run it again, the contents in the written in the file becomes empty.
ArrayList<String> memory = new ArrayList<String>();
public void fileHandling() {
try {
FileWriter fWriter = new FileWriter("notes.data");
for (int x = 0; x <= memory.size() - 1; x++) {
fWriter.write(memory.get(x) + '\n');
}
fWriter.close();
} catch (IOException e) {
System.out.println(e);
}
}
public void createNote() {
Scanner insertNote = new Scanner(System.in);
LocalDate todayDate = LocalDate.now();
LocalTime nowTime = LocalTime.now();
String timeFormat = nowTime.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM));
String dateTime = todayDate.toString() + " at " + timeFormat;
while (true) {
System.out.println();
System.out.println("Enter a note");
System.out.print("> ");
String note = insertNote.nextLine();
if (note == null) {
System.out.println("Invalid input! Try again");
break;
} else {
memory.add(note + " /" + dateTime);
fileHandling();
System.out.println("Note is saved!\n");
break;
}
}
I expect the program to save the contents of every input. Then if I exit and run the program again, the contents will go back to the array
Your code currently does the following:
You enter something (X) for the first time:
It gets added to the ArrayList
The ArrayList gets written into the file
Your file now contains: X
You enter something second (Y):
It gets added to the ArrayList (Which now contains: X, Y)
The ArrayList gets written into the file
Your file now contains: X + newline + Y
Your Problem is, that everytime you create a new FileWrite it overwrites your file.
This can be avoided by using the constructor like this:
FileWriter writer = new FileWriter("notes.data", true);
This sets it into the append mode and therefore keeps previous data in the file
You don't need to create a separate Scanner, in method createNote(), in order to get a "note" from the user.
It is usually better to write your code using the interface rather than the specific implementation because then you usually need to change less code if you decide to change the implementation. Hence the type for member variable memory should probably be List rather than ArrayList.
Note that ArrayList may waste memory if the list of "note"s is large. I suggest using LinkedList instead. Alternatively, use an array (rather than a List) and handle expanding the array when adding a "note" as well as reducing the array when removing a "note".
Having an infinite loop, i.e. while (true), which contains a single if-else where both the if block and the else block contain break statements, means that the loop will perform exactly one iteration. May as well remove the while loop – which means also removing the break statements.
Rather than writing the code that generates a timestamp repeatedly, you should adopt the DRY principle and extract that code into a separate method.
The file name should be a constant so as to minimize the amount of code changes you will need to do if you decide to change the file name.
By convention, text files have a filename extension of .txt whereas binary files have the .data extension.
Although you don't need to, I personally prefer to initialize class member variables in the constructor.
The below code is a SSCCE, hence I added a main method. More notes appear after the code.
package Methods;
import java.util.*;
import java.time.format.*;
import java.time.*;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
public class FileSys {
private static final String FILENAME = "notes.txt";
private static final String CREATE = "C";
private static final String DELETE = "D";
private static final String FIND = "F";
private static final String QUIT = "Q";
private static final String SHOW = "S";
private static final String UPDATE = "U";
Scanner reader;
List<String> memory;
public FileSys() throws IOException {
reader = new Scanner(System.in);
memory = new LinkedList<String>();
loadFile();
}
public void fileHandling() {
Path path = Paths.get(FILENAME);
try (BufferedWriter bw = Files.newBufferedWriter(path,
StandardOpenOption.CREATE,
StandardOpenOption.WRITE);
PrintWriter pw = new PrintWriter(bw)) {
for (String write : memory) {
pw.println(write);
}
}
catch (IOException e) {
e.printStackTrace();
}
}
public void createNote() {
String dateTime = getTimestamp();
System.out.println();
System.out.println("Enter a note");
System.out.print("> ");
String note = reader.nextLine();
memory.add(note + " / " + dateTime);
fileHandling();
System.out.println("Note is saved!");
}
public void searchNote() {
System.out.print("\nEnter note number: ");
try {
int search = reader.nextInt();
reader.nextLine();
System.out.println("\nSearch result:");
int index = memory.indexOf(memory.get(search - 1));
if (index != -1) {
System.out.println("[" + (index + 1) + "]" + " " + memory.get(search - 1));
}
else {
System.out.println("Note number-" + search + " is not found in the collection!");
}
}
catch (IndexOutOfBoundsException e) {
System.out.println("The note number you have entered is invalid!");
}
}
public void updateNote() {
String dateTime = getTimestamp(); // ZonedDateTime.now(ZoneId.systemDefault()).format(dateTimeObj);
System.out.print("\nEnter note number to change: ");
try {
int search = reader.nextInt();
int index = memory.indexOf(memory.get(search - 1));
String updateLine;
if (index != -1) {
System.out.println("\nCurrent note: ");
System.out.println("[" + (index + 1) + "]" + " " + memory.get(search - 1));
System.out.println("\nThe updated note will be: ");
System.out.print("> ");
reader.nextLine();
updateLine = reader.nextLine();
memory.set(index, updateLine + " /" + dateTime);
System.out.print("Note has been updated successfully!\n");
}
else {
System.out.println(search + " is not found in the collection!");
}
}
catch (IndexOutOfBoundsException e) {
System.out.println("The note number you have entered is invalid!");
}
fileHandling();
}
public void deleteNote() {
System.out.print("\nEnter note number to delete: ");
try {
int search = reader.nextInt();
reader.nextLine();
int index = memory.indexOf(memory.get(search - 1));
System.out.println();
if (index != -1) {
System.out.println("[" + (index + 1) + "]" + " " + memory.get(search - 1));
System.out.print("\nDo you want to delete this note? \n[y] or [n]: ");
char delDecision = reader.nextLine().charAt(0);
if (delDecision == 'y' || delDecision == 'Y') {
memory.remove(index);
System.out.println("Note has been deleted successfully!");
System.out.println();
}
else if (delDecision == 'n' || delDecision == 'N') {
System.out.println("Note was not deleted!");
}
else {
System.out.println("Invalid input!");
}
}
else {
System.out.println(search + " is not found in the collection!");
}
}
catch (IndexOutOfBoundsException e) {
System.out.println("The note number you have entered is invalid!");
}
fileHandling();
}
public void displayNote() {
if (memory.size() > 0) {
int counter = 0;
for (String note : memory) {
System.out.printf("%d. %s%n", ++counter, note);
}
}
else {
System.out.println("There are no notes.");
}
}
private String getTimestamp() {
LocalDate todayDate = LocalDate.now();
LocalTime nowTime = LocalTime.now();
String timeFormat = nowTime.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM));
String dateTime = todayDate.toString() + " at " + timeFormat;// ZonedDateTime.now(ZoneId.systemDefault()).format(dateTimeObj);
return dateTime;
}
private void loadFile() throws IOException {
Path path = Paths.get(FILENAME);
if (Files.isRegularFile(path)) {
memory.addAll(Files.readAllLines(path, Charset.defaultCharset()));
}
}
private void showMenu() {
String choice = "";
while (!QUIT.equalsIgnoreCase(choice)) {
System.out.println(CREATE + " - Create note");
System.out.println(DELETE + " - Delete note");
System.out.println(FIND + " - Search notes");
System.out.println(SHOW + " - Show notes");
System.out.println(UPDATE + " - Update note");
System.out.println(QUIT + " - Quit");
System.out.println();
System.out.print("Your choice: ");
choice = reader.nextLine();
if (!choice.isEmpty()) {
choice = choice.substring(0, 1);
choice = choice.toUpperCase();
switch (choice) {
case CREATE -> createNote();
case DELETE -> deleteNote();
case FIND -> searchNote();
case SHOW -> displayNote();
case UPDATE -> updateNote();
case QUIT -> System.out.println("Good bye.");
default -> System.out.println("Invalid: " + choice);
}
}
else {
System.out.println("No selection entered. Retry.");
}
}
}
public static void main(String[] args) {
try {
FileSys fs = new FileSys();
fs.showMenu();
}
catch (IOException xIo) {
xIo.printStackTrace();
}
}
}
Your code does not initially load memory with contents of file notes.txt so I added that in the constructor. Consequently you don't need to append to the file since you simply overwrite it with contents of memory.
The file handling is done using NIO.2 including try-with-resources – which was added in Java 7. There are more NIO.2 examples in the JDK documentation.
Whenever the code throws an unexpected exception, it is nearly always a good idea to print the stack trace.
How do I get data from a text file and save it into a string?
For example, my text file has the numbers 1, 4, 5, 6, 8, and 10.4. These numbers can be on the same line or on separate lines. I want to concatenate them into a string, like so: 1 4 5 6 8 10.4
import java.io.File;
import java.io.FileNotFoundException;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Scanner;
public class f {
public static void main(String args[]) {
int count = 0;
double totalcount = 0;
double average = 0;
Scanner read = new Scanner(System.in);
Scanner file;
String input = "";
String test = "";
double[] array1 = new double[100];
while (true) {
System.out.println("Enter name of file or enter quit to exit");
input = read.next();
if (input.equalsIgnoreCase("quit")) {
break;
}
try {
file = new Scanner(new File(input));
if (!file.hasNextLine()) {
System.out.println(input + " file is empty");
}
while (file.hasNext()) {
totalcount = totalcount + file.nextDouble();
count++;
}
while (file.hasNext()) {
test = test + (" ") + file.next();
}
System.out.println("bla" + test);
average = totalcount / count;
DecimalFormat df = new DecimalFormat("#.###");
df.setRoundingMode(RoundingMode.CEILING);
System.out.println("\nCount: " + count);
System.out.println("Total: " + df.format(totalcount));
System.out.println("Average: " + df.format(average));
System.out.println();
} catch (FileNotFoundException e) {
System.out.println(input + " doesn't exist");
}
}
}
}
My code does not work correctly.
Your code is working fine, the problem is, you trying to access content of your file two times.after first while loop , the hasnext() method will return false.because you already accessed all the element in first while loop.
so it will not execute -
while (file.hasNext()) {
test = test + (" ") + file.next();
}
Other than that your code is fine.
if you want store it in string also then do small modification in your first while loop as below-
while (file.hasNext()) {
Double d=file.nextDouble();
test = test + (" ")+d;
totalcount = totalcount + d;
count++;
}
I think this will give you what you want.
Hello i think below code will be useful for you ,as per your question i have txt file with data , first i am getting the location of file & then i am trying to get the content , at last i am printing it to the console
File f = new File("D:\\temp.txt");
String content11 = FileUtils.readFileToString(f);
System.out.println(content11);
package statescapitalquizz;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
*
* #author Steve
*/
public class Statescapitalquizz {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
String fileName = "Capitals.txt";
boolean found = false;
Scanner kb = new Scanner(System.in);
String target;
int n = 50;
String[] states = new String[n];
String[] capitals = new String[n];
try (Scanner inputStream = new Scanner(new FileInputStream(fileName))) {
for (int i = 0; i < n; i++) {
states[i] = inputStream.nextLine();
capitals[i] = inputStream.nextLine();
}
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
System.out.println("Please enter the name of a state: ");
target = kb.nextLine();
// the loop coninues to the end of the array if the city is not found
for (n=0; (!found) && (n < states.length) ; n++) {
if (states[n].matches(target)) {
//print found message and set found to true
System.out.println("The capital of " + target + " is"); //Where I need help
found = true;
} // end if
} // for loop
// after the loop – if not(found) print not found message
if (!found)
System.out.println(target + "is not a state in the United States");
}
}
Okay so the part I need help is how to retrieve the matching 'capital' to the 'state' that was entered by the user. My output statement is basically
//print found message and set found to true
System.out.println("The capital of " + target + " is" + ); //Where I need help
found = true;
} // end if
} // for loop
And I don't know what code to put in the output statement so it cross references my second array and puts the matching Capital from the State the user entered.
System.out.println("The capital of " + target + " is " + capitals[n]);
As mentioned in the comments to my question, is the answer.
I am trying to get the user to enter a four digit pin but I don't want it to be printed out on the screen as they type it. I have tried using System.console().readPassword(); like so:
import java.util.Scanner;
import java.io.*;
import java.util.InputMismatchException;
public class VirtualATM{
private static String Details;
public static void main(String [] args){
try{
//Create variables & scanner to be used throughout the program.
Scanner sc = new Scanner(System.in);
boolean RegisterOrExist = false;
int cardNo = 0;
String DirToWriteFile = System.getProperty("user.dir") + "/VirtualATM.txt"; //Get path to write text file to.
DirToWriteFile.trim();
File file = new File(DirToWriteFile);
// if file doesn't exists, then create it
if (!file.exists()) {
file.createNewFile();
}
//Create writer to write to files.
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
System.out.println("Enter card number, or type register to register a new card.");
String register = sc.nextLine();
if(register.equalsIgnoreCase("register")){
RegisterOrExist = false;
RegisterNewCard();
} else {
RegisterOrExist = true;
cardNo = Integer.valueOf(register);
}
bw.write(Details);
//Close the writer.
bw.close();
} catch(IOException e) {
e.printStackTrace();
}
}
/*** Method for registering a new card **/
public static void RegisterNewCard(){
Scanner sc = new Scanner(System.in);
System.out.print("Name: ");
String name = sc.nextLine();
int MaxVal = Integer.MAX_VALUE;
int CardNo = new java.util.Random().nextInt(MaxVal) + 1;
int balance = new java.util.Random().nextInt(10000) + 1;
boolean OverDraft = false;
int OverDraftLimit = 0;
if(OverDraft = true){
OverDraftLimit = 250;
}
char [] PIN = {};
System.out.println("Create a FOUR digit pin: ");
try{
PIN = System.console().readPassword();
}catch(InputMismatchException e){
e.printStackTrace();
}
int P1 = (int) PIN[0];
int P2 = (int) PIN[1];
int P3 = (int) PIN[2];
int P4 = (int) PIN[3];
int [] PinNo = {P1, P2, P3, P4};
Details = "Name: " + name + " CardNo: " + CardNo + " Current Balance: " + balance + " overdraft? " + OverDraft + " OverDraftLimit: " + OverDraftLimit + " pin: " + PinNo;
}
}
I then try to write the pin int [] pinNo = {P1, P2, P3, P4} to a text file using the BufferedWriter. I get the following this text in the text file when I input the pin as 2566:
[I#42a57993
Is there another way to read a password without it printing on the screen as the user types?
well this line
bw.write(Details);
is writing the details in the file where
Details = "..otherdetails..."+"pin: " + PinNo;
shows that you are writing the hash of the array. So simply replace "pin: " + PinNo with
the actual elements of the array
You can simply follow what Rod said or try this
Details = "..otherdetails..."+"pin: " + java.util.Arrays.toString(PinNo)
Tip - using this makes you password more secure
try like this
char[] PIN = System.console.readPassword();
Arrays.fill(password, ' ');
problem:
pin: " + PinNo;
You are actually printing the memory location of the array object not the array elements itself
solution:
you can get each index of the PinNo array and append each of them to the string
"pin: " + PinNo[0] + PinNo[1] + PinNo[2] + PinNo[3]
If you are trying to run your code on Eclipse IDE console then it'll not work. It is bug System.console() (Java 6.0) returns null when running inside Eclipse.
You need to run from command prompt or teminal, etc.
For more on this see these post :-
Hide input on command line - stackoverflow
Masking password input from the console - stackoverflow
I want to use the MaxFriends method to find the person with the most friends. Printing the number of friends from the linked list is easy enough but since I clear it after each iteration of the while loop I don't know how to compare the values at the end...
I think the problem could be simplified if I just found the line with the most 'tokens' or in this case strings. Is there a way to do this?
I'm reading in a text file (to create a linked list).
Text file looks like this:
john, peter, maria, dan, george, sonja
maria, nell, ted, don, matthew, ann, john, george
fred, steve
ann, tom, maria
Code thus far:
import java.util.*;
import java.io.*;
import javax.swing.JFileChooser;
public class Test {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<String>();
LinkData ld1 = new LinkData();
JFileChooser chooser = new JFileChooser(".");
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: ");
// open and read file:
Scanner scanner = null;
try {
scanner = new Scanner(chooser.getSelectedFile());
} catch (IOException e) {
System.err.println(e);
error();
}
if (scanner == null)
error();
while (scanner.hasNextLine()) {
int friendCount = 0;
String line = scanner.nextLine();
Scanner lineScan = new Scanner(line);
lineScan.useDelimiter(", ");
// System.err.println("The line that was scanned: " + line);
String leader = lineScan.next(); {
while (lineScan.hasNext()) {
list.add(lineScan.next());
friendCount++;
}
System.out.println("Friend Leader: " + leader + "\n" +
"\tFriends include: " + list.toString() + "\n" +
"\tNumber of Friends: " + list.size() + "\n");
} list.clear();
}
}
}
private static void error() {
System.err.println("An error has occurred: bad data");
System.exit(0);
}
public void maxFriends() {
}
}
If I understand the problem correctly, you just need to keep track of who has the most friends so far, and compare that to the next candidate for each line. Stuffing everything into a map or heap seems unnecessary.
By the way, the parsing you're doing is very simple and doesn't need a scanner:
String[] friends = line.split(",\\s*");
System.out.printf("%s has %d friends\n", friends[0], friends.length - 1);
I changed portion of your code into somewhat like below:
int maxFriendCount = 0; // added by me
String maxLeader = null; // added by me
while (scanner.hasNextLine()) {
int friendCount = 0;
String line = scanner.nextLine();
Scanner lineScan = new Scanner(line);
lineScan.useDelimiter(", ");
// System.err.println("The line that was scanned: " + line);
String leader = lineScan.next();
while (lineScan.hasNext()) {
list.add(lineScan.next());
friendCount++;
}
// Added by me
if(friendCount > maxFriendCount)
{
maxFriendCount = friendCount;
maxLeader = leader;
}
System.out.println("Friend Leader: " + leader + "\n" +
"\tFriends include: " + list.toString() + "\n" +
"\tNumber of Friends: " + list.size() + "\n");
list.clear();
}
After while loop terminates, you can get the leader with the most friends.
Why not use a Hashmap for storing the information on a per friend basis
Map<String, List<String>> friends = new HashMap<String, List<String>>();
After each iteration use the friends name as the key in the hashmap and then add the linked list to the map as the value.
Then in maxFriends you will be able to go through the keys and get the values and verify which list had the greatest size and thus the most friends.