Results 1 to 4 of 4
  1. #1
    Zahara is offline Member
    Join Date
    Feb 2012
    Location
    Scotland, United Kingdom
    Posts
    14
    Rep Power
    0

    Lightbulb First homework and I'm new to Java and new to programming itself, Plz help :(

    Hey!

    Just to let you know, I have taken up a course called Structures and Algorithms and I haven't done the pre-requiste :( I'm gonna start to learn from the basics.. But I have got to complete my homework by tonight and I'm confused :S

    Right, here's my question -

    Write a program (in a class called TextTest) that repeatedly prompts for and reads in a line of text that consists of one or more words. The program should output each word on a separate line and then the total number of words in the text. The program should terminate when the user responds with no to a prompt to repeat. The response is not case sensitive so a response of NO should also terminate the program.
    e.g. if the text is
    this is a test
    then the output should be
    this
    is
    a
    test
    number of words: 4

    My Answer:


    import java.io.*;


    public class TextTest {

    /**
    * @param args
    */
    private static BufferedReader stdin = new BufferedReader(new InputStreamReader( System.in ));

    public static void main(String arg[]) throws IOException {
    // TODO Auto-generated method stub


    name=Input.getString("input text: ");
    name=Input.getString("input text: ");
    name=Input.getString("input text: ");
    name=Input.getString("input text: ");

    String input = stdin.readLine();
    String[] arr = input.split(" ");
    int length = arr.length;
    System.out.println ("number of words:" +length);
    }

    }

    And from this I dont see the right output for the number of words, It just tells me the number of words for the last text that was been input and doesn't count for all four lines :( Plz help me with the right code, so that I could get the output as number of words : 4 and not 1 :)
    And also need to find a way to get the "no" formula into it :/

    The output it shows is below:

    input text: this
    input text: is
    input text: a
    input text: test

    number of words:1

    Any help would be appreciated!

    Thanks
    Zahara

  2. #2
    Ampzor is offline Member
    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0

    Default Re: First homework and I'm new to Java and new to programming itself, Plz help :(

    Quote Originally Posted by Zahara View Post
    The program should terminate when the user responds with no to a prompt to repeat. The response is not case sensitive so a response of NO should also terminate the program.
    If I'm correct, your code should have some finish method with an if statement. What that would look like, I'm not entirely sure. Try to google if statements if you don't know how to write them.

    As for the initial problem, try using the JOptionPane method, it's a lot easier then IO's. It also has an output, message, and input method. Google can help with thoughs, or even someone on the forums.

  3. #3
    bnson is offline Artemis
    Join Date
    Jul 2011
    Posts
    26
    Rep Power
    0

    Default Re: First homework and I'm new to Java and new to programming itself, Plz help :(

    Reference:
    String
    Strings (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
    Array
    Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
    While
    The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
    For
    The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
    ---------------------------------------------------------------
    The first question "how create loop input." and stop it.
    Java Code:
    	while (!tmp.equals("stop")) // Loop input and condition stop input.
    	{
    		tmp = stdin.readLine();
    		input += tmp;	    		    	
    	}
    Next question "how to store all input to string variable"
    Java Code:
    string = input1 + input2 + input3 + .....;
    You should referenced how to use String, Array and Loop(while,for,) before doing this exercise.

    Good luck

  4. #4
    Zahara is offline Member
    Join Date
    Feb 2012
    Location
    Scotland, United Kingdom
    Posts
    14
    Rep Power
    0

    Default Re: First homework and I'm new to Java and new to programming itself, Plz help :(

    Thanks for the info guys! I'm trying to learn java from the basics, and I hope I pass this module :S

Similar Threads

  1. Need HW help on java homework
    By sourlemons in forum New To Java
    Replies: 3
    Last Post: 10-16-2010, 06:44 AM
  2. java homework help
    By jenniferrlie in forum New To Java
    Replies: 5
    Last Post: 09-22-2009, 09:12 PM
  3. programming homework
    By pinkdiamondgail in forum Advanced Java
    Replies: 3
    Last Post: 04-15-2009, 02:10 PM
  4. programming homework
    By pinkdiamondgail in forum Advanced Java
    Replies: 2
    Last Post: 04-15-2009, 12:06 AM
  5. LF: Homework help with Java
    By excurssion in forum New To Java
    Replies: 2
    Last Post: 10-17-2008, 07:00 AM

Tags for this Thread

Posting Permissions

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