
-----------------------------------
Shivu
Fri Apr 04, 2008 5:41 pm

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 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');


	}
}

-----------------------------------
Shivu
Fri Apr 04, 2008 5:53 pm

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
Fri Apr 04, 2008 6:02 pm

RE:Java Error- Char!
-----------------------------------
     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 ;)

-----------------------------------
Shivu
Fri Apr 04, 2008 6:19 pm

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
Fri Apr 04, 2008 6:21 pm

RE:Java Error- Char!
-----------------------------------
oh! if you put sc.nextLine 90; only before the char thing... it automatically gets a new line?

-----------------------------------
HeavenAgain
Fri Apr 04, 2008 6:29 pm

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
Fri Apr 04, 2008 6:40 pm

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 !! :D

-----------------------------------
Barbarrosa
Sat Apr 05, 2008 3:03 pm

Re: Java Error- Char!
-----------------------------------
Don't forget to add the loop so the program repeats itself, as seems implied in the output.
