Results 1 to 4 of 4
  1. #1
    Dinomite07 is offline Member
    Join Date
    Nov 2015
    Posts
    24
    Rep Power
    0

    Default Exception in thread "main"

    Thank You

    *deleted so my classmates can't take my code by accident^

    original text:

    I am new to Java and this website and I am encountering a error which I do know what it means so I can't fix it.

    Java Code:
    public class TIL {
    public static void main(String[] args) {
    
    TypeWriter.Input();
    TypeWriter.FirstLevel();
    TypeWriter.SecondLevel();
    TypeWriter.ThirdLevel();
    TypeWriter.FourthLevel();
    TypeWriter.FifthLevel();
    }
    }
    Java Code:
    import java.util.Scanner;
    
    public class TypeWriter {
    public static char[] charArray = new char[4];
    
    public static void Input() {
    Scanner scan = new Scanner(System.in);
    // define variables we need of suitable type
    // initialize the values of the variables
    for (int i = 1; i <= 4; i = i + 1) {
    System.out.println("Enter #" + i + " Character ('T' or 'I' or 'L')");
    charArray[i] = scan.next().charAt(0);
    }
    scan.close();
    }
    
    public static void FirstLevel() {
    //prints user input
    for (int i = 1; i <= 4; i = i + 1) {	
    if (charArray[i] == 'T') {
    System.out.print("-----");
    } 
    else if (charArray[i] == 'I') {
    System.out.print("-----");
    }
    else { 
    System.out.print("| ");
    }
    System.out.print(" ");
    }
    System.out.println("");
    }
    
    public static void SecondLevel() {
    //prints user input
    for (int i = 0; i < 5; i = i + 1) {	
    if (charArray[i] == 'T') {
    System.out.print(" | ");
    } 
    else if (charArray[i] == 'I') {
    System.out.print(" | ");
    }
    else { 
    System.out.print("| ");
    }
    System.out.print(" ");
    }
    System.out.println("");
    }
    
    public static void ThirdLevel() {
    //prints user input
    for (int i = 0; i < 5; i = i + 1) {	
    if (charArray[i] == 'T') {
    System.out.print(" | ");
    } 
    else if (charArray[i] == 'I') {
    System.out.print(" | ");
    }
    else { 
    System.out.print("| ");
    }
    System.out.print(" ");
    }
    System.out.println("");
    }
    
    public static void FourthLevel() {
    //prints user input
    for (int i = 0; i < 5; i = i + 1) {	
    if (charArray[i] == 'T') {
    System.out.print(" | ");
    } 
    else if (charArray[i] == 'I') {
    System.out.print(" | ");
    }
    else { 
    System.out.print("| ");
    }
    System.out.print(" ");
    }
    System.out.println("");
    }
    
    public static void FifthLevel() {
    //prints user input
    for (int i = 0; i < 5; i = i + 1) {	
    if (charArray[i] == 'T') {
    System.out.print(" | ");
    } 
    else if (charArray[i] == 'I') {
    System.out.print("-----");
    }
    else { 
    System.out.print("----- ");
    }
    System.out.print(" ");
    }
    System.out.println("");
    }
    
    }
    Thank You
    Last edited by JosAH; 11-18-2015 at 09:13 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Exception in thread "main"

    it means you have exceed the maximum index of your array. Here is the culprit.

    for(inti = 1; i <= 4; i = i + 1) {

    Arrays in Java are 0 based so it should be from 0 to < 4 or 0 to <= 3.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Exception in thread "main"

    Don't delete code from the original thread. Even though it may be wrong, nobody knows what Jim's answer is all about anymore. Besides, if your classmates copy stuff off teh internetz and hand it in as their own, they deserve to fail. Best case they learn something from your code.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Exception in thread "main"

    I restored the original text; @OP: if you don't want your classmates to copy your code, you shouldn't post it here; removing it all, renders the entire thread useless so I restored the original text.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Replies: 1
    Last Post: 11-02-2012, 11:38 PM
  2. Replies: 2
    Last Post: 06-20-2012, 12:18 AM
  3. Replies: 5
    Last Post: 12-21-2011, 06:44 PM
  4. Replies: 14
    Last Post: 04-08-2011, 06:01 PM
  5. Replies: 5
    Last Post: 06-22-2010, 03:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •