• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

help - want to pass strings to other class

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's my problem. I have an application which is supposed to have one abstract class for a student & pass along functionality to subclasses. I have created a Main class which successfully accepts user input by calling a keyboard class method. What I'm trying to do is link my string inputs to the abstract class, which will create the record in 1 of 3 subclasses (or so I hope), which can then be displayed or manipulated. I also can't seem to declare a method in class Main and call it within it. Also, I was told that I should create a class which will decide which subclass constructor (from my abstract class) to create based on the college entered in Main, but I don't know how to do this either. My abstract class constructor is like this: ST(String x, String y, String z), as is my subclass constructors. Lastly, I'd like to store the output from the strings and/or constructors into an array.
Any suggestions are greatly appreciated. If anyone has an application that does something like this, I would love to see some example code. Here is some of my code below:

edited by Dave to format code

[This message has been edited by Dave Vick (edited November 14, 2001).]
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brian

This sounds like maybe it's a school assignmnet so I'll give you some tips but not complete answers, if you need more let me know...
It looks like you need to create 3 subclasses of your abstract class, it sounds like each of these subclasses will represent a student from a different college. After you get your input you need to decide which type of subclass you need to create. Tehn call the appropriate constructor. For instance of the school name input was xavier then you'd create a new xavier instance and send it the rest of the input (name and state), you dont need to inpout the school name because you know it is already xavier - you can set this in the constructor of the subclass then pass the other two inputs to the superclass constructor.
In your Main class you might be having a problem calling amethod if your tryiong to do it from within the main method - in that case you can only call static methods, unless you create an object of another class and call one of its methods. You might want to create a Main object then you can call your other methods like showMenu. within showMenu you can have a loop that just keeps repeating until they enter the exit command.
After each entry they give you you have to decide what they want to do adn call the appropriate method (add, find, etc).
To store them into an array just declare an array of Student objects then you can store any Student object into it (including items that are subclasses of Student), when you take an item off of the array you'll just need to cast it back to the appropriate type before you can use any of its methods. So you might need to put in a variable that'll hold the type of object it is. Then after you check the type you can cast the object and use it.
hope that helps a little bit, you might want to post more complete code or if there is a specific error you're getting post that section completely.

Also,
we hope you�ll enjoy visiting as a regular however, your name is not in keeping with our naming policy here at the ranch. Please re-register under an appropriate name as shown in the policy.


------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To reinforce from the previous post:
Move each class into it's own file. Don't have any code in your "main" except what you need to kick things off.
 
Williams, Brian
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dave Vick:
Brian
Your tips yesterday helped. Thanks. Here�s where I�m at now. I put all my methods from Main for processing keyboard input & student info into my KeyboardInput class and am calling them in Main. I am getting it to accept input and am passing the info to the college constructors; but my output for name, state, and college all says null. I also want to store each of these strings into an array and separate each string (not record) with a semicolon, and I still don�t know how to do the find (it is supposed to find a whole record based on the name entered). Lastly, for some reason my application will accept and then show the display menu and accept command line again before processing input. You were right, this is for a college assignment, and it is due tomorrow (unfortunately). I�ve worked on this over 20 hours so far and am very close, but it�s starting to drive me bonkers!!! I have provided entire code from some of my classes below:
public class Main {
public static void main (String args[]) {

try {

FileReader fr = new FileReader("PhoneBook1.txt");
StreamTokenizer st = new StreamTokenizer(fr);

char data_r[] = new char[1000];
int chars = fr.read(data_r);

if(chars == -1) {
System.out.println("Student.txt has no data");
System.out.println("Usage: java Student");
System.out.println("");
}
else if(chars != -1) {
System.out.println(new String(data_r, 0, chars));
}

fr.close();
}
catch(Exception e) {
System.out.println("Exception: " + e);
}

KeyboardInput k = new KeyboardInput();
k.displayMenu();
k.process();
}
}
import java.io.*;
public class KeyboardInput {
Xavier x = new Xavier();
Findlay f = new Findlay();

public static String keyboardInput;

public String readLine() {
char in;
keyboardInput = "";

try {
in = (char) System.in.read();
while(in != '\n') {
keyboardInput = keyboardInput + in;
in = (char) System.in.read();
}
}
catch (IOException e) {
System.out.println("No value");
keyboardInput = "";
}
keyboardInput = keyboardInput.trim();

return keyboardInput;
}

public String getKeyboardInput() {
return keyboardInput;
}

public String displayMenu() {
System.out.println("Available commands");
System.out.println("a - Add new student");
System.out.println("f - Find");
System.out.println("e - exit");
System.out.print("Enter command: ");
String s = new String();
s = readLine();
return s;
}

public void process() {
String x = new String();
x = displayMenu();
//while(!x.equals("e") | | x.equals("E")) {
if(x.equals("a") | | x.equals("A")) {
addStudent();
}
else if(x.equals("f") | | x.equals("F")) {
System.out.println("ssss");
}
else if(x.equals("e") | | x.equals("E")) {
exit();
}
else {
System.out.println("Can not recognize input. Try again");
System.out.println("");
displayMenu();
}
}

public void addStudent() {

String name = new String();
String phone = new String();
String college = new String();
System.out.print("Enter name: ");
name = readLine();
System.out.print("Enter phone: ");
phone = readLine();
System.out.print("Enter college: ");
college = readLine();

if(college.equals("Xavier") | | college.equals("xavier")) {
if(phone.length() != us.phone_length) {
System.out.println("Phone format is ######### requiring 10 digits");
displayMenu();
process();
}
else {
new Xavier(name, phone, college);
x.display();
System.out.println(x.toString());
}
}

if(college.equals("Findlay") | | college.equals("findlay")) {
if(phone.length() != f.phone_length) {
System.out.println("Phone format is ######### requiring 9 digits");
displayMenu();
process();
}
else {
new Findlay(name, phone, college);
f.display();
System.out.println(f.toString());
}
}
}
public void exit() {
//try {
// FileWriter fw = new FileWriter("PhoneBook.txt");
// char data_w[] = new char[1000];
// for(int i = 0; i < data_w.length; i++) {<br /> // fw.write((char)data_w[i]);<br /> // }<br /> // fw.close(); <br /> System.exit(0);<br /> // }<br /> // catch(Exception e) {<br /> // System.out.println("Exception: " + e);<br /> // }<br /> }<br /> }<br /> class Xavier extends Student {<br /> static int phone_length = 10;<br /> <br /> Xavier() {<br /> } <br /> <br /> Xavier(String name, String phone, String college) {<br /> super(name, phone, college);<br /> if(super.name.length() > super.name_size) {
this.adjust_name();
}
}

public void display() {
super.display();
}

public String toString() {
return getName() + " " + getPhone() +
" " + getCollege();
}

public String adjust_name() {
StringBuffer s1 = new StringBuffer(name);
s1.delete(19, name.length());
name = s1.toString();
return name;
}
}

public abstract class Student {
static int name_size = 20;
public String name, phone, college;

Student() {
}

Student(String name, String phone, String college) {
this.name = name;
this.phone = phone;
this.college = college;
}

public void display() {
System.out.print("Name ");
System.out.print("Phone ");
System.out.println("College ");
System.out.print("---- ");
System.out.print("---- ");
System.out.println("---- ");
}
public abstract String toString();
public abstract String adjust_name();

public String getName() {
return name;
}

public String getPhone() {
return phone;
}

public String getCollege() {
return college;
}

}
This sounds like maybe it's a school assignmnet so I'll give you some tips but not complete answers, if you need more let me know...
It looks like you need to create 3 subclasses of your abstract class, it sounds like each of these subclasses will represent a student from a different college. After you get your input you need to decide which type of subclass you need to create. Tehn call the appropriate constructor. For instance of the school name input was xavier then you'd create a new xavier instance and send it the rest of the input (name and state), you dont need to inpout the school name because you know it is already xavier - you can set this in the constructor of the subclass then pass the other two inputs to the superclass constructor.
In your Main class you might be having a problem calling amethod if your tryiong to do it from within the main method - in that case you can only call static methods, unless you create an object of another class and call one of its methods. You might want to create a Main object then you can call your other methods like showMenu. within showMenu you can have a loop that just keeps repeating until they enter the exit command.
After each entry they give you you have to decide what they want to do adn call the appropriate method (add, find, etc).
To store them into an array just declare an array of Student objects then you can store any Student object into it (including items that are subclasses of Student), when you take an item off of the array you'll just need to cast it back to the appropriate type before you can use any of its methods. So you might need to put in a variable that'll hold the type of object it is. Then after you check the type you can cast the object and use it.
hope that helps a little bit, you might want to post more complete code or if there is a specific error you're getting post that section completely.

Also,
we hope you�ll enjoy visiting as a regular however, your name is not in keeping with our naming policy here at the ranch. Please re-register under an appropriate name as shown in the policy.


 
It runs on an internal combustion engine. This ad does not:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic