Results 1 to 4 of 4

Thread: Squeeze Project

  1. #1
    APCompSci is offline Member
    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Default Squeeze Project

    Hey guys,

    My teacher assigned me a project where I'm basically reading in a file, extracting the spaces at the beginning of each line, and replacing them with an integer representing the amount of original spaces there were. For example, if I were to enter

    " The quick brown fox"

    The output should be...

    "(5)The quick brown fox"

    Could someone please explain how I would go about doing this? So far, I've tried to use arraylists separating line by line... can anyone tell me if this is a good approach?
    Java Code:
    import java.util.ArrayList;
    import java.util.Scanner;
    import java.io.IOException;
    import java.io.File;
    import java.io.FileReader;
    
    public class Squeeze {
        
        ArrayList<String> lines = new ArrayList<String>();
        
        public ArrayList<String> input() {
            int line = 0;
    
            try {
                File file = new File("textfile.txt");
                FileReader fR = new FileReader(file);
                Scanner input = new Scanner(fR);
                while (input.hasNextLine()) {
                    lines.add(line, input.nextLine());
                    line++;
                }
                
            }
            
            catch (IOException e) {
                e.printStackTrace();
            }
                return lines;
        }
        
        public void newText() {
            
            int[] spaceCount = new int[100];
            for (int i = 0; i < lines.size(); i++) {
                for (int k = 0; k < lines.size(); k++) {
                    if (lines.get(i).substring(k, k+1) == " ") {
                        spaceCount[i]++;
                    }
                    
                    else {
                        break;
                    }
                }
            }


    ^^^ I don't want to go any further until I know if my idea is any good
    Last edited by Norm; 02-08-2012 at 03:37 AM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: Squeeze Project

    extracting the spaces at the beginning of each line
    " The quick brown fox"

    The output should be...

    "(5)The quick brown fox"
    I only see 1 space at the beginning of the line shown.

    You should use the equals() method to compare Strings, not the == operator.

  3. #3
    APCompSci is offline Member
    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Default Re: Squeeze Project

    Sorry, I guess this forum took away my spaces at the beginning of that line

    (Kinda funny, since that's exactly what I'm trying to get this project to do)

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: Squeeze Project

    Right I understand.

    Did you try the equals method?

Similar Threads

  1. Creating a project in eclipse from existing project
    By Suraiya in forum New To Java
    Replies: 1
    Last Post: 10-08-2011, 09:14 AM
  2. How to convert from Ant project to maven project?
    By jiapei100 in forum New To Java
    Replies: 0
    Last Post: 06-08-2011, 10:01 AM
  3. Adding a project to an existing project
    By Seijuro in forum NetBeans
    Replies: 4
    Last Post: 08-08-2010, 10:15 AM
  4. open existing project project ..
    By itaipee in forum Eclipse
    Replies: 1
    Last Post: 12-28-2008, 08:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •