Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

charAt error in a program to print characters

Status
Not open for further replies.

JavaNoob101

Technical User
Sep 30, 2006
2
US
At least i think it's charAt.
I started this program to try and make mathmatical designs for characters (don't ask, no job, no school, i get bored) during the summer, and can't figure out why this has been happening:

SAMPLE OUTPUT*************************


Please input a letter to select a print style
a
Please enter the character you wish to print
@
Number of times you wish to print it per line:
4
Number of lines you wish to print:
3
@
Please input a letter to select a print style
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind
ex out of range: 0
at java.lang.String.charAt(String.java:558)
at Printshapes.main(Printshapes.java:32)
Press any key to continue . . .

END OUTPUT ERROR ****************

As for the code, i should probably post that too, but it's long. I'm going to put it down anyway, please tell me if there's a better way to post code.


BEGIN CODE *****************************************

import java.util.*;

public class Printshapes
{
public static void main (String[] args)
{
Scanner keyboard = new Scanner(System.in);

//Defining variables
char style = ' ';
char ch = ' ';
String stylestr = " ";
String chstr = " ";
int times = 0;
int rows = 0;
int t = 0;
int r = 0;

//Prompts and inputs

//Do Loop
do
{
style = ' ';
stylestr = " ";
System.out.println ("Please input a letter to select a print style");
stylestr = keyboard.nextLine();
style = stylestr.charAt(0);

if (style == 'a') (A is supposed to print the character a certain number of times on a certain number of lines)
{

System.out.println ("Please enter the character you wish to print");
chstr = keyboard.nextLine();
ch = chstr.charAt( 0 );

System.out.println ("Number of times you wish to print it per line:");
times = keyboard.nextInt();

System.out.println ("Number of lines you wish to print:");
rows = keyboard.nextInt();

for (r = rows; r > 0 ; r--);
{
for (t = times ; t > 0 ; t--);
{
System.out.print (ch);
}
System.out.println();
}

}
else
if (style == 'b')(B was counting up by one per line, to take it one step further)
{

System.out.println ("Please enter the character you wish to print");
chstr = keyboard.nextLine();
ch = chstr.charAt( 0 );

System.out.println ("Number of lines you wish to print:");
rows = keyboard.nextInt();

for (t = 1 ; t > rows ; t++);
{
System.out.print (ch);

for (r = rows ; r > 0 ; r--);
{
System.out.println();
}
}


}
else
if (style == 'f')
break;
}
while (style != 'f');


}
}

END CODE ******************************************

I would just like to know why the error is happening, please do not help with anything else. This is my own project and it's a pride thing.
 
Hi

If your [tt]String[/tt]'s length is 0 you can not extract any character from it. Check for its length and if is 0, skip the next lines.
Code:
[gray]// ...[/gray]
stylestr = keyboard.nextLine();

[red][b]if[/b] (stylestr.length()==0) [b]continue[/b];[/red]

style = stylestr.charAt(0);
[gray]// ...[/gray]
JavaNoob101 said:
tell me if there's a better way to post code
Yes. Put the code between [tt][ignore]
Code:
[/ignore][/tt] and [tt][ignore]
[/ignore][/tt] marks and in the code mark your comment as comment ( with [tt]//[/tt] or [tt]/* */[/tt] ), not just put it in parenthesis ( () ).

Feherke.
 
Thanks, just got back to the program today, and realized... why is it only printing one character? Why isn't it printing a line of them like I asked it to?
 
These are the relevant bits:
Code:
                    for (r = rows; r > 0 ; r--);
                    {
                        for (t = times ; t > 0 ; t--);
                        {
                            System.out.print (ch);
                        }
                        System.out.println();
                    }

I'm not sure why you're so fond of "counting down," but most people "count up" on their for loops.
Code:
                    for (r = 0; r < rows ; r++);
                    {
                        for (t = 0; t < times  ; t++);
                        {
                            System.out.print (ch);
                        }
                        System.out.println();
                    }

Other than that, your logic looks right.
 
Case of the funky semicolon:
Code:
 for (int r = rows; r > 0; r--)
	{
		for (int t = times; t > 0; t--)
		{
			System.out.print (ch);
		}
		System.out.println ();
	}
Code:
 for (int r = rows; r > 0; r--);
	{
		for (int t = times; t > 0; t--);
		{
			System.out.print (ch);
		}
		System.out.println ();
	}

In the b-clause too, but there is another bug:

Code:
  for (t = 1 ; t > rows ; t++);
t will allmost never be greater than rows, will it?

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top