Results 1 to 9 of 9
  1. #1
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Newbie Coding Challenge Help

    Hello, I'm brand new to Java and I've been reading and watching many tutorials and now trying to do some basic coding on my own from this website: JavaRanch Cattle Drive.

    The first challenge is to write a program that gets input from the user (the users name) and then prints it 100 times. I've been trying to set up a loop, but I can't get it to work properly (it just prints the name once and stops).

    Here is my code:


    import java.util.Scanner;
    public class Gertrude {
    public static void main(String args[]){

    Scanner name = new Scanner(System.in);

    int i = 0;

    while(i < 101 )

    {
    System.out.println(name.nextLine());
    i++;

    }
    }

    }

    What am I doing wrong?

    Thanks!

  2. #2
    shahrukh1 is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: Newbie Coding Challenge Help

    try this
    Java Code:
    BufferedReader reader;
    
            reader = new BufferedReader(new InputStreamReader(System.in));
    
            System.out.println("What is your move?");
    
            String name = reader.readLine();
    
    for (int i =0; i<101;i++)
    System.out.println(name);

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

    Default Re: Newbie Coding Challenge Help

    Quote Originally Posted by Zigster View Post
    Hello, I'm brand new to Java and I've been reading and watching many tutorials and now trying to do some basic coding on my own from this website: JavaRanch Cattle Drive.
    Welcome to the Java-Forms.org!

    The first challenge is to write a program that gets input from the user (the users name) and then prints it 100 times. I've been trying to set up a loop, but I can't get it to work properly (it just prints the name once and stops).
    What am I doing wrong?
    Since you know how many times that you want to loop before the loop begins, use a for loop, not a while loop. Get your String from the user before the loop.

  4. #4
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: Newbie Coding Challenge Help

    Thanks guys!

    So, with this code:

    import java.util.Scanner;
    public class Gertrude {
    public static void main(String args[]){

    Scanner name = new Scanner(System.in);

    for (int i = 0; i < 101; i++)
    System.out.println(name);
    }
    }

    I get: java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q?\E][infinity string=\Q?\E]

    100 times, lol. Still not really sure what's going on?

  5. #5
    shahrukh1 is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: Newbie Coding Challenge Help

    try the code i had used

  6. #6
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: Newbie Coding Challenge Help

    So I tried that code before:

    public class test2 {
    public static void main(String args[]){
    BufferedReader reader;

    reader = new BufferedReader(new InputStreamReader(System.in));

    System.out.println("What is your move?");

    String name = reader.readLine();

    for (int i =0; i<101;i++)
    System.out.println(name);
    }
    }

    And I get:


    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    BufferedReader cannot be resolved to a type
    BufferedReader cannot be resolved to a type
    InputStreamReader cannot be resolved to a type
    Syntax error, insert "}" to complete ClassBody

    at test2.main(test2.java:4)

  7. #7
    shahrukh1 is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: Newbie Coding Challenge Help

    sorry, add this to the very top of your code,

    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    also, you main method make it like this
    Java Code:
    public static void main(String[] args) throws IOException
    you have to import the precompiled libraries for Buffered Reader (you may not understand now, but will understand for sure later)
    now the "throws IOException" i have so that if the reader encounters an "error" it just ignores it, for now this is fine, but later u will learn how to properly handle errors
    Last edited by shahrukh1; 04-23-2012 at 12:46 AM.

  8. #8
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: Newbie Coding Challenge Help

    Thanks, it works!

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;

    public class test2 {
    public static void main(String[] args) throws IOException{

    BufferedReader reader;

    reader = new BufferedReader(new InputStreamReader(System.in));

    System.out.println("What is your name?");

    String name = reader.readLine();

    for (int i =0; i<101;i++)
    System.out.print(name + " ");
    }
    }

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Newbie Coding Challenge Help

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. A newbie to Java needing help on coding error
    By Turo in forum New To Java
    Replies: 5
    Last Post: 08-28-2011, 05:11 AM
  2. Replies: 4
    Last Post: 10-25-2010, 03:49 PM
  3. Coding newbie, please help
    By skuzzie in forum New To Java
    Replies: 12
    Last Post: 10-11-2010, 03:11 AM
  4. Challenge!
    By pruttohutto in forum Forum Lobby
    Replies: 2
    Last Post: 04-28-2010, 04:15 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
  •