Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: squiggle

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Squiggle Homework help

    Program: Squiggle
    Write a program named Squiggle.java. The program will ask for the name of a text file. The program will then read lines from the text file.
    For each line read from the input file:
    • Print each line read. • Print each line in “squiggle” format:
    • The first line will consist of the 0th, 3rd, 6th, 9th, etc. characters from the line of text. Put two blank spaces after each character.
    • The second line will consist of the 1st, 4th, 7th, etc., characters from the line of text. Put one blank space at the start of the line and two blank spaces after each character.
    • The third line will consist of the 2nd, 5th, 8th, etc., characters from the line of text. Put two blank spaces at the start of the line and two blank spaces after each character.
    • Print a blank line.
    Here is a short example. The contents of sample1.txt: Four-score and seven years ago, our fathers brought forth on this continent a new nation, conceived in liberty and dedicated to the proposition that all men are created equal.
    The result of running the program using sample1.txt: Enter the name of the file: sample1.txt Four-score and seven years ago, our fathers brought forth on this Frcenseyra,ufhsrg r i
    o-o denesg rae ohftots usra v a oo trbutohnh
    continent a new nation, conceived in liberty and dedicated to the cte n tnccv bta dadoh
    oinaeni,oeeileyndit e nnt wao nidnir decett
    proposition that all men are created equal. ppiottlm ert u.
    rotnh lea eeea osi aa nrcadql
    Note: there is a blank line at the end of the output.
    Special cases:
    There are three special cases that your program will need to handle:

    1. When there is a blank line in the input file, your program will print one blank line to the output (nothing else).
    2. When there is a line with only a single character, your program will print the line. The squiggle will then consist of only the first line (not the other two lines, since there would be no characters on those lines). Print a blank line at the end.
    3. When there is a line with only two characters, your program will print the line. The squiggle will then consist of two lines (not three). Print a blank line at the end.
    Here is an example. The contents of sample2.txt: abcdefg abcdef
    abcde abcd abc ab
    a Q QR QRST QRSTUVWXYZ
    The result of running the program using sample2.txt: Enter the name of the file: sample2.txt abcdefg adg
    be cf
    abcdef ad
    be cf
    abcde ad
    be c
    abcd ad
    b
    c
    abc

    a
    ab a
    a a
    Q Q
    QR Q
    R
    b
    b
    c
    QRST QT
    R
    S
    QRSTUVWXYZ QTWZ
    RUX SVY
    Note one: there are two blank lines in the output before the line abcde. The first blank line is the blank line that follows the output for abcdef. The second blank line is the only output line for the blank line that is in sample2.txt.
    Note two: there is a blank line at the end of the output.
    We will provide some sample input files (including sample1.txt and sample2.txt from the examples above) and the correct output on D2L.

    Sample1.txt

    Four-score and seven years ago, our fathers brought forth on this
    continent a new nation, conceived in liberty and dedicated to the
    proposition that all men are created equal.

    Sample2.txt

    abcdefg
    abcdef

    abcde
    abcd
    abc
    ab
    a
    Q
    QR
    QRST
    QRSTUVWXYZ

    Sample3.txt

    0123456789
    01234
    0123
    012
    01
    0
    01
    012
    0123

    abcdefghijklmnopqrstuvwxyz
    ABCDEFGHIJKLMNOPQRSTUVWXYZ

    Sample4.txt

    Gettysburg Address
    November 19, 1863
    On the battlefield near Gettysburg, Pennsylvania

    Four-score and seven years ago, our fathers brought forth on this
    continent a new nation, conceived in liberty and dedicated to the
    proposition that all men are created equal.

    Now we are engaged in a great civil war, testing whether that nation
    or any nation so conceived and so dedicated can long endure. We are
    met on a great battle field of that war.

    We have come to dedicate a portion of that field, as a final
    resting place for those who here gave their lives that this nation
    might live. It is altogether fitting and proper that we should do this.

    But, in a larger sense, we can not dedicate - we can not consecrate - we
    can not hallow - this ground. The brave men, living and dead, who
    struggled here, have consecrated it, far above our poor power to
    add or detract. The world will little note, nor long remember, what
    we say here, but it can never forget what they did here.

    It is for us the living, rather, to be here dedicated to the unfinished
    work which they who fought here have thus far so nobly advanced. It is
    rather for us to be here dedicated to the great task remaining before
    us - that from these honored dead we take increased devotion to that
    cause for which they gave the last full measure of devotion - that we
    here highly resolve that these dead shall not have died in vain - that
    this nation, under God, shall have a new birth of freedom - and that
    government of the people, by the people, for the people, shall not
    perish from the earth.

    import java.util.Scanner;
    import java.io.*;
     
    public class Squiggle 
    {
    	public static void main( String[] args) throws FileNotFoundException
    	{
     
    		Scanner scanInput = new Scanner( System.in ); 
    		System.out.println( "Enter the name of a file: " ); //asks user for name of file
    		String filename = scanInput.nextLine(); //gets the file name
     
    		File myFile = new File(filename); //makes the filename that is input by the user into myFile(a file)
    		Scanner fileScan = new Scanner(myFile); 
     
     
    		while (fileScan.hasNextLine() ) {
    			System.out.println(fileScan.nextLine() );
     
    			String line1;
    			line1 = fileScan.nextLine(); 
    			char squiggle0 = line1.charAt(0);
    			char squiggle3 = line1.charAt(3);
    			char squiggle6 = line1.charAt(6);
    			char squiggle9 = line1.charAt(9);
    			char squiggle12 = line1.charAt(12);
    			char squiggle15 = line1.charAt(15);
    			String twoBlanks = "  ";
    			System.out.println( squiggle0 + twoBlanks + squiggle3 + twoBlanks +
    				squiggle6 + twoBlanks + squiggle9 + twoBlanks + squiggle12 + twoBlanks
    				+ squiggle15 + twoBlanks); //print statement
    			//end of first line's squiggle statement
     
    			String line2;  
    			line2 = fileScan.nextLine(); 
    			char squiggle1 = line2.charAt(1);
    			char squiggle4 = line2.charAt(4);
    			char squiggle7 = line2.charAt(7);
    			char squiggle10 = line2.charAt(10);
    			char squiggle13 = line2.charAt(13);
    			char squiggle16 = line2.charAt(16);
     
    			System.out.println( squiggle1 + twoBlanks + squiggle4 + twoBlanks +
    				squiggle7 + twoBlanks + squiggle10 + twoBlanks + squiggle13 + twoBlanks
    				+ squiggle16 + twoBlanks); //print statement
    			//end of second line's squiggle statement
     
    			String line3; 
    			line3 = fileScan.nextLine();
    			char squiggle2 = line3.charAt(2);
    			char squiggle5 = line3.charAt(5);
    			char squiggle8 = line3.charAt(8);
    			char squiggle11 = line3.charAt(11);
    			char squiggle14 = line3.charAt(14);
    			char squiggle17 = line3.charAt(17);
    			System.out.println( squiggle2 + twoBlanks + squiggle5 + twoBlanks +
    				squiggle8 + twoBlanks + squiggle11 + twoBlanks + squiggle14 + twoBlanks
    				+ squiggle17 + twoBlanks); //print statement
    			//end of third line's squiggle statement
     
     
     
    		} //end of while
    	}//end of main method
    }//end of
    Last edited by helloworld922; March 24th, 2010 at 11:01 PM.

  2. #2
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    Location
    the middle of the woods
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: squiggle

    ok , so if i understand this what u really need is ,a txt file reader , a loop that goes though 3 times taking the each letter out in gaps of 3 letters until the end of that line , and have some kind of spaceing to it

    heres what i think would work best
    String inTXT = file.readLine();
    while (inTXT != null){    // the inital printout 
       System.out.println(inTXT);
       inTXT = file.readLine();
    }
    // reaload the txt file 
     
    while (inTXT != null){  // this would give letters from 0 3 6 9 exc
       String squigleOne; 
       for ( int i=0; (i*3) <= inTXT.length(); i++){
          squigleOne += inTXT.getCharAt(i*3);
       }
       System.out.println(squigleOne);
       inTXT = file.readLine();
    }
    // reaload the txt file 
     
    while (inTXT != null){  // this would give letters from 1 4 7 10  exc
       String squigleOne;
       for ( int i=0; (i*3)<=inTXT.length(); i++){
          squigleOne += inTXT.getCharAt((i + 1)*3);
       }
       System.out.println(squigleOne);
       inTXT = file.readLine();
    }
    // reaload the txt file 
     
    while (inTXT != null){  // this would give letters from 2 5 8 11 exc
       String squigleOne;
       for ( int i=0; (i*3)<=inTXT.length(); i++){
          squigleOne += inTXT.getCharAt((i + 2)*3);
       }
       System.out.println(squigleOne);
       inTXT = file.readLine();
    }

    this is just a quick rough idea of a while and for loop to get it done , all the formatting would still need to be added
    plus its probably has a hand full of bugs in this version that would need fixed
    Programming: the art that fights back