Skip to main content
Fix minor typo, highlight task description, add [beginner] tag
Source Link

Apologies if this is is the wrong place. I've been doing a Java for beginners course for a few weeks  (2 hours a week), we had an exam question.

Take a sentence as an input from a user for twitter. Using only String Arrays and String Buffers (do not use split() or any other methods not covered in class yet). Separate the sentence into words and add to an array. Then change any instance of the word "cannot" with "can't". Must use an instantiable class and all relevant set, compute, get methods covered. Comment all code to show you understand what everything you've used is doing.

Take a sentence as an input from a user for twitter. Using only String Arrays and String Buffers (do not use split() or any other methods not covered in class yet). Separate the sentence into words and add to an array. Then change any instance of the word "cannot" with "can't". Must use an instantiable class and all relevant set, compute, get methods covered. Comment all code to show you understand what everything you've used is doing.

Apologies if this is is the wrong place. I've been doing a Java for beginners course for a few weeks(2 hours a week), we had an exam question.

Take a sentence as an input from a user for twitter. Using only String Arrays and String Buffers (do not use split() or any other methods not covered in class yet). Separate the sentence into words and add to an array. Then change any instance of the word "cannot" with "can't". Must use an instantiable class and all relevant set, compute, get methods covered. Comment all code to show you understand what everything you've used is doing.

I've been doing a Java for beginners course for a few weeks  (2 hours a week), we had an exam question.

Take a sentence as an input from a user for twitter. Using only String Arrays and String Buffers (do not use split() or any other methods not covered in class yet). Separate the sentence into words and add to an array. Then change any instance of the word "cannot" with "can't". Must use an instantiable class and all relevant set, compute, get methods covered. Comment all code to show you understand what everything you've used is doing.

Source Link

Splitting a String into words and add to an array, Only using StringBuffer and String Array

Apologies if this is is the wrong place. I've been doing a Java for beginners course for a few weeks(2 hours a week), we had an exam question.

Take a sentence as an input from a user for twitter. Using only String Arrays and String Buffers (do not use split() or any other methods not covered in class yet). Separate the sentence into words and add to an array. Then change any instance of the word "cannot" with "can't". Must use an instantiable class and all relevant set, compute, get methods covered. Comment all code to show you understand what everything you've used is doing.

This is the code I submitted (it works), looking for feedback before the results come out to see what I can expect.

Thanks

App Class enter code here

public class tweetApp{
    public static void main(String arg[]){
        //decalre variables
        String sentence;
        //create an array for the new sentence
        String [] newSentence;

        //instaniate classes 
        tweet myTweet;
        Scanner input;
        //creat classes
        myTweet = new tweet();
        input = new Scanner(System.in);


        System.out.print("Enter a sentence: ");
        sentence = input.nextLine();

        //set method for the sentence inputted from the user
        myTweet.setSentence(sentence);
        //compute method for chaging the sentence given by the user
        myTweet.computeSentence();
        //set the array to the new changed sentence
        newSentence = myTweet.getNewSentence();
        //output each section of the array and a space, to print out the entire message
        for(int y = 0; y < newSentence.length; y++){
            System.out.print(newSentence[y]+" ");

        }
        //end the program by adding a blank print line to make sure the command line looks ok.
        System.out.println();
    }
}

enter code here

Instantiable class

enter code here
public class tweet{
    //declare variables
    private String sentence;
    private int strlen;
    private StringBuffer word;
    private String newSentence[];
    private String newWord;
    private int wordCount;
    //create constuctor
    public tweet(){
        sentence = " ";
        strlen = 0;
        word = new StringBuffer();
        wordCount = 1; //starting at 1 if user enters 1 word
        newSentence = new String[wordCount];
        newWord = " ";  
    }
    //set method
    public void setSentence(String sentence){
        this.sentence = sentence;
    }
    //compute method
    public void computeSentence(){
        strlen = sentence.length();//get length of sentence 
        for(int x = 0; x < strlen; x++){//transverse the string,
            if(sentence.charAt(x)==' '){//calculate word count  
                wordCount = wordCount +1;
            }
        }
        newSentence = new String[wordCount]; //use word count to set size of the array 
        for(int j = 0; j < newSentence.length; j++){//transverse the array,
            for(int i = 0; i< strlen; i++){//transverse string,
                if(sentence.charAt(i) == ' '){//check if char is a space,
                    j = j+1;//if so,increment j by 1
                    word = new StringBuffer();//start a new buffer
                }
                else{
                    word.append(sentence.charAt(i));//char at i, append to buffer,  
                    newWord = word.toString();//convert buffer to string,
                    newSentence[j] = newWord;//add to j'th place of array
                    }
            }
        }
        for(int x = 0; x < newSentence.length; x++){//change any instance of cannot to can't
            if(newSentence[x].equalsIgnoreCase("cannot")){
                newSentence[x] = "can't";
            }
        }
    }
        //get method, returning new sentence to app class
        public String[] getNewSentence(){
            return newSentence;
        }
    }