Member Avatar for timmy568

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();
	}
}

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);

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.

Member Avatar for timmy568

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

Member Avatar for timmy568

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();
        

    }

}
Member Avatar for timmy568

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));
	}
}
Member Avatar for timmy568

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...

Member Avatar for timmy568

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

Member Avatar for timmy568

that is the whole while loop...

Member Avatar for timmy568

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

Member Avatar for timmy568

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();
	}
}
Member Avatar for timmy568

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));
		}
	}
}
Member Avatar for timmy568

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

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.

Member Avatar for timmy568

Are you trying to create a GUI?

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.