Hi guys, I was doing my project when i came across this error: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:686)
at Screen.screenRunner(Screen.java:84)
at Screen.main(Screen.java:90)

I am also having problems getting my program to accept user input(height and width).

import java.util.Scanner;
public class Screen {
	Scanner sc = new Scanner(System.in);
	private static char [][] grid;
	private static char borderChar;

	public Screen(){
	}

	public Screen(int h, int w){
		grid = new char [h][w];
	}

	public Screen(char b){
		this.borderChar = b;
	}

	public static void clearScreen(){
		for(int i = 0; i < grid.length; i++){
			for(int j = 0; j < grid[0].length; j++){
				grid [i][j] = 0;
			}
		}
	}
	
	public void setH(int h){
	}

	public static int getH(){
		return grid.length;
	}
	
	public void setW(int w){
	}

	public static int getW(){
		return grid[0].length;
	}

	public static boolean isValidLocation(int h, int w){
		if((h < (grid.length - grid.length) &&
			w < (grid[0].length - grid[0].length))
			&& (h > grid.length && w > grid[0].length))
			return false;
		else
			return true;
	}

	public static void paintAt(int h, int w, char status){
		grid[h][w] = status;
	}

	public static int getAt(int h, int w){
		return grid[h][w];
	}

	public static void draw(char borderChar){
		for(int i = 0; i < getH(); i++){
			System.out.print(borderChar + " ");
		}
		System.out.println();
		for(int j = 0; j < getW(); j++){
			System.out.print(borderChar);
			for(int m = 0; m < 2*getW() - 3; m++){
				System.out.print(" ");
			}
			System.out.print(borderChar + "\n");
		}
		for(int k = 0; k < getH(); k++){
			System.out.print(borderChar + " ");
		}
	}

	public void getScreenSize(){
		System.out.println("What is the your desired height?");
		int hrun = sc.nextInt();
		System.out.println("What is the your desired width?");
		int wrun = sc.nextInt();
	}

	public void screenRunner(){
		System.out.println("What is the your desired border character?");
		String run = sc.nextLine();
		draw(run.charAt(0));
	}

	public static void main(String [] args){
		Screen s = new Screen(15, 15);
		s.getScreenSize();
		s.screenRunner();
	}
}
timmy568
Deleted Member

Hi guys, I was doing my project when i came across this error: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:686)
at Screen.screenRunner(Screen.java:84)
at Screen.main(Screen.java:90)

I am also having problems getting my program to accept user input(height and width).

import java.util.Scanner;
public class Screen {
	Scanner sc = new Scanner(System.in);
	private static char [][] grid;
	private static char borderChar;

	public Screen(){
	}

	public Screen(int h, int w){
		grid = new char [h][w];
	}

	public Screen(char b){
		this.borderChar = b;
	}

	public static void clearScreen(){
		for(int i = 0; i < grid.length; i++){
			for(int j = 0; j < grid[0].length; j++){
				grid [i][j] = 0;
			}
		}
	}
	
	public void setH(int h){
	}

	public static int getH(){
		return grid.length;
	}
	
	public void setW(int w){
	}

	public static int getW(){
		return grid[0].length;
	}

	public static boolean isValidLocation(int h, int w){
		if((h < (grid.length - grid.length) &&
			w < (grid[0].length - grid[0].length))
			&& (h > grid.length && w > grid[0].length))
			return false;
		else
			return true;
	}

	public static void paintAt(int h, int w, char status){
		grid[h][w] = status;
	}

	public static int getAt(int h, int w){
		return grid[h][w];
	}

	public static void draw(char borderChar){
		for(int i = 0; i < getH(); i++){
			System.out.print(borderChar + " ");
		}
		System.out.println();
		for(int j = 0; j < getW(); j++){
			System.out.print(borderChar);
			for(int m = 0; m < 2*getW() - 3; m++){
				System.out.print(" ");
			}
			System.out.print(borderChar + "\n");
		}
		for(int k = 0; k < getH(); k++){
			System.out.print(borderChar + " ");
		}
	}

	public void getScreenSize(){
		System.out.println("What is the your desired height?");
		int hrun = sc.nextInt();
		System.out.println("What is the your desired width?");
		int wrun = sc.nextInt();
	}

	public void screenRunner(){
		System.out.println("What is the your desired border character?");
		String run = sc.nextLine();
		draw(run.charAt(0));
	}

	public static void main(String [] args){
		Screen s = new Screen(15, 15);
		s.getScreenSize();
		s.screenRunner();
	}
}

I see no problem when accepting the user height and width?
and do this to accept a char...

Scanner sc = new Scanner(System.in);
        String input = sc.next();
 
        char[] myChar;
        myChar = input.toCharArray();
        System.out.println(myChar);

Edited 4 Years Ago by DavidKroukamp: n/a

You simply need to check if the length of the string is greater than 0. The error you're getting is resulting from the charAt(0) call on a string that has no first element (probably the user simply pressed 'enter' without typing anything). Try adding this check:

public void screenRunner(){
	boolean read = false;
	while(!read){
		System.out.println("What is your desired border character?");
		String run = sc.nextLine();
		if(run.length() != 0){
			draw(run.charAt(0));
			read = true;
		}
	}
}

Note that the boolean read value will make the input message loop until some character is entered.

Edited 4 Years Ago by dmanw100: n/a

ok thanks for that, but now how would i go ahead and make the user inputted values fdor height and width be used as the size of my screen. I just manually input the values in an object reference in main, and my 2 argument constructor uses those values.

ok thanks for that, but now how would i go ahead and make the user inputted values fdor height and width be used as the size of my screen. I just manually input the values in an object reference in main, and my 2 argument constructor uses those values.

declare them as global variables, and then send those global variables as arguments, or declare them in main, and send themas an argument to the method

but if you declare them in main, then you cannot use scanner.

but if you declare them in main, then you cannot use scanner.

yes you can? this compiles with no errors:

package javaapplication21;

import java.util.Scanner;


public class JavaApplication21 {//Horses
static String a=null;
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        a = sc.next();
        

    }

}

Ok so i have gotten this far but it is still throwing the StringStringIndexOutOfBoundsException

import java.util.Scanner;
public class Screen {
	private static char [][] grid;
	private static char borderChar;

	public Screen(){
	}

	public Screen(int h, int w){
		grid = new char [h][w];
	}

	public Screen(char b){
		this.borderChar = b;
	}

	public static void clearScreen(){
		for(int i = 0; i < grid.length; i++){
			for(int j = 0; j < grid[0].length; j++){
				grid [i][j] = 0;
			}
		}
	}

	public static int getH(){
		return grid.length;
	}

	public static int getW(){
		return grid[0].length;
	}

	public static boolean isValidLocation(int h, int w){
		if((h < (grid.length - grid.length) &&
			w < (grid[0].length - grid[0].length))
			&& (h > grid.length && w > grid[0].length))
			return false;
		else
			return true;
	}

	public static void paintAt(int h, int w, char status){
		grid[h][w] = status;
	}

	public static int getAt(int h, int w){
		return grid[h][w];
	}

	public void draw(char borderChar){
		for(int i = 0; i < getH(); i++){
			System.out.print(borderChar + " ");
		}
		System.out.println();
		for(int j = 0; j < getW(); j++){
			System.out.print(borderChar);
			for(int m = 0; m < 2*getW() - 3; m++){
				System.out.print(" ");
			}
			System.out.print(borderChar + "\n");
		}
		for(int k = 0; k < getH(); k++){
			System.out.print(borderChar + " ");
		}
	}

	public static void main(String [] args){
		Scanner sc = new Scanner(System.in);
		System.out.println("What is the your desired width?");
		int w = sc.nextInt();
		System.out.println("What is the your desired height?");
		int h = sc.nextInt();
		Screen s = new Screen(h, w);
		System.out.println("What is the your desired border character?");
		String run = sc.nextLine();
		s.draw(run.charAt(0));
	}
}

The program works perfectly if i comment the height and width out in main, and insert values manually. I dont understand why

public static void main(String [] args){
		Scanner sc = new Scanner(System.in);
		System.out.println("What is the your desired width?");
		int w = sc.nextInt();
		System.out.println("What is the your desired height?");
		int h = sc.nextInt();
		Screen s = new Screen(h, w);
		System.out.println("What is the your desired border character?");
		String run = sc.nextLine();
		s.draw(run.charAt(0));
	}

is not working...

now it works fine but my println prints out twice.

boolean read = false;
		while(!read){
			System.out.println("What is your desired border character (only the first character will be printed)?");
			String run = sc.nextLine();
			if(run.length() != 0){
					draw(run.charAt(0));
					read = true;

now it works fine but my println prints out twice.

boolean read = false;
		while(!read){
			System.out.println("What is your desired border character (only the first character will be printed)?");
			String run = sc.nextLine();
			if(run.length() != 0){
					draw(run.charAt(0));
					read = true;

By "prints out twice" do you mean that after the inputting, it loops again showing again the println?

Can you post the whole while loop

Edited 4 Years Ago by zeroliken: n/a

that is the whole while loop...

and yes it does run twice, but i do not understand why.

now it works fine but my println prints out twice.

boolean read = false;
		while(!read){
			System.out.println("What is your desired border character (only the first character will be printed)?");
			String run = sc.nextLine();
			if(run.length() != 0){
					draw(run.charAt(0));
					read = true;

If that's the whole loop your missing 2 end brackets one for the if statement and one for the whole while loop

Thanks Man! So now in my other class im having trouble as well. My sanner is not working for some odd reason. When i want to print a vertical line, it just printlines and moves on.

import java.util.Scanner;
public class Interactive {
	Scanner sc = new Scanner(System.in);
	VLine v = new VLine();

	public void menuRunner(){
		System.out.println("What is the your desired height?");
		int hrun = sc.nextInt();
		System.out.println("What is the your desired width?");
		int wrun = sc.nextInt();
		Screen s = new Screen(hrun, wrun);
		s.screenRunner();
		System.out.println();
		int x = 0;
		while(x == 0){
			System.out.println("Please Make a Selection:");
            System.out.println("[1] Clear The Screen");
            System.out.println("[2] Print a Horizontal Line");
            System.out.println("[3] Print a Vertical Line");
            System.out.println("[4] Print Text");
            System.out.println("[5] Print a Box");
            System.out.println("[6] Print a Frame");
            System.out.println("[7] Exit The Menu");
            int selection = sc.nextInt();
            switch (selection){
            	case 1:
            		s.clearScreen();
            		System.out.println();
                	break;
                case 2:
                	System.out.println("Print a Horizontal Line \n");
                	break;
                case 3:
                	vLinePrint();
                	break;
                case 4:
                	System.out.println("Print Text \n");
                	break;
                case 5:
                	System.out.println("Print a Box \n");
                	break;
                case 6:
                	System.out.println("Print a Frame \n");
                	break;
                case 7:
                	System.out.println("Thank You for Your Time!");
                	System.exit(0);
                default:
                	System.out.println("Please enter a valid selection! \n");
        	 }
        }
    }

    public void vLinePrint(){
		System.out.println("What is the your desired character?");
		String vl = sc.nextLine();
		v.verticalPrint(vl);
    }

	public static void main(String[] args) {
		Interactive i = new Interactive();
		i.menuRunner();
	}
}

hey guys could you help me with something else? in my VLine class, i have to draw a vertical line onto a screen. But I cant get my code to work:

public class VLine extends Line{

	public VLine(){
	}

	public VLine(int l, String pChar, Screen s){
		super(l, pChar);
	}

	public void paintOn(Screen s){
		s.paintAt(0, 0, ' ');
	}

	public void paintOn(Screen s, int xc, int yc){
		System.out.println("What is the your desired print character?");
		String pChar = sc.nextLine();
		paintOn(s);
		s.paintAt(xc, yc, pChar);
		for(int i = 0; i < pChar.length(); i++){
			System.out.println(pChar.charAt(i));
		}
	}
}

and this is Screen:

import java.util.Scanner;
public class Screen {
	Scanner sc = new Scanner(System.in);
	private static char [][] grid;
	private static char borderChar;

	public Screen(){
	}

	public Screen(int h, int w){
		grid = new char [h][w];
	}

	public Screen(char b){
		this.borderChar = b;
	}

	public static void clearScreen(){
		for(int i = 0; i < grid.length; i++){
			for(int j = 0; j < grid[0].length; j++){
				paintAt(i, j, ' ');
			}
		}
	}

	public static int getH(){
		return grid.length;
	}

	public static int getW(){
		return grid[0].length;
	}

	public static boolean isValidLocation(int h, int w){
		if((h < (grid.length - grid.length) &&
			w < (grid[0].length - grid[0].length))
			&& (h > grid.length && w > grid[0].length))
			return false;
		else
			return true;
	}

	public static void paintAt(int h, int w, char status){
		if(isValidLocation(h, w)){
			grid[h][w] = status;
		}
	}

	public static int getAt(int h, int w){
		return grid[h][w];
	}

	public static void draw(char borderChar){
		for(int i = 0; i < getH(); i++){
			System.out.print(borderChar + " ");
		}
		System.out.println();
		for(int j = 0; j < getW() - 2; j++){
			System.out.print(borderChar);
			for(int m = 0; m < 2*getW() - 3; m++){
				System.out.print(" ");
			}
			System.out.print(borderChar + "\n");
		}
		for(int k = 0; k < getH(); k++){
			System.out.print(borderChar + " ");
		}
	}

	public void screenRunner(){
		System.out.println("What is the your desired border character?");
		String run = sc.nextLine();
		draw(run.charAt(0));
	}
}

i have to draw a vertical line onto a screen.

Are you trying to create a GUI?
If that's the case you can either use a Swing component for the draw capability like in a JPanel or
the draw function for Applets

Edited 4 Years Ago by zeroliken: n/a

Java Server Pages, Java Server Faces, or a servile. It is object oriented programing language. Which supports multi threading. As a search string or add some items to a shopping cart in any case, as requested by the user activates call the session.

Are you trying to create a GUI?

No, i have to create one in console output. A GUI would be much easier.

This article has been dead for over six months. Start a new discussion instead.