 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Randy Tingley Guest
|
Posted: Sat Apr 03, 2004 12:21 am Post subject: loading varables |
|
|
I have a variable "book" which askes the user for four titles which will be
with one patron's name.
How would i load this with a loop?
Some how I am getting lost?
Thank you.
input Patron's name
name = reader.readLine("Enter the first patron's name: ");
patron.setName(p);
for (int i = 1; i <= 3; i++){
Book = reader.readLine("Enter the book's title: ");
Book.setBook(i, Book);
|
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Sat Apr 03, 2004 12:43 am Post subject: Re: loading varables |
|
|
On Fri, 2 Apr 2004 19:21:30 -0500, "Randy Tingley" <rtingley (AT) nep (DOT) net>
wrote or quoted :
| Quote: | I have a variable "book" which askes the user for four titles which will be
with one patron's name.
How would i load this with a loop?
Some how I am getting lost?
Thank you.
|
First read http://mindprod.com/jgloss/codingconventions.html
It is important for people to understand your code you use caps
properly.
What type of variable is book? String[] JComboBox, String?
What code you need depends heavily on that.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
|
|
| Back to top |
|
 |
Randy Tingley Guest
|
Posted: Sat Apr 03, 2004 12:57 am Post subject: Re: loading varables |
|
|
"Roedy Green" <look-at-the-website (AT) mindprod (DOT) com> wrote
| Quote: | On Fri, 2 Apr 2004 19:21:30 -0500, "Randy Tingley"
wrote or quoted :
I have a variable "book" which askes the user for four titles which will
be
with one patron's name.
How would i load this with a loop?
Some how I am getting lost?
Thank you.
First read http://mindprod.com/jgloss/codingconventions.html
It is important for people to understand your code you use caps
properly.
What type of variable is book? String[] JComboBox, String?
What code you need depends heavily on that.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
|
Book is a String
We have two programs and I am trying to code (3rd program) an interface that
will
accept user input. I know that I am a novice and the books that I have all
over
my kitchen table seem to be worthless ...
So far with dozens of attempts ... my efforts have been in vain
|
|
| Back to top |
|
 |
Mike Hoover Guest
|
Posted: Sat Apr 03, 2004 8:24 am Post subject: Re: loading varables |
|
|
On the line that reads:
patron.setName(p);
Shouldn't is be:
patron.setName(name);
"Randy Tingley" <rtingley (AT) nep (DOT) net> wrote
| Quote: | I have a variable "book" which askes the user for four titles which will
be
with one patron's name.
How would i load this with a loop?
Some how I am getting lost?
Thank you.
input Patron's name
name = reader.readLine("Enter the first patron's name: ");
patron.setName(p);
for (int i = 1; i <= 3; i++){
Book = reader.readLine("Enter the book's title: ");
Book.setBook(i, Book);
|
|
|
| Back to top |
|
 |
Randy Tingley Guest
|
Posted: Sat Apr 03, 2004 11:32 am Post subject: Re: loading varables |
|
|
I have been attempting to modify this program below to accept user input for
patron's
name and book's title:
public class LibraryTester{
public static void main (String[] args)
{
Patron p = new Patron("Ken Lambert");
Book b1 = new Book("Cider House Rules", "John Irving");
Book b2 = new Book("The Perfect Storm", "Sebastian Junger");
Book b3 = new Book("The Illiad", "Homer");
Book b4 = new Book("Hamlet", "William Shakespeare");
System.out.println(p.borrowBook(b1));
System.out.println(p.borrowBook(b2));
System.out.println(p.borrowBook(b3));
System.out.println(p.borrowBook(b4));
System.out.println(p);
System.out.println(p.returnBook("Cider House Rules"));
System.out.println(p.hasBook("Cider House Rules"));
System.out.println(p.hasBook("The Perfect Storm"));
System.out.println(p);
}
}
**********************************************
to pass data to another two programs, this one is Book:
public class Book {
//instance varibles
private String author, title;
//construction method
public Book()
{
author = " ";
title = " ";
}
//another constructor method - same name
public Book(String author, String title)
{
author = author;
title = title;
}
public String author()
{
return(author);
}
public String title()
{
return(title);
}
public String toString()
{
return("Title: " + title + "n" + "Author:" +
author);
}
}
****************************************************
And pass to this program Patron:
public class Patron {
//instance varibles
private String name;
private Book Book1, Book2, Book3;
//constructor method
public Patron()
{
name = " ";
Book1 = null;
Book2 = null;
Book3 = null;
}
public void setName(String nm){
//Set a student's name
name=nm;
}
public String getName()
{
return(name);
}
public boolean borrow(Book B)
{
if (Book1 == null)
{
Book1 = B;
return(true);
}
else if (Book2 == null)
{
Book2 = B;
return(true);
}
else if (Book3 == null)
{
Book3 = B;
return(true);
}
else
return(false);
}
public boolean hasbook(String title)
{
if (Book1 !=null)
if
(Book1.title().equals(title))
return(true);
if (Book2 !=null)
if
(Book2.title().equals(title))
return(true);
if (Book3 !=null)
if
(Book3.title().equals(title))
return(true);
return(false);
}
public boolean returnbook(String title)
{
if (Book1 !=null)
if
(Book1.title().equals(title))
{
Book1 = null;
return(true);
}
if (Book2 !=null)
if
(Book2.title().equals(title))
{
Book2 = null;
return(true);
}
if (Book3 !=null)
if
(Book3.title().equals(title))
{
Book3 = null;
return(true);
}
return(false);
}
public String toString()
{
String str;
str = ("Patron's name: " +
name);
if (Book1 != null)
str = (str +
"n" + Book1);
if (Book2 != null)
str = (str +
"n" + Book2);
if (Book3 != null)
str = (str +
"n" + Book3);
return(str);
}
}
//ends patrons class
***************************************************
I have rewritten my LibraryTester at least 11 times in vain.
Any comments will be greatly appreciated.
Thank you.
|
|
| Back to top |
|
 |
Randy Tingley Guest
|
Posted: Sat Apr 03, 2004 11:47 am Post subject: Re: loading varables |
|
|
[snip for readablility]
My first 18 - 20 attempts to re-code LibraryTester to accept user input was:
import TerminalIO.KeyboardReader;
import java.text.NumberFormat;
import BreezySwing.Format;
public class LibraryTester {
public static void main (String[] args){
// Instantiate the Patrons & Books and the keyboard
Patron patron1 = new Patron();
Patron patron2 = new Patron();
Patron patron3 = new Patron();
//Book b1 = new Book();
//Book b2 = new Book();
//Book b3 = new Book();
KeyboardReader reader = new KeyboardReader();
String name;
String book;
// Input the first patron's data
name = reader.readLine("Enter the first patron's name: ");
//patron1.setName(name);
for (int i = 1; i <= 3; i++){
book = reader.readLine("Enter the book's title: ");
student1.setBook(i, book);
}
// Input the second patron's data
name = reader.readLine("Enter the second patron's name: ");
patron2.setName(name);
for (int i = 1; i <= 3; i++){
book = reader.readLine("Enter the book's title: ");
patron2.setBook(i, book);
}
// Input the third patron's data
name = reader.readLine("Enter the third patron's name: ");
patron3.setName(name);
for (int i = 1; i <= 3; i++){
book = reader.readLine("Enter the book's title: ");
patron3.setBook(i, book);
}
// Output the three patrons information
System.out.println(patron1);
System.out.println(patron2);
*****************************************
I know that I am missing a piece somewhere. Between my wife complaining the
kitchen table is
cluttered with all my java info, books, print outs, and other research stuff
for the last week, I am starting
to believe that I don't understand how to pass info in java.
Any comments, directions, would be greatly appreciated.
Thank you very much for all those that take a minute to review my kaos.
Randy
|
|
| Back to top |
|
 |
Andrew Harker Guest
|
Posted: Sat Apr 03, 2004 1:14 pm Post subject: Re: loading varables |
|
|
Randy Tingley wrote:
| Quote: | I know that I am missing a piece somewhere. Between my wife complaining the
kitchen table is
cluttered with all my java info, books, print outs, and other research stuff
for the last week, I am starting
to believe that I don't understand how to pass info in java.
Any comments, directions, would be greatly appreciated.
Thank you very much for all those that take a minute to review my kaos.
Randy
|
I suggest you start with one class and make that do what you want
rather than trying to integrate all these classes all at once.
For each class write a little test harness and check it out - this
will also help you work out how to create a class, call each method,
etc. Code a bit then test it. Pay attention to things like variable
names, scope etc are correct (also follow Roedy's advice too). Look
at various data structures (eg arrays) and play with those a bit at a
time too.
You could use test classes like JUnit but to start with take advantage
of the fact you can have a 'main' in each class ... eg here is a simple
book demo ...
// file Book.java
public class Book {
private String title;
private String author;
Book(String title) {
this(title, "Unknown");
}
Book(String title, String author) {
this.title = title;
this.author = author;
}
public String toString() {
return "Book:title["+title+"],author["+author+"]";
}
public void setAuthor(String author) {
this.author = author;
}
// this will test out the Book class
public static void main(String[] args) {
Book b1 = new Book("Big words");
System.out.println(b1);
b1.setAuthor("RT");
System.out.println(b1);
// Book[] books = new Book[3];
// books[0] = new Book("One", "Fred");
// books[1] = new Book("Two");
// books[2] = new Book("Three", "Barney");
// following is equiv. to above
Book[] books = {
new Book("One", "Fred"),
new Book("Two"),
new Book("Three", "Barney")
};
for (int i = 0; i < books.length; i++) {
System.out.println(books[i]);
}
}
}
|
|
| Back to top |
|
 |
Randy Tingley Guest
|
Posted: Sat Apr 03, 2004 1:55 pm Post subject: Re: loading varables |
|
|
"Andrew Harker" <andrew (AT) sadNOSPAMPLEASEgeeks (DOT) com> wrote
| Quote: | Randy Tingley wrote:
I know that I am missing a piece somewhere. Between my wife complaining
the
kitchen table is
cluttered with all my java info, books, print outs, and other research
stuff
for the last week, I am starting
to believe that I don't understand how to pass info in java.
Any comments, directions, would be greatly appreciated.
Thank you very much for all those that take a minute to review my kaos.
Randy
I suggest you start with one class and make that do what you want
rather than trying to integrate all these classes all at once.
For each class write a little test harness and check it out - this
will also help you work out how to create a class, call each method,
etc. Code a bit then test it. Pay attention to things like variable
names, scope etc are correct (also follow Roedy's advice too). Look
at various data structures (eg arrays) and play with those a bit at a
time too.
You could use test classes like JUnit but to start with take advantage
of the fact you can have a 'main' in each class ... eg here is a simple
book demo ...
// file Book.java
public class Book {
private String title;
private String author;
Book(String title) {
this(title, "Unknown");
}
Book(String title, String author) {
this.title = title;
this.author = author;
}
public String toString() {
return "Book:title["+title+"],author["+author+"]";
}
public void setAuthor(String author) {
this.author = author;
}
// this will test out the Book class
public static void main(String[] args) {
Book b1 = new Book("Big words");
System.out.println(b1);
b1.setAuthor("RT");
System.out.println(b1);
// Book[] books = new Book[3];
// books[0] = new Book("One", "Fred");
// books[1] = new Book("Two");
// books[2] = new Book("Three", "Barney");
// following is equiv. to above
Book[] books = {
new Book("One", "Fred"),
new Book("Two"),
new Book("Three", "Barney")
};
for (int i = 0; i < books.length; i++) {
System.out.println(books[i]);
}
}
}
Thank you! I will try this. |
Randy
|
|
| Back to top |
|
 |
Randy Tingley Guest
|
Posted: Sat Apr 03, 2004 4:34 pm Post subject: Re: loading varables |
|
|
"Randy Tingley" <rtingley (AT) nep (DOT) net> wrote
| Quote: |
"Andrew Harker" <andrew (AT) sadNOSPAMPLEASEgeeks (DOT) com> wrote in message
news:406eb8d1.0 (AT) entanet (DOT) ..
Randy Tingley wrote:
I know that I am missing a piece somewhere. Between my wife
complaining
the
kitchen table is
cluttered with all my java info, books, print outs, and other research
stuff
for the last week, I am starting
to believe that I don't understand how to pass info in java.
Any comments, directions, would be greatly appreciated.
Thank you very much for all those that take a minute to review my
kaos.
Randy
I suggest you start with one class and make that do what you want
rather than trying to integrate all these classes all at once.
For each class write a little test harness and check it out - this
will also help you work out how to create a class, call each method,
etc. Code a bit then test it. Pay attention to things like variable
names, scope etc are correct (also follow Roedy's advice too). Look
at various data structures (eg arrays) and play with those a bit at a
time too.
You could use test classes like JUnit but to start with take advantage
of the fact you can have a 'main' in each class ... eg here is a simple
book demo ...
// file Book.java
public class Book {
private String title;
private String author;
Book(String title) {
this(title, "Unknown");
}
Book(String title, String author) {
this.title = title;
this.author = author;
}
public String toString() {
return "Book:title["+title+"],author["+author+"]";
}
public void setAuthor(String author) {
this.author = author;
}
// this will test out the Book class
public static void main(String[] args) {
Book b1 = new Book("Big words");
System.out.println(b1);
b1.setAuthor("RT");
System.out.println(b1);
// Book[] books = new Book[3];
// books[0] = new Book("One", "Fred");
// books[1] = new Book("Two");
// books[2] = new Book("Three", "Barney");
// following is equiv. to above
Book[] books = {
new Book("One", "Fred"),
new Book("Two"),
new Book("Three", "Barney")
};
for (int i = 0; i < books.length; i++) {
System.out.println(books[i]);
}
}
}
Thank you! I will try this.
Randy
I think I got it! |
Thank you very much!
|
|
| Back to top |
|
 |
Andrew Thompson Guest
|
Posted: Sun Apr 04, 2004 7:25 am Post subject: Re: loading varables |
|
|
On Sat, 3 Apr 2004 08:55:16 -0500, Randy Tingley wrote:
| Quote: | ..Between my wife complaining the
kitchen table is
cluttered with all my java info,
|
And our group is becoming cluttered with
your untrimmed replies, 85 lines for your
last reply to say 'thank you', is ridiculous.
Please use the 'delete' key before responding..
<http://www.physci.org/kbd.jsp?key=del>
--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
|
|
| Back to top |
|
 |
Randy Tingley Guest
|
Posted: Sun Apr 04, 2004 12:44 pm Post subject: Re: loading varables |
|
|
Do you grip all the time?
First to you bring to my attention that I am top posting.
I read your link and made sure that I did not do that again.
Next you told me that when replying I, "cut" or "snipped" to
much of the text out. Now people couldn't see all the question.
Now you are telling me that I am not cutting out enough?
once again you attached a link for me to review.
This newsgroup has taught me alot of valuable information
on java programming that I can not get from any book. This to
me is a "priceless" newsgroup.
I am trying to gain the knowledge that these wonderful people are
willing to share, along with learning this group's rules. I looked in
this group for FAQ's, but there are none?
Thank you, for all that ARE assisting me in my quest for knowledge.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|