We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,451 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Java input help

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();
	}
}
6
Contributors
19
Replies
2 Days
Discussion Span
1 Year Ago
Last Updated
20
Views
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);
DavidKroukamp
Master Poster
Team Colleague
735 posts since Dec 2011
Reputation Points: 279
Solved Threads: 181
Skill Endorsements: 4

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.

dmanw100
Posting Whiz in Training
271 posts since Apr 2008
Reputation Points: 104
Solved Threads: 32
Skill Endorsements: 0

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.

timmy568
Deleted Member

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

DavidKroukamp
Master Poster
Team Colleague
735 posts since Dec 2011
Reputation Points: 279
Solved Threads: 181
Skill Endorsements: 4

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

timmy568
Deleted Member

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

    }

}
DavidKroukamp
Master Poster
Team Colleague
735 posts since Dec 2011
Reputation Points: 279
Solved Threads: 181
Skill Endorsements: 4

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

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

timmy568
Deleted Member

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;
timmy568
Deleted Member

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

zeroliken
Nearly a Posting Virtuoso
1,346 posts since Nov 2011
Reputation Points: 214
Solved Threads: 205
Skill Endorsements: 14

that is the whole while loop...

timmy568
Deleted Member

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

timmy568
Deleted Member

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

zeroliken
Nearly a Posting Virtuoso
1,346 posts since Nov 2011
Reputation Points: 214
Solved Threads: 205
Skill Endorsements: 14

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

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

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

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

zeroliken
Nearly a Posting Virtuoso
1,346 posts since Nov 2011
Reputation Points: 214
Solved Threads: 205
Skill Endorsements: 14

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.

forkmartin
Newbie Poster
5 posts since Jan 2012
Reputation Points: 6
Solved Threads: 1
Skill Endorsements: 0

Are you trying to create a GUI?

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

timmy568
Deleted Member

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1289 seconds using 2.77MB