Results 1 to 5 of 5
  1. #1
    Dannii is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Default 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.

    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);
    		}
    }
    I am aware that this assignment can be done with array however its not allowed =)

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    a few things.
    First off are you required to use BufferedReader and StringTokenizer? You can accomplish the same thing easier using
    Java Code:
    //declare 10 string variables
    Scanner in = new Scanner(System.in);
    word1 = in.nextLine();
    word2 = in.nextLine();
    ...
    word10 = in.nextLine();
    and then just print the words out with a simple print statement.

    //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.

  3. #3
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    word is an int. you want to print the text which is s in your code.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    Also you can't compare strings using "==" you have to use the String method equals():
    Java Code:
    if (s.equals(null))
    I hope the above will work.
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    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;
        }
    Also you can't compare strings using "==" you have to use the String method equals():
    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).

    Mr. Beans
    Last edited by Mr.Beans; 04-13-2009 at 11:54 PM.

Similar Threads

  1. Newbie Help
    By mattkid in forum New To Java
    Replies: 4
    Last Post: 03-25-2009, 04:55 AM
  2. I am newbie
    By Seoplanner in forum Introductions
    Replies: 0
    Last Post: 11-11-2008, 01:22 PM
  3. newbie needs help...
    By vicky08 in forum New To Java
    Replies: 2
    Last Post: 03-31-2008, 04:26 PM
  4. Newbie
    By CSnoob87 in forum Introductions
    Replies: 2
    Last Post: 02-18-2008, 08:49 AM
  5. Problems with while loop
    By Albert in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 07:19 AM

Posting Permissions

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