simple java problem... i think

java.io.IOException: Stream closed dont know why

Page 1 of 1

11 Replies - 695 Views - Last Post: 02 February 2010 - 01:53 PM Rate Topic: -----

#1 ludjer  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 186
  • Joined: 31-October 08

simple java problem... i think

Posted 02 February 2010 - 01:13 PM

got a problem with some code
package prac0;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * C++ to Java prac 0
 * @author Ludger Peters
 * @version V1
 * @student-number 29078718
 */

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        palindrome pal = new palindrome();
        GCD gcd = new GCD();
        int choice;
        try {
            do {
                System.out.println("Enter 1 for palindrome checker");
                System.out.println("Enter 2 to work out greatest common divisor");
                InputStreamReader converter = new InputStreamReader(System.in);
                BufferedReader in = new BufferedReader(converter);
                choice = Integer.parseInt(in.readLine());
                switch (choice) {
                  case 1:
                              pal.start();
                     break;
                  case 2:
                              gcd.start();
                     break;
                  default:
                     System.out.println("Unknown value ");
                     choice = 0;
                    }
                
                in.close();
                converter.close();
            } while(choice != 0);
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

it runs the first time properly but the second time it happens
it gets to here
choice = Integer.parseInt(in.readLine());

and then the following happens, this is the full output of the program

Quote

run:
Enter 1 for palindrome checker
Enter 2 to work out greatest common divisor
1
Enter word to check
piggy
piggy
5
No
Enter 1 for palindrome checker
Enter 2 to work out greatest common divisor
java.io.IOException: Stream closed
BUILD SUCCESSFUL (total time: 5 seconds)


did i do somthing wrong if needed i can attach the other files, but the only thing that would interfere is the following, though why would it stop only after one loop?
InputStreamReader converter = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(converter);
in.close();
converter.close();

but i highly doubt that

Thanks in advanced.

EDIT
Update i removed the functions and tied it again and it ran with out a problem now i have a problem that it is the streamer getting closed for some reason i have tried the following
Taken the streamer outside the do while brackets
changed the streamer and buffer names

dont know what else to try

This post has been edited by ludjer: 02 February 2010 - 01:22 PM


Is This A Good Question/Topic? 0
  • +

Replies To: simple java problem... i think

#2 circuspeanuts  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 224
  • Joined: 11-April 08

Re: simple java problem... i think

Posted 02 February 2010 - 01:24 PM

the only thing that I can think of is:

do {
                System.out.println("Enter 1 for palindrome checker");
                System.out.println("Enter 2 to work out greatest common divisor");
                InputStreamReader converter = new InputStreamReader(System.in);
                BufferedReader in = new BufferedReader(converter);
                choice = Integer.parseInt(in.readLine());
                switch (choice) {
                  case 1:
                              pal.start();
                     break;
                  case 2:
                              gcd.start();
                     break;
                  default:
                     System.out.println("Unknown value ");
                     choice = 0;
                    }
                
                in.close();
                converter.close();
            } while(choice != 0);


the entire time your code is running, you never set the value of choice back to 0. You do once the program hits an unknown integer.

Try that.


EDIT: now that I think about it, wouldn't you want to initiate
int count;



inside your try/do-while block so it's called over each time the loop goes through?

This post has been edited by circuspeanuts: 02 February 2010 - 01:30 PM

Was This Post Helpful? 0
  • +
  • -

#3 circuspeanuts  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 224
  • Joined: 11-April 08

Re: simple java problem... i think

Posted 02 February 2010 - 01:37 PM

come to think about it, you have a class called palindrome? post all of your code so I can utilize it and better be able to help you.
Was This Post Helpful? 0
  • +
  • -

#4 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: simple java problem... i think

Posted 02 February 2010 - 01:38 PM

Just kidding. Looking further into it.

Second Edit: Apparently I was right.

Move the close() statements to outside the loop.

This post has been edited by Locke: 02 February 2010 - 01:51 PM

Was This Post Helpful? 0
  • +
  • -

#5 ludjer  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 186
  • Joined: 31-October 08

Re: simple java problem... i think

Posted 02 February 2010 - 01:39 PM

thanks for helping circuspeanuts after alot of playing around i finally found a solution i took out the
in.close();
converter.close();

of the other 2 classes and ounce i did this then it kept on looping dont know why even though i did change the names of the objects...

also

Quote

the entire time your code is running, you never set the value of choice back to 0. You do once the program hits an unknown integer.

if it hits an unknown integer it is meant to close down its a prac im doing had to convert c++ code to java code so im trying to do exactly what he did
this was the original code
#include <iostream>
#include "palindrome.h"
#include "GCD.h"
using namespace std;

int main()
{
	palindrome* pal = new palindrome();
	GCD* gcd = new GCD();
	cout << "Enter 1 for palindrome checker" << endl;
	cout << "Enter 2 to work out greatest common divisor" << endl;
	int choice;
	do
	{
	
	cin >> choice;

	switch (choice) {
      case 1:
		  pal->start();
         break;
      case 2:
		  gcd->start();
         break;
      default: 
         cout << "Unknown value" << endl;
		 choice = 0;	
		}

	}
	while(choice != 0);
	

}

Locke ye i decided to take the stream and buffer out the loop.
thanks alot guys for helping
its solved now.

edit:
Locke one thing though why would it matter if i close it i do reinitialize it anyway at the beginning of the loop ?

This post has been edited by ludjer: 02 February 2010 - 01:42 PM

Was This Post Helpful? 0
  • +
  • -

#6 circuspeanuts  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 224
  • Joined: 11-April 08

Re: simple java problem... i think

Posted 02 February 2010 - 01:41 PM

Wow, locke, I didn't even see that..

nice catch
Was This Post Helpful? 0
  • +
  • -

#7 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: simple java problem... i think

Posted 02 February 2010 - 01:43 PM

View Postludjer, on 02 February 2010 - 02:39 PM, said:

edit:
Locke one thing though why would it matter if i close it i do reinitialize it anyway at the beginning of the loop ?


It shouldn't...that's why I'm looking further into it. :) I edited my previous post.

Did taking out out work, though?

I don't think you actually have to close the stream anyway. But why would you reinitialize them every iteration? Seems like a lot of work to keep a constant input buffer from the keyboard. Instantiate them once, before the loop. Then close them after the loop. That would alleviate all problems anyway.

This post has been edited by Locke: 02 February 2010 - 01:46 PM

Was This Post Helpful? 1
  • +
  • -

#8 circuspeanuts  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 224
  • Joined: 11-April 08

Re: simple java problem... i think

Posted 02 February 2010 - 01:46 PM

View Postludjer, on 02 February 2010 - 12:39 PM, said:

thanks for helping circuspeanuts after alot of playing around i finally found a solution i took out the
in.close();
converter.close();

of the other 2 classes and ounce i did this then it kept on looping dont know why even though i did change the names of the objects...

also

Quote

the entire time your code is running, you never set the value of choice back to 0. You do once the program hits an unknown integer.

if it hits an unknown integer it is meant to close down its a prac im doing had to convert c++ code to java code so im trying to do exactly what he did
this was the original code
#include <iostream>
#include "palindrome.h"
#include "GCD.h"
using namespace std;

int main()
{
	palindrome* pal = new palindrome();
	GCD* gcd = new GCD();
	cout << "Enter 1 for palindrome checker" << endl;
	cout << "Enter 2 to work out greatest common divisor" << endl;
	int choice;
	do
	{
	
	cin >> choice;

	switch (choice) {
      case 1:
		  pal->start();
         break;
      case 2:
		  gcd->start();
         break;
      default: 
         cout << "Unknown value" << endl;
		 choice = 0;	
		}

	}
	while(choice != 0);
	

}

Locke ye i decided to take the stream and buffer out the loop.
thanks alot guys for helping
its solved now.


I understand that, HOWEVER, once the loop goes through, then count is pretty much dumped, so when it loops again, it has no variable to go by, thus causing an error. It's like this



//variable outside the function
int count;

        try {
            do {
                System.out.println("Enter 1 for palindrome checker");
                System.out.println("Enter 2 to work out greatest common divisor");
                InputStreamReader converter = new InputStreamReader(System.in);
                BufferedReader in = new BufferedReader(converter);

                //variable is used
                choice = Integer.parseInt(in.readLine());
                switch (choice) {
                  case 1:
                              pal.start();
                     break;
                  case 2:
                              gcd.start();
                     break;
                  default:
                     System.out.println("Unknown value ");
                     choice = 0;
                    }
                
                in.close();
                converter.close();
            
            //while choice != 0, however, when it loops back up, where's choice? outside the function... I think it should be moved within the try block at least, because having it outside the function and not passing it into the method the second time.

            } while(choice != 0);
        } catch (Exception e) {
            System.out.println(e);
        }



I'm glad you figured it out now though :)

That's my understanding of the way you're explaining. I think of it in terms of set and get methods that you would use in C++, if the variable isn't in the method, it can't be used more than once typically, but locke is probably right, I'm probably making it more complicated than it's meant to be.

This post has been edited by circuspeanuts: 02 February 2010 - 01:48 PM

Was This Post Helpful? 1
  • +
  • -

#9 ludjer  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 186
  • Joined: 31-October 08

Re: simple java problem... i think

Posted 02 February 2010 - 01:48 PM

well currently this is my code and i took out the following from the other 2 classes
in.close();
converter.close();


here is my main code now it works fine
package prac0;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) {
        palindrome pal = new palindrome();
        GCD gcd = new GCD();
        int choice;
        try {
            InputStreamReader convertera = new InputStreamReader(System.in);
            BufferedReader ina = new BufferedReader(convertera);
            do {
                System.out.println("Enter 1 for palindrome checker");
                System.out.println("Enter 2 to work out greatest common divisor");
                choice = Integer.parseInt(ina.readLine());
                switch (choice) {
                  case 1:
                              pal.start();
                     break;
                  case 2:
                              gcd.start();
                     break;
                  default:
                     System.out.println("Unknown value ");
                     choice = 0;
                    }
            } while(choice != 0);
            ina.close();
            convertera.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

Was This Post Helpful? 0
  • +
  • -

#10 circuspeanuts  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 224
  • Joined: 11-April 08

Re: simple java problem... i think

Posted 02 February 2010 - 01:50 PM

hm, well I'm glad you figured it out, sorry I couldn't be of help :(
Was This Post Helpful? 0
  • +
  • -

#11 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: simple java problem... i think

Posted 02 February 2010 - 01:50 PM

Oh, right. You didn't initialize them in the loop anymore. And you didn't close them (or attempt to) multiple times. I misread the braces. Apologies.

Anyway, so yeah it works fine. Glad I could help (assuming I did). ;)
Was This Post Helpful? 0
  • +
  • -

#12 ludjer  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 186
  • Joined: 31-October 08

Re: simple java problem... i think

Posted 02 February 2010 - 01:53 PM

View Postcircuspeanuts, on 02 February 2010 - 09:50 PM, said:

hm, well I'm glad you figured it out, sorry I couldn't be of help :(

View PostLocke, on 02 February 2010 - 09:50 PM, said:

Oh, right. You didn't initialize them in the loop anymore. And you didn't close them (or attempt to) multiple times. I misread the braces. Apologies.

Anyway, so yeah it works fine. Glad I could help (assuming I did). ;)

both of you guys helped thanks alot got ides from both of you and just fooled a bit more around with the code
thanked both of you :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1