The Artima Developer Community
Sponsored Link

Java Answers Forum
TIJ e2 ch2 ex7 question

4 replies on 1 page. Most recent reply: Feb 23, 2011 7:02 AM by James Watson

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Jordan O

Posts: 7
Nickname: crowbar
Registered: Feb, 2011

TIJ e2 ch2 ex7 question Posted: Feb 11, 2011 7:26 AM
Reply to this message Reply
Advertisement
Thinking in Java by Bruce Eckel
edition2 ch2 ex7

Ch.2 Exercise 7 states: Write a program that prints three arguments taken from the command line. To do this, you'll need to index into the command-line array of Strings.

I was able to complete the exercise with the following code:
////
import java.io.*;
public class ch2ex7 {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s;
while ((s = in.readLine()).length() != 0) System.out.println(s);
}
}
///:~
This code is taken from p.603 ch11:Echo.java
Being that exercise 7 of ch 2 is written before the reader is supposed to know hardly anything about java makes me wonder how on earth Mr. Eckel expected a fresh programmer to solve the problem. Am I missing something? I went back through all of ch2 and could not find anything that he had written intended to help the user write this program.

If you know an easier way to write such a program or why Mr. Eckel would put such a complex problem in the beginning exercises of his book I would appreciate it.

Regards,
Jordan O


Jordan O

Posts: 7
Nickname: crowbar
Registered: Feb, 2011

Re: TIJ e2 ch2 ex7 question Posted: Feb 11, 2011 8:04 AM
Reply to this message Reply
UPDATE:
I did figure out how to do it simpler:

///
public class co2ex7 {
public static void main(String[] args) {
for (int i =0; i < args.length; i++){
if ((args.length != 0) && (i < 3)) System.out.println(args);
}
}
}
///~

However I still don't think that a novice programmer would be able to do that with no knowledge of arrays.

Any thoughts?

Jordan O

Jordan O

Posts: 7
Nickname: crowbar
Registered: Feb, 2011

Re: TIJ e2 ch2 ex7 question Posted: Feb 11, 2011 8:09 AM
Reply to this message Reply
CORRECTION: In the above topic it took args to mean italicize the text so my code is corrupted. See the following code for what I meant to post.

UPDATE:
I did figure out how to do it simpler:

///
public class co2ex7 {
public static void main(String[] args) {
for (int j =0; j < args.length; i++){
if ((args.length != 0) && (j < 3)) System.out.println(args[j]);
}
}
}
///~

However I still don't think that a novice programmer would be able to do that with no knowledge of arrays.

Any thoughts?

Jordan O

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: TIJ e2 ch2 ex7 question Posted: Feb 23, 2011 7:01 AM
Reply to this message Reply
You are still too complex:

public class co2ex7 {
  public static void main(String[] args) {
    for (int i = 0; i < args.length && i < 3; i++){
      System.out.println(args[i]);
    }
  }
}


I'm guessing that since the question specifically said 3 arguments, the following was the acceptable solution:

public class co2ex7 {
  public static void main(String[] args) {
    for (int i = 0; i < 3; i++){
      System.out.println(args[i]);
    }
  }
}


Your solution is ahead of the book, I think. Is it too hard for someone who knows nothing of arrays? Well, now you know about arrays, right? It's a pretty important and basic thing to learn.

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: TIJ e2 ch2 ex7 question Posted: Feb 23, 2011 7:02 AM
Reply to this message Reply
This might have been OK too:

public class co2ex7 {
  public static void main(String[] args) {
    System.out.println(args[0]);
    System.out.println(args[1]);
    System.out.println(args[2]);
  }
}

Flat View: This topic has 4 replies on 1 page
Topic: scrolbar want  immediate response.......... Previous Topic   Next Topic Topic: execute .class file from .jsp

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use