Results 1 to 11 of 11
  1. #1
    drucey is offline Member
    Join Date
    Oct 2010
    Posts
    24
    Rep Power
    0

    Default Couple hopefully simple things.

    Hi guys, been doing my assignment for a long time now and really come on doing it. I basically have to read in information from a file and do different things to it. I have created a variable size array using loops - made it print out the first 20 results using loops, my current issue is:

    To promt the teacher to move onto the next task:


    Scanner scan2 = new Scanner(System.in);
    yorn = scan2.next();

    if (yorn == "n")
    {
    System.out.println("No selected, program terminating");
    }

    yorn is set a String but doesnt seem to recognise it at all. The promt shows but seems to execute the else statement no matter what.

    Secondly (The most frustrating for me) is i am using similar code to manipulate the array to print out every other line in capital letters. I've come up with this in the else statement:

    else
    {
    while ((count < 20)&&(count % 2 == 0))
    {
    System.out.println(pageLine [count].toUpperCase());
    count ++;
    }

    The problem it it only prints the first line. I really hope that this is simple because it makes sense to me and compiles. Maybe something small.

    SOrry about the abundance of text and bombarding you guys with something so trivial!

    Chris

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    yorn is set a String but doesnt seem to recognise it at all. The promt shows but seems to execute the else statement no matter what
    strings = objects --> use the equals method!

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default

    Use .equals, not ==, when dealing with Strings. == compares the actual instances. The equals method compares the content.

    What happens when count is 1?

  4. #4
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    You'd be better off using a for loop:
    Java Code:
    for(int x = 1; x < 20; x += 2) {
    ...
    }

  5. #5
    drucey is offline Member
    Join Date
    Oct 2010
    Posts
    24
    Rep Power
    0

    Default

    Thanx for the replies!

    so it would look like (yorn.equals "N")? got a compile error with that.

    And sorry but i dont fully understand that if loop?

    Basically when count is 1 nothing is meant to happen, the outcome is hopefully [0] [2] [4] ect will get printed on screen using .toUpperCase

  6. #6
    drucey is offline Member
    Join Date
    Oct 2010
    Posts
    24
    Rep Power
    0

    Default

    Ah i managed to get the .equals working great. Now just to try and work out why the array wont print. I used a similar method to print the first twenty lines but using scan.nextLine:

    while (scan1.hasNextLine())
    {
    pageLine[count] = scan1.nextLine();
    count++;
    }

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Can you show your latest code? But please use code tags if you do so (see the first link in my signature links).

  8. #8
    drucey is offline Member
    Join Date
    Oct 2010
    Posts
    24
    Rep Power
    0

    Default

    while ((count < 20)&&(count % 2 == 0))
    {
    System.out.println(pageLine [count].toUpperCase());
    count ++;
    Last edited by drucey; 11-25-2010 at 10:53 PM.

  9. #9
    zjames is offline Member
    Join Date
    Nov 2010
    Posts
    20
    Rep Power
    0

    Default

    You have a while loop controlling the printing, which means that as soon as
    (count % 2 == 0)
    evaluates to false, the loop will terminate. This will happen on the second item. Try something like
    Java Code:
     while ((count < 20))
    			{ if (count % 2 == 0) {
    				System.out.println(pageLine [count].toUpperCase());
    				}
                                      count ++;
                                           
    			}

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Iron Lion suggests you use a for loop instead of a while loop in his post above. I suggest you follow his suggestions.

  11. #11
    drucey is offline Member
    Join Date
    Oct 2010
    Posts
    24
    Rep Power
    0

    Default

    Ah i see, i neglected to notice that the loop would terminate!

    Thats amazing, thanks so much for all your help.
    Last edited by drucey; 11-18-2010 at 08:58 PM.

Similar Threads

  1. Move things in Applet
    By Chasingxsuns in forum New To Java
    Replies: 6
    Last Post: 02-19-2014, 08:21 AM
  2. Checking if 2 things are false
    By ile4 in forum New To Java
    Replies: 4
    Last Post: 11-16-2010, 10:40 AM
  3. XML, and other things.
    By Tortex in forum New To Java
    Replies: 5
    Last Post: 03-28-2010, 05:53 PM
  4. so whats going on? (things aren't showing up)
    By Adrien in forum AWT / Swing
    Replies: 9
    Last Post: 02-20-2010, 07:22 PM
  5. Program That Can Do Things On The Internet
    By IronLegion in forum Eclipse
    Replies: 1
    Last Post: 12-09-2009, 08:06 PM

Posting Permissions

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