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 :/ :(think):
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
Re: First homework and I'm new to Java and new to programming itself, Plz help :(
Quote:
Originally Posted by
Zahara
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.
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.
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"
Code:
string = input1 + input2 + input3 + .....;
You should referenced how to use String, Array and Loop(while,for,) before doing this exercise.
Good luck
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