Results 1 to 9 of 9
Thread: Newbie Coding Challenge Help
- 04-22-2012, 11:40 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 74
- Rep Power
- 0
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!
- 04-22-2012, 11:52 PM #2
Member
- Join Date
- Apr 2012
- Posts
- 7
- Rep Power
- 0
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);
-
Re: Newbie Coding Challenge Help
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?
- 04-23-2012, 12:24 AM #4
Member
- Join Date
- Apr 2012
- Posts
- 74
- Rep Power
- 0
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?
- 04-23-2012, 12:27 AM #5
Member
- Join Date
- Apr 2012
- Posts
- 7
- Rep Power
- 0
Re: Newbie Coding Challenge Help
try the code i had used
- 04-23-2012, 12:33 AM #6
Member
- Join Date
- Apr 2012
- Posts
- 74
- Rep Power
- 0
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)
- 04-23-2012, 12:43 AM #7
Member
- Join Date
- Apr 2012
- Posts
- 7
- Rep Power
- 0
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;
Java Code:public static void main(String[] args) throws IOException
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 errorsLast edited by shahrukh1; 04-23-2012 at 12:46 AM.
- 04-23-2012, 12:55 AM #8
Member
- Join Date
- Apr 2012
- Posts
- 74
- Rep Power
- 0
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 + " ");
}
}
- 04-23-2012, 09:23 AM #9
Re: Newbie Coding Challenge Help
If you're forever cleaning cobwebs, it's time to get rid of the spiders.
Similar Threads
-
A newbie to Java needing help on coding error
By Turo in forum New To JavaReplies: 5Last Post: 08-28-2011, 05:11 AM -
does gui coding in netbeans is just the same with coding without nebeans?
By maizuddin35 in forum NetBeansReplies: 4Last Post: 10-25-2010, 03:49 PM -
Coding newbie, please help
By skuzzie in forum New To JavaReplies: 12Last Post: 10-11-2010, 03:11 AM -
Challenge!
By pruttohutto in forum Forum LobbyReplies: 2Last Post: 04-28-2010, 04:15 PM
Bookmarks