• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strange bug in a class I wrote (not sure how to explain in title)

 
Christopher Young
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. I am a very new first year programmer to Java and I am learning Java through applications. I am building a game that requires a lot of input from the user (don't all games need this?). The way I've been taught (maybe teacher's preference on showing us "behind the switch" stuff or due to AP testing mandating a certain style) I have been taught that when the user needs to input a number to declare a string and parse it to a number.

This requires two lines of code. Since the game I am designing is very method heavy (it's a text-based adventure. I wanted to infuse writing into my programming, so it uses "rooms", also, I've generally been method heavy with anything fairly complex I write. Keeps everything in check a lot easier and less sequential at times), I decided to make a class.

This is the code for the class

import java.io.*;

// current bugs: if method to is just println it will go somewhere in the program, not terminate.

public class InputOutput
{
private static String UserInput;
private static int StringtoInt;

public static int inputInt() throws IOException
{
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(reader);
UserInput = input.readLine();
StringtoInt = Integer.parseInt(UserInput);
return StringtoInt;
}
}


So, when I want the user to input a command, I just type int <variable> = InputOutput.inputInt(); This works exactly as intended, except for one problem.

If i have say

public static void method1() throws IOException
{
int choice = InputOutput.inputInt();

switch (choice)
{
case 1: method300();
}
}

public static void method300()
{
//code that outputs text
}


with no input call(right term? sorry, new to all this) it will go to method300, output the text in there, and then kick to some other method (always the same one, but why on earth that one i have no clue) and output the text there. I have a feeling it will run through the program if there were no input interrupts.

Why is this doing this? I'ts a fairly annoying bug that I can work around because I'm pretty much going to have an interrupt everywhere (nature of the game) but I want to know why is it doing this? What is causing the bug? Because I'm not seeing it at all so it's just puzzling to me.

Is it because import java.io.*; is declared more than once in my program (some classes are broken into seperate files and need io for throws IOException)

Also, if I were to add exception catches to the class, would this fix it? Or would I no longer need "throws IOException" after each class?

I don't neccessarily want someone to just post a fix for me. What I'm looking for is jut to be pointed in the right direction... i want a clue so that my brain has something to go on and stuff.
 
Christopher Young
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, if it matters, I am using the codeWarrior IDE
 
Tarek Khojah
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many design issues aside, shouldn't there be a "break" somewhere in that switch/case?
 
Jan van Mansum
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just tried the following code and see nothing strange happening. If I type 1, followed by Enter, it goes to method3000(), otherwise not.



Can you paste this code into a Test.java file, compile and run it, and tell me how what you are seeing is different from what you expect?
 
Christopher Young
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I apologize for not using the code tags as I did not know about them when I made the post. I'll re-do it so it should be easier to read.

the lack of a break might be it, i dunno. I generally haven't messed forcing a test to be true at some point because I haven't worried about it yet (I know, bad habit... users will do silly things with a program just to do it, i know I do)

 
Adam Schaible
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use a switch statement, without breaks - once it finds the first suitable case, it will execute it, and then every other one after it until it runs out of cases, or finds a break.


[ November 02, 2007: Message edited by: Adam Schaible ]
 
Christopher Young
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So should every switch statement be denoted by
[code]
switch (variable)
{
case 1: // blah
break;
case 2: // Blah
break;
case 3: // blah
break;
// etc
}

Also, I haven't done a lot of the things you guys have said yet in regarding to testing this (haven't gotten around to it yet, sorry guys) but I've found a few more errors in my tool. It's either a simple error or a very big one. And maybe its just related to how I'm handling the testing
 
Rob Spoor
Sheriff
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chris Young the second:
So should every switch statement be denoted by

Unless you explicitly want it to fall through to the next case. In that case, always put a comment that you wanted to fall through:
 
Christopher Young
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks.
 
Christopher Young
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well did some heavy testing and I believe I have fixed this bug.

It is something that shows up in the case tests when no break was included (rather than some flaw in the class like I was thinking at first).

The tip on how the switch - case statement works was really helpful.

Thank you. I really appreciate it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic