Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Java Error- Char!
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Shivu




PostPosted: Fri Apr 04, 2008 5:41 pm   Post subject: Java Error- Char!

hi!
I don't why this is happening, but when i prompt the user to enter a variable which is a char, the program displays an error and just quits. it doesn't even give the user a chance to input it.
the program compiles fine but whenever it comes to that spot, it just exits displaying an error. I'll be grateful is someone can help me!!
thanks.

Here is my full code and the part that has the "problem" is highlighted. (the thing is that, when i use the declared scanner, that code doesn't work, but when i declare another scanner, the user is able to enter their option.


import java.util.*;

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



int choice;
int length = 1, width = 1;
char cont;




System.out.println("Welcome to the Shape Generator:\n");
System.out.println("This program can draw the following shapes:");
System.out.println("1) Horizontal Line");
System.out.println("2) Vertical Line");
System.out.println("3) Rectangle");
System.out.println("4) Left slant right angle triangle");
System.out.println("5) Isosceles triangle");

System.out.println("\nEnter your choice (1-5): ");
choice = sc.nextInt();



while (choice >5 || choice <1){
System.out.println("\nINVALID! Your choice must be between 1 and 5.\nEnter your choice: ");
choice = sc.nextInt();
}

// prompts for the dimensions and validates the input

do {

switch (choice) {

case 1: case 2: case 4: case 5:

System.out.print("Enter the ");
if (choice == 1) {
System.out.print("length of the horizontal line (1-20): ");
length = sc.nextInt();
} else if (choice == 2) {
System.out.print("length of the vertical line (1-20): ");
length = sc.nextInt();
} else if (choice == 4) {
System.out.print("height of the left slant right angle triangle (1-20): ");
length = sc.nextInt();
} else if (choice == 5) {
System.out.print("height of the isosceles triangle (1-20): ");
length = sc.nextInt();
}

break;

case 3:

System.out.print ("Enter the length of the rectangle (1-20): ");
length = sc.nextInt();

do {

if (length < 1 || length > 20) {
System.out.println ("INVALID!");
System.out.print ("\nEnter the length of the rectangle (1-20): ");
length = sc.nextInt();
}
} while (length < 1 || length > 20 );

System.out.print ("\nEnter the width of the rectangle (1-20): ");
width = sc.nextInt();

do {

if (width < 1 || width > 20) {
System.out.println ("INVALID!");
System.out.print ("\nEnter the width of the rectangle (1-20): ");
width = sc.nextInt();
}
} while (width <1 || width > 20 );

break;

default:
System.out.println ("INVALID!");

}
} while (length < 1 || length > 20 || width < 1 || width > 20 );

// outputs the figures

if (choice == 1) {

System.out.println ("\nHere is your horizontal line: \n");
for (int i = 1; i <= length; i++ ) {
System.out.print ("*");
}
} else if (choice == 2) {
System.out.println ("\nHere is your vertical line: \n");

for (int i = 1; i <= length; i++ ) {
System.out.println ("*");
}
} else if (choice == 3) {
System.out.println ("\nHere is your rectangle: \n");

for (int i = 1; i <= length; i ++) {
for (int j = 1; j <= width; j ++) {
System.out.print("*");
}
System.out.println ();
}

} else if (choice == 4) {
System.out.println ("\nHere is your left angle right triangle: \n");

for (int i = 1; i <= length; i ++) {
for (int j = length; j > i ; j --) {
System.out.print(" ");
}
for (int k = 1; k <=i; k++) {
System.out.print("*");
}
System.out.println ();
}
} else if (choice == 5) {
System.out.println ("\nHere is your isoceles triangle: \n");

for (int i = 1; i <= length; i ++) {
for (int j = length; j > i ; j --) {
System.out.print(" ");
}
for (int k = 1; k <i*2; k++) {
System.out.print("*");
}
System.out.println ();
}
}

// continue?

do {

Scanner sc1 = new Scanner (System.in);

System.out.print("\nWould you like to draw another one (y/n) ? ");
cont = sc1.nextLine().charAt(0);



the above part is where the 'continue' part is... where the problem is happening. again, it complies perfeectly, it just doesn't allow the user to enter the option when the program is running... instead it exits and displays an error. right now, i declared another scanner- scanner sc1... and original scanner is scanner sc and i want to use the original scanner throughout the code. but due to this problem, it is not working... this is the case only when i declare cont as char.


if (cont != 'y' || cont != 'Y' || cont != 'n' || cont != 'N') {
System.out.println ("INVALID! Your input must be 'y' or 'n'");
}

} while (cont != 'y' || cont != 'Y' || cont != 'n' || cont != 'N');


}
}
Sponsor
Sponsor
Sponsor
sponsor
Shivu




PostPosted: Fri Apr 04, 2008 5:53 pm   Post subject: Re: Java Error- Char!

the error by the way is: (and the error is displayed when the program is running) (this error is shown only when i use the original scanner... scanner sc):


----jGRASP exec: java Shape_Generator

Welcome to the Shape Generator:

This program can draw the following shapes:
1) Horizontal Line
2) Vertical Line
3) Rectangle
4) Left slant right angle triangle
5) Isosceles triangle

Enter your choice (1-5):
5
Enter the height of the isosceles triangle (1-20): 2

Here is your isoceles triangle:

*
***

Would you like to draw another one (y/n) ? Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:687)
at Shape_Generator.main(Shape_Generator.java:154)

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
HeavenAgain




PostPosted: Fri Apr 04, 2008 6:02 pm   Post subject: RE:Java Error- Char!

code:
     sc.nextLine(); // put it here to get a new line before getting more lines
    do {
      System.out.print("\nWould you like to draw another one (y/n) ? ");
      cont = sc.nextLine().charAt(0);
     
     
      if (cont != 'y' || cont != 'Y' || cont != 'n' || cont != 'N') {
        System.out.println ("INVALID! Your input must be 'y' or 'n'");
      }
     
    } while (cont != 'y' || cont != 'Y' || cont != 'n' || cont != 'N');
as much as i dont like scanner, i think this is how you would do it, you read another line before you actually go into that loop

as you can see, scanner still keeps your old data or somewhat like that, so you enetered 5, 2, and its still there, what you want is to move on to the next line (clean line) to start off with

altho i could be wrong. again, thumbs down for scanner!!

oh Ps. your condition for that while loop is wrong Wink
Shivu




PostPosted: Fri Apr 04, 2008 6:19 pm   Post subject: RE:Java Error- Char!

wow thanks! so if i put the sc.nextLine ().charAt(0); before the loop, it should work?

and yea, the condition is worng, i was wondering is you could help me that as well... cuz i'm stuck! :S i don't know what i'm doing wrong...

thanks again!!
Shivu




PostPosted: Fri Apr 04, 2008 6:21 pm   Post subject: RE:Java Error- Char!

oh! if you put sc.nextLine 90; only before the char thing... it automatically gets a new line?
HeavenAgain




PostPosted: Fri Apr 04, 2008 6:29 pm   Post subject: RE:Java Error- Char!

well, let see now, you want the user to enter y or n (ignoring cases)
so if he/she enter y OR n then it should be false (so your loop will exit)
so we will have something like
!(entered == 'y' || entered == 'n')
Shivu




PostPosted: Fri Apr 04, 2008 6:40 pm   Post subject: RE:Java Error- Char!

hmm... yea that makes sense!

yea cuz if one of them is true the entire thing is true and the ! will make is false... so the loop exits!

wow! thank you very much for your help !! Very Happy
Barbarrosa




PostPosted: Sat Apr 05, 2008 3:03 pm   Post subject: Re: Java Error- Char!

Don't forget to add the loop so the program repeats itself, as seems implied in the output.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: