Java Homework Problem - Help Needed
OK, Here's a question assigned to our beginners Java class, (it's only the first week of class). Since it's only the first week, I wouldn't think the solution would be very complex. Anyway, here's the problem given, word for word...
"Given an int variable, datum, that has already been declared, write a few statements that read an integer value from standard input into this variable. "
Thanks a lot of you can give the answer.
[452 byte] By [
Jim_Wa] at [2007-10-3 4:29:05]

> Thanks a lot of you can give the answer.No.I can only give a push in the right direction.Look into java.io.BufferedReader, java.lang.Integer.parseInt and java.util.Scanner
> Thanks a lot of you can give the answer.Thanks for giving me the opportunity to do your homework. But seriously, what do you have so far. Someone might help if you get stuck.
> OK, Here's a question assigned to our beginners Java
> class, (it's only the first week of class). Since
> it's only the first week, I wouldn't think the
> solution would be very complex. Anyway, here's the
> problem given, word for word...
>
> "Given an int variable, datum, that has already been
> declared, write a few statements that read an integer
> value from standard input into this variable. "
>
> Thanks a lot of you can give the answer.
Read here: http://javaalmanac.com/egs/java.io/ReadFromStdIn.html
This is what I've tried
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
datum = in.readLine();
and
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
datum = Integer.parseInt(in.readLine());
Keep in my this is the first week of an intro course... I really wouldn't ecpect the solution to be overly complex... anyway, neither of these is correct. The lab is done through one of these online websites, they give you a box to write the solution and it just replys, correct or incorrect.
Jim_Wa at 2007-7-14 22:32:08 >

Do you mean like a [url http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTextField.html]JTextField[/url]?
try
{
BufferedReader in = new BufferedReader( new InputStreamReader(
System.in ) );
datum = Integer.parseInt( in.readLine() );
}
catch ( Exception e )
{
e.printStackTrace();
}
> The lab is done through one of these online
> websites, they give you a box to write the solution
> and it just replys, correct or incorrect.
I don't think I am interested in helping given these parameters.
The first thing you said is wrong. What's wrong with the second I don't know unless it is lack of try/catch
And again there is the Scanner as another solution.
If none of these "work" then I don't know what to tell you. I don't think I like an automated checking system. Especially when we don't know the complete parameters of your assignment, or what needs to be submittied, etc.
> If none of these "work" then I don't know what to
> tell you. I don't think I like an automated checking
> system. Especially when we don't know the complete
> parameters of your assignment, or what needs to be
> submittied, etc.
I think hes looking to get input from a JTextField, not from the command line.
werdtry:Scanner sc = new Scanner(System.in);datum = sc.nextInt();In any case turning in stuff like this is really rather lame. I can see how it could make labs easier to grade, but still.
No, he's not. He's putting his lab solution into a text box for automatic grading. the problem he has to solve is getting an int from the standard input.
> > If none of these "work" then I don't know what to
> > tell you. I don't think I like an automated
> checking
> > system. Especially when we don't know the complete
> > parameters of your assignment, or what needs to be
> > submittied, etc.
>
> I think hes looking to get input from a JTextField,
> not from the command line.
I disagree. The first posts says
"Given an int variable, datum, that has already been declared, write a few statements that read an integer value from standard input into this variable. "
> I think hes looking to get input from a JTextField, not from the command line.
I don't think JTextField (or even JOptionPane) counts as "standard input", as the OP described what he needed. Using JTextField or JOptionPane in first week of a first Java course sounds unlikely (I hope they don't do that first!).
MLRona at 2007-7-14 22:32:08 >

And if standard input means Swing then the guy needs a new school...
I see. I misunderstood his comment.
> I think hes looking to get input from a JTextField,
> not from the command line.
Yes he is (IMHO) given the problem statement:
"Given an int variable, datum, that has already been declared, write a
few statements that read an integer value from standard input into this
variable. "
Just the solution must be entered using a JTextArea:
"The lab is done through one of these online websites, they give you a
box to write the solution and it just replys, correct or incorrect"
If none of the Integer.parseInt(bufferedReader.readLine()) thingies are
considered to be correct, I guess the "correct" answer must've something
to do with that darn Scanner class.
kind regards,
Jos
JosAHa at 2007-7-21 10:32:24 >

Fourth time's a charm. :D
Maybe he should read the line first (into a String variable), and then parse it separately into an int.He probably isn't expected to do exceptions in the first week of class, either.
MLRona at 2007-7-21 10:32:24 >

I did get the correct solution. I don't have it now, but I think someone posted it earlier in the thread. It was two lines, using the scanner class, no need to use BufferedInputReader. Thanks for your attention, everyone.
Jim_Wa at 2007-7-21 10:32:24 >

> using the scanner class, no need to use
> BufferedInputReader
> If none of the
> Integer.parseInt(bufferedReader.readLine()) thingies
> are
> considered to be correct, I guess the "correct"
> answer must've something
> to do with that darn Scanner class.
I wonder how the website decides whether a program is classified as correct. Will it actually be so nit-picky to require a Scanner solution, which isn't in the problem statement, when there are several ways of accomplishing the task? In the worse case, the program will have to match the submitted program with the "correct" program.
> I wonder how the website decides whether a program is
> classified as correct. Will it actually be so
> nit-picky to require a Scanner solution, which isn't
> in the problem statement, when there are several ways
> of accomplishing the task? In the worse case, the
> program will have to match the submitted program with
> the "correct" program.
Well, it's not as bad as you might think. This appears to be some sort of Java 1.5 certification exam simulator, as the Scanner solution would only be required in a Java 5 environment. There's probably not too many variations on the theme that work, so you could just store all the possible answers. The Scanner doesn't need to be mentioned in the problem statement, because Scanner is now the accepted way to parse an input stream in Java 5.
Brian
Yeah. In real life, there are usually multiple reasonable ways to solve a problem, with some better or worse than others but usually only in a way relevant to that particular context and often the criteria has nothing to do with the code at hand.
To arbitrarily say that the Scanner approach is "correct" and the BufferedReader approach is "incorrect" is to confuse the student unnecessarily, or to encourage a dogmatic approach to programming.
@OP: Please tell me you didn't pay for this class. Consider switching to a different class. If you did pay for it and don't want to quit it, keep in mind that some of the grading seems arbitrary and misleading.
> I did get the correct solution. I don't have it now,
> but I think someone posted it earlier in the thread.
> It was two lines, using the scanner class, no need
> to use BufferedInputReader. Thanks for your
> attention, everyone.
Great, so you've just proven the class is worthless. You put in a perfectly valid solution to the question you posted and it rejected it because you used a slightly different method than what was expected. Personally, I'd bring it up with the professor and see what they say, hopefully they recognize the lunacy of their system and give you an alternative. If they don't, I'd see if there's another class I could take.
> > I wonder how the website decides whether a program
> is
> > classified as correct. Will it actually be so
> > nit-picky to require a Scanner solution, which
> isn't
> > in the problem statement, when there are several
> ways
> > of accomplishing the task? In the worse case, the
> > program will have to match the submitted program
> with
> > the "correct" program.
>
> Well, it's not as bad as you might think. This
> appears to be some sort of Java 1.5 certification
> exam simulator, as the Scanner solution would only be
> required in a Java 5 environment. There's probably
> not too many variations on the theme that work, so
> you could just store all the possible answers. The
> Scanner doesn't need to be mentioned in the problem
> statement, because Scanner is now the accepted way to
> parse an input stream in Java 5.
>
> Brian
What he's describing is analogous to being asked for two numbers that 5 is the sum of and being marked wrong because he put 3 + 2 instead of 2 + 3.
> I wonder how the website decides whether a program is
> classified as correct. Will it actually be so
> nit-picky to require a Scanner solution, which isn't
> in the problem statement, when there are several ways
> of accomplishing the task? In the worse case, the
> program will have to match the submitted program with
> the "correct" program.
Just wrote a simple/dumb Java Test program ([url http://forum.java.sun.com/thread.jspa?messageID=4370874]source[/url]). It probably works in a similar fashion to the program the OP is using. I used regular expressions to match the answer against the answers given by a user.
The Case Against Homework: How Homework Is Hurting Our Children and What We Can Do About It (Crown, 2006)
hiwaa at 2007-7-21 10:32:24 >

> The Case Against Homework: How Homework Is Hurting> Our Children and What We Can Do About It (Crown,> 2006)Just read the book review. Interesting? Where was that book when I was growing up?