0

So I am just figuring out methods to finish my java project and i am having trouble skipping over white space. I got pretty much all the methods working except this one. If anyone can help me understand how to get the FileInputStream to skip over whitespace, then I can finally finish this project. Just stuck on this one part. This purpose of this class(not finished just working on methods then cleaning it up) is to take in a file(ASCII text art) and "zoom in" by a perfect square. Thank you

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

public class makeBiggerTesting {
    public static void main(String[] args) {
        String fileName = "";
        Scanner keyBoard = new Scanner(System.in);
        System.out.print("Enter fileName: ");
        fileName = keyBoard.nextLine();

        try {
            int zoom = 9;
            int i = (int) Math.sqrt((double) zoom);
            //Scanner fileScan = new Scanner(fileName);
            FileInputStream inputStream = new FileInputStream(fileName);
//          while(inputStream.available() > 0) {
//              char input = (char) inputStream.read();
//              System.out.print((char) input);
//              
//              }
//          System.out.println("");
            while(inputStream.available() > 0) {
            char input = (char) inputStream.read();

            for(int row = 1; row <= i; row++) {

                for(int col = 1; col <= i; col++) {
                    System.out.print((char) input);
                }
                System.out.println("");
            }
            }

        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

}

this is what one of the file looks like:

@ @

output:
@@@ @@@
@@@ @@@
@@@ @@@

is what it is suppose to be. but i get

@@@ (Bunch of space)@@@
@@@                 @@@
@@@                 @@@

When i run the program it turns into a big mess so i think its also zooming the white space. I could be wrong though

  • Please post an example input and output. I don't think your file is "10 11" – Mark Sholund Mar 22 '19 at 01:16
  • Your right, is this new one better? – Someone needs help Mar 22 '19 at 01:25
  • That seems to be better. – Mark Sholund Mar 22 '19 at 01:27
  • You can omit by, remembering the last output, if it is space and you are now outputting a space, ignore the current output. But I don't think your thought of producing the question is right, what if the "image" containing space initially? Is it an X - Y question? – Geno Chen Mar 22 '19 at 01:28
  • To start, do **not** use `InputStream.available()`, it does **not** do what you think it does. To find out if you have reached the end of the stream, check the return value of the `read()` call and compare it to `-1`. https://stackoverflow.com/questions/3695372/what-does-inputstream-available-do-in-java – Erwin Bolwidt Mar 22 '19 at 02:13

1 Answers1

1

I am not sure if I understood your problem entirely. Are you after something like following

public class makeBigger {
    public static void main(String[] args) {
        Scanner keyBoard = new Scanner(System.in);
        System.out.print("Enter fileName: ");
        String fileName = keyBoard.nextLine();
        int zoom = 9;
        int i = (int) Math.sqrt((double) zoom);         
        try(FileInputStream inputStream = new FileInputStream(fileName)){
            int input = -1;
            String line = "";
            while ((input = inputStream.read()) > 0 ) { 
                if((char)input == ' ') {
                    line += (char) input;
                }else if(input == 10 || input == 13) {                      
                    for (int col = 1; col <= i; col++) {                                
                        System.out.println(line);                           
                    }   
                    line = "";
                }else {
                    line += new String(new char[i]).replace("\0", ""+(char)input);
                }                       
            }
            if(! "".equals(line)) {
                for (int col = 1; col <= i; col++) {                                
                    System.out.println(line);                           
                }   
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {               
            e.printStackTrace();
        }
    }
}
Gro
  • 1,613
  • 1
  • 13
  • 19