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); DavidKroukamp
Practically a Master Poster
Team Colleague
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169