I cant get the console to print out the string that is entered for
first, middle, last. It retuns null. Im sorry Im new to this.
public class NamePrinter {
public String first;
public String middle;
public String last;
/**
* NamePrinter constructor comment.
*/
public NamePrinter(String first, String middle, String last) {
}
/**
* This method was created in VisualAge.
*/
public void calculateName() {
System.out.println (first + middle +last); //This is where I am going
to make the differnet name combos
//I am having trouble making the console print the input for my 3
strings.
//I can't make it print whatever is inputed for the Strings.
//It says nullnullnull
}
}
package Homework2; //THIS IS THE RUN FILE. DOES IT MATTER IF IT IS
AFTER THE calculateName class
//in the package???? Is it supposed to go before
it??
/**
* This type was created in VisualAge.
*/
import java.io.*;
public class Test {
/**
* Test constructor comment.
*/
public Test() {
super();
}
/**
* Starts the application.
* @param args an array of command-line arguments
*/
public static void main(java.lang.String[] args) throws Exception {
String first, middle, last;
NamePrinter name = new NamePrinter("first", "middle", "last");
BufferedReader input = new BufferedReader
(new InputStreamReader(System.in));
System.out.println("Enter First Name: ");
first = input.readLine();
System.out.println("Enter Middle Name: ");
middle = input.readLine();
System.out.println("Enter Last Name: ");
last = input.readLine();
name.calculateName(); //returns nullnullnull
}
}
Andrew Thompson - 19 Mar 2004 19:23 GMT
> I cant get the console to print out the string that is entered for
> first, middle, last. It retuns null. Im sorry Im new to this.
You're in the right place, but using the wrong software.
> public class NamePrinter {
>
> public String first;
> public String middle;
> public String last;
These are attributes of the class, there
is nothing assigned to them here, so they
are 'null'
> /**
> * NamePrinter constructor comment.
> */
That is a very unhelpful comment.
> public NamePrinter(String first, String middle, String last) {
>
> }
Here the constructor accepts three strings,
but instead of doing something logical like..
this.first = first;
....
Which would assign them to the variables defined
above, the values are instead lost..
(so the class attributes are still 'null')
> /**
> * This method was created in VisualAge.
> */
Aaaah yes, here it is. Dump this 'VisualAge' for
the moment and figure how to compile and run
from the command line. You will learn much
quicker, and understand more.
> public void calculateName() {
>
> System.out.println (first + middle +last); //This is where I am going
> to make the differnet name combos
Yes, but first you need to understand the basics.
Did you skip the HelloWorld example perhaps?
See if you can get it working from what I
have pointed out. If you do not understand
what I am saying it may be best to put this
aside for the moment as you go back and do
the progressive steps, starting w/HelloWorld.
HTH

Signature
Andrew Thompson
* http://www.PhySci.org/ Open-source software suite
* http://www.PhySci.org/codes/ Web & IT Help
* http://www.1point1C.org/ Science & Technology
Ian Shef - 19 Mar 2004 19:57 GMT
Andrew Thompson <SeeMySites@www.invalid> wrote in news:oesi7sk8fws4
$.u4n7z5owk5lj$.dlg@40tude.net:
>> I cant get the console to print out the string that is entered for
>> first, middle, last. It retuns null. Im sorry Im new to this.
[quoted text clipped - 44 lines]
>>
>> System.out.println (first + middle +last); //This is where I am
going
>> to make the differnet name combos
>
[quoted text clipped - 9 lines]
>
> HTH
Also, you have:
System.out.println("Enter First Name: ");
first = input.readLine();
System.out.println("Enter Middle Name: ");
middle = input.readLine();
System.out.println("Enter Last Name: ");
last = input.readLine();
but first, middle, and last are defined locally to main, so main gets the
entered text and the object called "name" still has nulls.
Remove the definitions of first, middle, and last in main, and try:
System.out.println("Enter First Name: ");
name.first = input.readLine();
System.out.println("Enter Middle Name: ");
name.middle = input.readLine();
System.out.println("Enter Last Name: ");
name.last = input.readLine();
Better might be:
System.out.println("Enter First Name: ");
name.setFirst(input.readLine()) ;
System.out.println("Enter Middle Name: ");
name.setMiddle(input.readLine()) ;
System.out.println("Enter Last Name: ");
name.setLast(input.readLine()) ;
but then you will have to define the methods setFirst, setMiddle, and
setLast in your class NamePrinter.
Good Luck!

Signature
Ian Shef 805/F6 * These are my personal opinions
Raytheon Company * and not those of my employer.
PO Box 11337 *
Tucson, AZ 85734-1337 *
Mark Haase - 20 Mar 2004 00:08 GMT
> Also, you have:
>
[quoted text clipped - 7 lines]
> but first, middle, and last are defined locally to main, so main gets the
> entered text and the object called "name" still has nulls.
This seems like reason enough to me why CS courses shouldn't start with
Java! I see these kinds of simple misconceptions all too often. Why not
start with something like BASIC, where you can teach basic procedural
programming concepts, such as flow-control statements and simple design
patterns.
Then, once kids get that, introduce the idea of objects and start
showing them Java. It would add so much more to the curriculum.
|\/| /| |2 |<
mehaase(at)sas(dot)upenn(dot)edu
Roedy Green - 20 Mar 2004 07:16 GMT
>Why not
>start with something like BASIC, where you can teach basic procedural
>programming concepts, such as flow-control statements and simple design
>patterns.
Because they will forever write spaghetti in whatever language they
code.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Mark Haase - 21 Mar 2004 06:17 GMT
> >Why not
> >start with something like BASIC, where you can teach basic procedural
[quoted text clipped - 3 lines]
> Because they will forever write spaghetti in whatever language they
> code.
Uhh, care to elaborate why? Good technique in any language ought to
carry over to another, no?
|\/| /| |2 |<
mehaase(at)sas(dot)upenn(dot)edu
Jon A. Cruz - 21 Mar 2004 08:40 GMT
> Uhh, care to elaborate why? Good technique in any language ought to
> carry over to another, no?
Yes.
And bad technique in any language also carries over to another.
That's one of the big reasons Pascal used to be the language of choice
for education over BASIC. Although it's possible to write good code in
BASIC, it's also very easy to write bad code with it.
Andrew Thompson - 21 Mar 2004 08:52 GMT
> And bad technique in any language also carries over to another.
>
> That's one of the big reasons Pascal used to be the language of choice
> for education over BASIC.
What is the chosen teaching language now?

Signature
Andrew Thompson
* http://www.PhySci.org/ Open-source software suite
* http://www.PhySci.org/codes/ Web & IT Help
* http://www.1point1C.org/ Science & Technology
Jon A. Cruz - 21 Mar 2004 20:53 GMT
> What is the chosen teaching language now?
Java.
Here in the US, Comp-sci intros and the AP placement tests have switched
over.
Chiron Paixos - 19 Mar 2004 19:50 GMT
>I cant get the console to print out the string that is entered for
>first, middle, last. It retuns null. Im sorry Im new to this.
[quoted text clipped - 12 lines]
>
>}
So what do you want to do with the values you pass to the constructor
of NamePrinter as arguments. By now they are going to limbo / nirvana
/ bitbucket / garbage-collection!
Maybe some statements like
this.first = first
might come handy, but who knows?
At the current state of your code the three string-attributes first,
middle, last are are initialized as null-stings and left that way.
>/**
> * This method was created in VisualAge.
> */
>public void calculateName() {
>
> System.out.println (first + middle +last); //This is where I am going
>
>
> //I am having trouble making the console print the input for my 3
>strings.
> //I can't make it print whatever is inputed for the Strings.
> //It says nullnullnull
And that's absolutely correct as I already explained in my first
comment.
>}
>}
[quoted text clipped - 24 lines]
>
>NamePrinter name = new NamePrinter("first", "middle", "last");
Fine! Now you have one instance of NamePrinter. It the constructor
would work, the attributes would have been initialized. (first with
"first", middle with "middle" and last with "last")
> BufferedReader input = new BufferedReader
> (new InputStreamReader(System.in));
[quoted text clipped - 8 lines]
> name.calculateName(); //returns nullnullnull
>
With a working constructor for NamePrinter it would return
firstmiddlelast in every case ignoring your input. Maybe the
statements in the main-method have to be rearranged in a different
order.
>
>
>
>}
>}
As it's your homework I won't give away the working solution but these
clues should be more than sufficient to get it done.
HTH
Chiron
--
eo quod in multa sapientia multa sit indignatio et qui addit scientiam addat et laborem
Roedy Green - 19 Mar 2004 21:37 GMT
> //I can't make it print whatever is inputed for the Strings.
> //It says nullnullnull
chances are the problem lies with the code to input strings.
Input one per line. See http://mindprod.com/fileio.html
for how. Set it up for input/console/characters
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
dedreux - 20 Mar 2004 08:16 GMT
Thanks for all your help. I was able to figure it out from some of the
hints in your posts. It wasnt easy though. Thanks for helping. I am
more of a visual learner and I am taking this java class online where
it is hard to be in front of a screen with an instructor. Im sure, as
you guys know, this can seem quite confusing for some people, and I
certainly do respect the time and effort that you people have given to
learn it. Thanks for your help....I will need more.
> > //I can't make it print whatever is inputed for the Strings.
> > //It says nullnullnull
>
> chances are the problem lies with the code to input strings.
> Input one per line. See http://mindprod.com/fileio.html
> for how. Set it up for input/console/characters