Results 1 to 5 of 5
- 04-13-2009, 04:47 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 4
- Rep Power
- 0
Newbie having problems with for loop
Greetings to all,
I have an assignment to do but I do not yet fully understand how to display the correct data in a for loop.
The assignment is this:
1. Let the user write 10 words, each separated with ENTER/Return key
2. Display each word the other way around. Meaning display the last written word first!
This is the code I have made but the for loop aint working. It only shows the int 9 8 7 6 5 4 3 2 1 and not the words written.
Any ideas and suggestions would be appreciated.
I am aware that this assignment can be done with array however its not allowed =)Java Code:import java.io.*; import java.util.*; public class uppg3 { public static void main (String[] args) throws IOException { BufferedReader input = new BufferedReader (new InputStreamReader(System.in)); System.out.println("Write 10 words"); int rows = 0, words=0; while (rows <10){ System.out.print("Ord"+(rows+1)+"? "); String s = input.readLine(); if (s==null) break; rows++; StringTokenizer t = new StringTokenizer(s); words = words + t.countTokens(); } for (int i=9; i>=0; i--){ System.out.println(words); } }
- 04-13-2009, 05:41 PM #2
a few things.
First off are you required to use BufferedReader and StringTokenizer? You can accomplish the same thing easier using
and then just print the words out with a simple print statement.Java Code://declare 10 string variables Scanner in = new Scanner(System.in); word1 = in.nextLine(); word2 = in.nextLine(); ... word10 = in.nextLine();
//edit
Sorry I misread the part about having to use a for loop. disregard this for the most part.Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
- 04-13-2009, 09:20 PM #3
word is an int. you want to print the text which is s in your code.
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 04-13-2009, 10:07 PM #4
Also you can't compare strings using "==" you have to use the String method equals():
I hope the above will work.Java Code:if (s.equals(null))
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 04-13-2009, 11:52 PM #5
Why don't you just modify a buffer string?
Something like
Java Code:public String revers() throws IOException { BufferedReader input = new BufferedReader (new InputStreamReader(System.in)); String buffer = ""; String inputString = ""; for (int i = 0; i <= 10; i++) { inputString = input.readLine(); if(inputString != null) buffer = inputString + " " + buffer; } return buffer; }Though in most cases this is true, I'm pretty sure that the "==" operand compares the reference. So if s references a null object it "s == null" should return true. If your comparing two strings it's best to use a deep comparison (String.equals).Also you can't compare strings using "==" you have to use the String method equals():
Mr. BeansLast edited by Mr.Beans; 04-13-2009 at 11:54 PM.
Similar Threads
-
Newbie Help
By mattkid in forum New To JavaReplies: 4Last Post: 03-25-2009, 04:55 AM -
I am newbie
By Seoplanner in forum IntroductionsReplies: 0Last Post: 11-11-2008, 01:22 PM -
newbie needs help...
By vicky08 in forum New To JavaReplies: 2Last Post: 03-31-2008, 04:26 PM -
Newbie
By CSnoob87 in forum IntroductionsReplies: 2Last Post: 02-18-2008, 08:49 AM -
Problems with while loop
By Albert in forum New To JavaReplies: 2Last Post: 07-04-2007, 07:19 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks