Search for more details......

Showing posts with label java file reading and patter printing. Show all posts
Showing posts with label java file reading and patter printing. Show all posts

java file reading and patter printing

Below code example shows how to read file and prints patterns according to the file data.

A file that contains following data.

*5
/5
@2 
#3
#4
@1


You want to print the following pattern style.

*****
/////
@@
###
####
@



try {
            String filename="file.txt";
            BufferedReader br=new BufferedReader(new FileReader(filename));
      
            String line="";
            char tmpc=0;
            int tmpi=0;
            while((line=br.readLine())!=null){
              
                if(line.length()==2){
                    tmpc=line.charAt(0);
                    tmpi=Integer.parseInt(""+line.charAt(1));
                  
                    for(int i=0;i<tmpi;i++){
                      
                        System.out.println(tmpc);
                      
                    }
                    System.out.println();
                  
                }
            }
      
        } catch (FileNotFoundException e) {
          
            e.printStackTrace();
        } catch (IOException e) {
          
            e.printStackTrace();
        }

In this code example set filename variable to where your file to read.