Results 1 to 4 of 4
- 02-08-2012, 03:04 PM #1
Member
- Join Date
- Feb 2012
- Location
- Scotland, United Kingdom
- Posts
- 14
- Rep Power
- 0
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 :/.gif)
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
- 02-08-2012, 03:21 PM #2
Member
- Join Date
- Feb 2012
- Posts
- 6
- Rep Power
- 0
Re: First homework and I'm new to Java and new to programming itself, Plz help :(
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.
- 02-08-2012, 04:38 PM #3
Artemis
- Join Date
- Jul 2011
- Posts
- 26
- Rep Power
- 0
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.
Next question "how to store all input to string variable"Java Code:while (!tmp.equals("stop")) // Loop input and condition stop input. { tmp = stdin.readLine(); input += tmp; }
You should referenced how to use String, Array and Loop(while,for,) before doing this exercise.Java Code:string = input1 + input2 + input3 + .....;
Good luck
- 02-11-2012, 12:45 AM #4
Member
- Join Date
- Feb 2012
- Location
- Scotland, United Kingdom
- Posts
- 14
- Rep Power
- 0
Similar Threads
-
Need HW help on java homework
By sourlemons in forum New To JavaReplies: 3Last Post: 10-16-2010, 05:44 AM -
java homework help
By jenniferrlie in forum New To JavaReplies: 5Last Post: 09-22-2009, 08:12 PM -
programming homework
By pinkdiamondgail in forum Advanced JavaReplies: 3Last Post: 04-15-2009, 01:10 PM -
programming homework
By pinkdiamondgail in forum Advanced JavaReplies: 2Last Post: 04-14-2009, 11:06 PM -
LF: Homework help with Java
By excurssion in forum New To JavaReplies: 2Last Post: 10-17-2008, 06:00 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks