My basic encrypt/uncrypt program... break; breaks when not supposed 2 in java
Hey, I wrote this... I don't see anything wrong with it, I asked other ppl and they don't see anything wrong with it, do any of you?
When I run this everything seems to work, but if I enter choice 1, nothing happens, the program loops. If I enter choice 2 I get the output (I deleted a few blank lines 2 make it look better) Code:
1 Encrypt If I enter choice 3, it succesfully quits the program. I think the only problem could be is the break; (breaks the loop) but it breaks the loop if is in if statement and it is. Can somebody tell me how to fix this. Code:
public class Encrypt { |
I edited out an incorrect answer.
___________________________________ Be prepared. Create a LifeBoat CD. http://users.rcn.com/srstites/LifeBo...home.page.html Steve Stites |
edited out an even wronger answer.... :o
|
When you read the choice, you press enter so the newline is left in the buffer, so when you try to read a string in choice 2, you get that remaining newline and you never get the chance to input anything new.
|
huh?
But I made string s= newline. So why would int choice affect it? (if thats what u meant) |
Can you post your EasyReader class?
|
Re: My basic encrypt/uncrypt program... break; breaks when not supposed 2 in java
I ran your code through my debugger. I didn't have your EasyReader class of course, so I changed
Code:
EasyReader n = new EasyReader(); Code:
BufferedReader n = new InputStreamReader(System.in); The code didn't loop on my machine, what I did notice was that you don't empty temp on a new loop. So if the user types "test" the first time, temp will be filled with let's say *!^* (just an example - it will be different when you try it). If you want to try something else, temp still contains this encoded string and the next string will simply be added to temp (resulting in wrong output). So you should add Code:
while (choice != 3) { |
Quote:
Code:
BufferedReader n = new BufferedReader(new InputStreamReader(System.in)); Code:
choice= Integer.parseInt(n.readLine()); Also, like nephilim mentioned, you need to reset temp after encrypting/decrpting Going back to the original problem of looping, if you were to check what's actually being read into choice, you would see the problem: Code:
DataInputStream n = new DataInputStream(System.in); So you're better of using a BufferedReader and it's readLine(), then parsing to an int whenever you need to. |
Sorry for my late reply coolman0stress. Here ya go, also available at http://www.skylit.com/javamethods/appxe.html
Code:
// package com.skylit.io; |
All times are GMT -5. The time now is 03:59 AM. |