Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 이렇게 오류가 나는게 왜 나는지 잘 모르겠습니다.

gud0415의 이미지

package Baseball;
import java.util.Scanner;
import java.io.*;

public class Pl_Modify
{
public static void clearScreen()
{
for (int i = 0; i < 80; i++)
System.out.println(" ");
}

private String[] arr;

public void pl_modify()
{
Scanner scan = new Scanner(System.in);
LoginCtr logctr = new LoginCtr();
PlayerList playerlist = new PlayerList();
int choice;
String result = "";
String group = "";
String name = "";
playerlist.playerlist();
System.out.println(" ");
System.out.print("수정할 선수를 입력하세요 : ");
name = scan.nextLine();
System.out.println("");
System.out.println("1. 구단");
System.out.println("2. 포지션");
System.out.println("3. 비밀번호");
System.out.print("수정할 것을 고르세요 : ");
choice = scan.nextInt();

if(choice == 1)
{
try
{
BufferedReader br = new BufferedReader(new FileReader(new File("D:\\\\Java Program\\\\Baseball\\\\src\\\\Baseball\\\\player.txt")));
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("D:\\\\Java Program\\\\Baseball\\\\src\\\\Baseball\\\\player.txt"), true));
PrintWriter pw = new PrintWriter(bw, true);
String row = null;
System.out.print("수정할 구단을 입력하세요 : ");
group = scan.nextLine();
while((row = br.readLine()) != null)
{
String[] arr = row.split(",");
result = arr[1].replaceFirst(arr[1], group);
if(name.equals(arr[0]))
{
pw.write(arr[0] + "," + result + "," + arr[2] + "," + arr[3] + "," + arr[4] + " ");
pw.println(" ");
pw.flush();
System.out.println("수정되었습니다.");
logctr.log_ctr();
}
}
bw.close();
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

이런식으로 입력받은 구단을 수정하려고 하는데 이름입력과 구단입력에서
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
이러한 오류가 납니다.

왜 이렇게 나오는 것일까요 ,...ㅜㅜ

그리고 가능하다면 수정한것을 텍스트에 덮어쓰고싶은데 다음줄로 저장이 됩니다. 어떻게 하는지 알려주세요ㅜㅜ

힌트의 이미지

힌트1: ArrayIndexOutOfBoundsException를 잘 끊어 읽어 보세요. 힌트를 더 드리자면, 대문자 앞에서 끊으면 됩니다.

힌트2: 주어진 프로그램에서 "Array"는 단 하나뿐이네요. 무엇일까요?

힌트3: 그 길이는 어떻게 정해질까요?

힌트4: 결국 가능성은 세 가지죠. 입력 파일이 잘못됐거나, 귀하의 입력 파일에 대한 가정이 잘못됐거나, 둘 다거나. 셋 중 어느 것이 참인지는 질문글 내용만으로는 알 수 없고, 지금으로서는 오직 귀하만이 알 수 있습니다.

추가의 이미지

Quote:
그리고 가능하다면 수정한것을 텍스트에 덮어쓰고싶은데 다음줄로 저장이 됩니다. 어떻게 하는지 알려주세요ㅜㅜ

컴퓨터에서 텍스트 파일을 쓰는 건 원고지에 쓰는 것과 비슷합니다. 한가운데를 고치고 싶을 때는 잘 안 됩니다.
운이 좋으면 (길이가 잘 맞으면) 지우개로 필요한 만큼만 딱 지우고 덮어쓸 수도 있습니다. 하지만 대개는 고치고 싶은 부분부터 끝까지 싹 지워내고 전부 다시 써야 합니다.

어떻게든 할 수 있는 방법이 없는 것은 아니지만(지저분한 방법부터 복잡한 방법까지 다양합니다.) 그다지 별로 노력할 만한 가치가 없겠다 싶거든 그냥 파일 전체를 매번 통째로 쓰는 방법을 권장합니다. 그게 제일 간단하지요.

gud0415의 이미지

감사합니다!! 수정해보겠습니다

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.