Results 1 to 2 of 2
  1. #1
    Mode is offline Member
    Join Date
    Dec 2010
    Posts
    1
    Rep Power
    0

    Default Scanner class in my for loop

    Alright I'm relatively new to Java and I'm nearly done with my first Java class.
    I am currently working on my final and I have run into a snag. I probably knew the answer to this issue at one point, but have forgotten it.
    Can someone explain to me why this code won't loop despite me sending the for loop the proper keyboard input via Scanner p so that the boolean would be true. I decided to make a small sample program so that no one will have to read through the pile of coding since my final is nearly complete I just need to loop it.
    I would really appreciate fast help because my final and it is due in 20 minutes.

    int j;
    String i = "y";
    for(j = 0; i == "y"; j++)
    {
    Scanner p = new Scanner(System.in);
    System.out.println("y/n?");
    i = p.nextLine();
    }

  2. #2
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    450
    Rep Power
    7

    Default

    PHP Code:
    for(j = 0; i.equals("y"); j++)
    {
    Scanner p = new Scanner(System.in);
    System.out.println("y/n?");
    i = p.nextLine();
    }
    Hope this helps
    Mak
    (Living @ Virtual World)

Similar Threads

  1. Scanner, while loop and sorting arrays/string?
    By RSYR in forum New To Java
    Replies: 10
    Last Post: 04-20-2011, 07:13 PM
  2. Scanner Loop
    By Phenomena in forum New To Java
    Replies: 2
    Last Post: 04-01-2010, 07:47 PM
  3. Problem with while loop and the scanner method
    By hhh80p in forum New To Java
    Replies: 2
    Last Post: 02-28-2010, 12:47 PM
  4. Problem with scanner in loop
    By grifan526 in forum New To Java
    Replies: 2
    Last Post: 07-21-2009, 01:06 AM
  5. Scanner-While Loop
    By hyunski in forum New To Java
    Replies: 2
    Last Post: 03-12-2009, 03:15 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
  •