-1

I have the following code:

import java.util.Random; 
import java.util.Scanner;  
import java.io.*;
public class Main
{
public static void main(String[] args)
{
    try
    {
  FileInputStream in = null;
  FileOutputStream outp = null;

  in = new FileInputStream("file.txt");


  Random rand = new Random(); 

  int x = rand.nextInt(9);
  int guess; 
  int count=0;

  Scanner input = new Scanner(System.in);

  int temp;
  char znak;
  int i=0;
  while(i<1000)
  {
    temp=rand.nextInt(94)+32;
    znak=(char)temp;
    in.write((int)znak);
    i++;
  }
  i=0;
  in.close();

  outp = new FileOutputStream("file.txt");
  while(i<1000)
  {
    znak = (char)outp.read();
    System.out.print(znak);
    i++;
  }

    }
    catch(Exception e)
    {
            System.out.print("Input error");
            return;

    }

}

}

I apologise for pasting it all in instead of only the relevant parts, but I'm still new to Java and can't pinpoint where exactly the problem lies. When I attempt to compile it, I get the following errors:

Main.java:31: error: cannot find symbol
        in.write((int)znak);
          ^
  symbol:   method write(int)
  location: variable in of type FileInputStream
  Main.java:40: error: cannot find symbol
        znak = (char)outp.read();
                         ^
  symbol:   method read()
  location: variable outp of type FileOutputStream

What could be causing the problem here? From what I've gathered, these are normally returned when I try to use an undefined variable, but I do define them before.

Panzer
  • 47
  • 7
  • 1
    Does this answer your question? [What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?](https://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-or-cannot-resolve-symbol-error-mean) – Savior Apr 10 '20 at 20:54

1 Answers1

3

InputStreams aren't writable, and OutputStreams aren't readable. You have the order of operations you want to do backwards.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • Ah, thank you! I figured that "InputStreams" means input into file. – Panzer Apr 10 '20 at 20:57
  • After fixing these, the program always catches the exception immediately. Are there any other obvious mistakes that you can see at a glance? – Panzer Apr 10 '20 at 21:01