Trouble with understanding String toString() method.
So I understand that toString() is a public value-returning method.
This is an example my textbook gives:
Code:
public String toString() {
String str = "";
if (hr < 10)
str = "0";
str = str + hr + ":";
if (min < 10)
str = str + "0";
str = str + min + ":";
if (sec < 10)
str = str + "0";
str = str + sec;
return str;
}
This is based on an earlier more lengthy code that implements setting Time and such. So I supposed this toString() method is something like formatting? Because in this example, if the user provided the hr, min, sec respectively with 8, 25, 26 then the output would be "08:25:56".
However what I am trying to do is essentially read a text file and then from that text file, extracting particular information. My professor suggested to build a temporary array so I can sort through it. And he's rather specific, so much so he provided us a template for the class and for the main method, so I don't have very much leeway with this.
I think I would have to provide the question if anyone is confused as to what I'm actually asking but nonetheless, this is what I have thus far from my code:
Code:
class Player{
//data members
private String name ;
private String position;
private int height ; // in inches
private double weight ; // in lb
private int age;
private int exp ;
private String college ;
private double salary ;
private int no ;
// public accessors
public String getName() {
return name1;
}
public String getPosition() {
return position;
}
public int getHeight() {
return height;
}
public double getWeight() {
return weight;
}
public int getAge() {
return age1;
}
public int getExp() {
return experience;
}
public String getCollege() {
return college;
}
public double getSalary() {
return salary;
}
public int getNo() {
return number;
}
// public mutators
public void setName(String newName) {
name1 = name;
}
public void setPosition(String newPosition) {
position = pos;
}
public void setHeight(int height) {
height = ht;
}
public void setWeight(double weight) {
weight = wt;
}
public void setAge(int age) {
age1 = age;
}
public void setExp(int exp) {
experience = exp;
}
public void setCollege(String College) {
college = col;
}
public void setSalary(double salary ) {
salary = sal;
}
public void setNo(int no) {
number = no;
}
// constructors :
public Player() { // default constructor
//setting my initial variables below to
//use for catching input a user may provide
//that is invalid
name1 = "no name";
position = "n/a";
height = 0;
weight = 0.0;
age1 = 0;
experience = 0;
college = "n/a";
salary = 1000000000;
number = 0;
}
public Player(String name, int ht , double wt , int age, String col, double sal) {
// this is determing the information that is going to be output.
name = name;
position = "N/A" // no need for the player's position
height = ht;
weight = wt;
age = age;
exp = 0; // no need for experience
college = col;
salary = sal;
number = 0; // no need for the player's number
}
public Player(int no , String name, String pos, int ht , double wt , int age, int exp , String col, double sal) {
name1 = name;
position = pos;
height = ht;
weight = wt;
age1 = age;
experience = exp;
college = col;
salary = sal;
number = no;
}
public Player(Player other) { // copy constructor
name1 = other.name1;
position = player.position;
height = player.height;
weight = player.weight;
age1 = player.age1;
experience = player.experience;
college = player.college;
salary = player.salary;
number = player.number;
}
public String toString() {
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance();
}
public void updateAge() {
age1++;
}
public void updateExp() {
experience++;
}
public String convert(int ht) {
String out = "" ;
int ft = ht/ 12 ;
int in = ht % 12 ;
if( ft > 0 ) out = ft + "' " + in + "\" " ;
else out = ht + "\"" ;
return out ;
}
}
If anyone wants to know my directions for this assignment to better put my question into perspective, I am more than happy to post that as well. I just don't want to solely get the answer, I would like help and guidance through this because I'm stuck on the String toString() part.
If anyone is truly interested in really helping me out step by step, I would truly appreciate it. I've been working on this all darn day and this computer screen has been giving me the biggest migraine of my entire school-quarter. :s:
Re: Trouble with understanding String toString() method.
Quote:
I'm stuck on the String toString() part.
Are you talking about a method named toString() that is supposed to return a String value?
When you reference an instance of a class, the compiler will call that class's toString() method. The method should return a String with some values from the class. What it returns is up to the programmer to decide.
When you print a Player object, what would you like to see printed?
For testing, create an instance of the Player class and call println with it to see what happens.
Re: Trouble with understanding String toString() method.
I'm actually looking to print out the contents of a file but in a sort of neat fashion. The file itself contains information about NBA players (specifically the Chicago Bulls team). This is an example of how it one line of information looks in the text file: 1 Rose,Derrick PG 75 190 23 3 Memphis 6993708 .
The text file shows the player's number, the player's name, the player's position, height, weight, age, experience, college attended, and salary, respectively.
So I want to print all of this information to a new file but in this order, rather, "Name : ", "Number: ", "Position: ", "Height: ' '' ", "Weight: lb.", " Age: ", "Experience: ", " College: ", and finally "Salary: ".
So in essence I am only switching the player's name and his position to print out but I am also trying to include the symbols for feet and inches within the height and lb for the weight.
I thought I would be able to do this easily but I'm actually very stumped.
I thought it would be a simple with something like:
return String.format("Name: %s \t Number: %3d\n Position: %s \nHeight : %2d'%2d'' \t Weight: %10.2f lb\n Age: %2d \t Experience : %2d \n College: %s \n Salary : %s\n", name, pos, ht, wt, age, exp, col, sal);
Have yet to test it but I feel I am missing something.
Re: Trouble with understanding String toString() method.
Can you show the program's current output
and then show what you want it to be.
Also post the code that is creating the output.
Re: Trouble with understanding String toString() method.
I actually can't provide that just yet because I haven't completed the main driver for the Player class. Sorry, but I will try and work on that. I have the classes to the files and I decompiled them to take a look at the codes. It helps but then it doesn't help because I'm seeing code we've never discussed in class and a lot of it seems so unnecessary. So I'm trying to make it simpler but I continue to confuse myself. I s'pose that's what I get for trying to look at one of many answers to this assignment.
Re: Trouble with understanding String toString() method.
Quote:
I actually can't provide that
How do you know the output is not what you want if you have never seen it?
Re: Trouble with understanding String toString() method.
Well for one I don't think the "ht" is going to be output in the fashion that I want it to.
Even though I have a code for it (this was provided in the template the professor gave)
Code:
public String convert(int ht) {
String out = "" ;
int ft = ht/ 12 ;
int in = ht % 12 ;
if( ft > 0 ) out = ft + "' " + in + "\" " ;
else out = ht + "\"" ;
return out ;
}
I don't think "ht" will be converted in the toString() method. If anything, the ht would possibly come out twice. Say in the file it reads "75" then the output should be "Height: 76' 76'' "
Re: Trouble with understanding String toString() method.
Write some test code, compile it, execute it and see what it does.
Re: Trouble with understanding String toString() method.
okay, this is what I have for my main driver:
Code:
import java.util.*;
import java.io.IOException;
import java.io.*;
import java.text.*;
class hw7{
Player [] team = new Player[20];
public static Player load( Scanner sc ){
// this method uses scanner class object sc to read one player's information from the roster file and returns a player object
//Player p;
String name ;
String position ;
int height ;
double weight ;
int age ;
int exp ;
String college ;
double salary ;
int no ;
int size = 0;
Scanner sc = new Scanner (System.in) ;
while ( sc.hasNext() ); {
no = sc.nextInt();
name = sc.next();
position = sc.next();
height = sc.nextInt();
weight = sc.nextDouble();
age = sc.nextInt();
exp = sc.nextInt();
college = sc.next();
salary = sc.nextDouble();
team[size++] = new Player( no, name, position, height, weight, age, exp, college, salary);
}
}
public static void main(String [] args) {
load ();
File in ;
PrintWriter pw = new PrintWriter(System.out, true) ; ;
try {
if ( args.length > 0 ) {
sc = new Scanner(new File(args[0]));
if ( args.length == 2 ){
pw = new PrintWriter(new FileWriter(args[1]),true);
}
}
}catch(IOException e ) {
System.out.println("Error in openning file ....\n") ;
}
NumberFormat currencyFormatter= NumberFormat.getCurrencyInstance();
int i = 0;
double a = 0.0;
double a1 = 0.0;
double a2 = 1000000000;
double a3 = 0.0;
double a5 = 0.0;
double a6 = 0.0;
double a7= 0.0;
double a8 = 0.0;
int b = 0;
int c = 0;
int d = 0;
int i1 = 0;
int c1 = 0;
for(; sc.hasNext() && i < 20; i++) {
team[i] = load();
double a4 = team[i].getSalary();
if(a4 > a1) {
a1 = a4;
b = i;
} else
if(a4 < a2) {
a2 = a4;
c = i;
}
a = a + a4;
int b1 = team[i].getHeight();
a5 = a5 + b1;
if(b1 > l) {
d = b1;
i1 = i;
}
double a9 = team[i].getWeight();
if(a9 > a7) {
a7 = a9;
c1 = i;
}
a6 = a6 + a9;
}
double a10 = a / (double)i;
a6 = a6 / i;
a5 = a5 / i;
for(int d1 = 0; d1 < i; d1++)
pw.println(team[d1]);
pw.printf("\n\n\tThe average pay is %15s\n", currencyFormatter.format(a10));
pw.printf("\tThe maximum pay is %15s (%s)\n", currencyFormatter.format(a1), team[b].getName());
pw.printf("\tThe minimum pay is %15s (%s)\n", currencyFormatter.format(a2), team[c].getName());
int i2 = (int)a5;
int b2 = (int)(a5 * 10);
b2 = b2 % 10;
double a11 = ((i2 % 12) * 10 + b2) / 10;
pw.printf("\n\tThe average height is %2d' %3.1f''\n", double int (i2 / 12));
pw.printf("\tThe tallest is of height %2d' %2d'' (%s)\n\n", int (d / 12), int (l % 12), team[i1].getName());
pw.printf("\tThe average weight is %4.1flb\n\n", double a6);
pw.printf("\tThe heaviest is %4.1flb (%s)\n\n", double a7, team[c1].getName());
}
but I receive the following errors:
hw7.java:130: '.class' expected
pw.printf("\n\tThe average height is %2d' %3.1f''\n", double int (i2 / 12));
^
hw7.java:130: ')' expected
pw.printf("\n\tThe average height is %2d' %3.1f''\n", double int (i2 / 12));
^
hw7.java:132: '.class' expected
pw.printf("\tThe tallest is of height %2d' %2d'' (%s)\n\n", int (d / 12), int (l % 12), team[i1].getName());
^
hw7.java:132: ')' expected
pw.printf("\tThe tallest is of height %2d' %2d'' (%s)\n\n", int (d / 12), int (l % 12), team[i1].getName());
^
hw7.java:134: '.class' expected
pw.printf("\tThe average weight is %4.1flb\n\n", double a6);
^
hw7.java:134: ')' expected
pw.printf("\tThe average weight is %4.1flb\n\n", double a6);
^
hw7.java:136: '.class' expected
pw.printf("\tThe heaviest is %4.1flb (%s)\n\n", double a7, team[c1].getName());
^
hw7.java:136: ')' expected
pw.printf("\tThe heaviest is %4.1flb (%s)\n\n", double a7, team[c1].getName());
^
hw7.java:138: '}' expected
^
9 errors
Why must I need ".class" there?
Re: Trouble with understanding String toString() method.
You should not code the datatypes: (int, double) in the argument list for the methods. Put only the variable names.
Re: Trouble with understanding String toString() method.
Well it was a step in the right direction. But now I received more errors concerning that it cannot find the variable symbols.
hw7.java:26: sc is already defined in load(java.util.Scanner)
Scanner sc = new Scanner (System.in) ;
^
hw7.java:39: non-static variable team cannot be referenced from a static context
team[size++] = new Player( no, name, position, height, weight, age, exp, college, salary);
^
hw7.java:46: load(java.util.Scanner) in hw7 cannot be applied to ()
load ();
^
hw7.java:57: cannot find symbol
symbol : variable sc
location: class hw7
sc = new Scanner(new File(args[0]));
^
hw7.java:88: cannot find symbol
symbol : variable sc
location: class hw7
for(; sc.hasNext() && i < 20; i++) {
^
hw7.java:89: non-static variable team cannot be referenced from a static context
team[i] = load();
^
hw7.java:89: load(java.util.Scanner) in hw7 cannot be applied to ()
team[i] = load();
^
hw7.java:90: non-static variable team cannot be referenced from a static context
double a4 = team[i].getSalary();
^
hw7.java:100: non-static variable team cannot be referenced from a static context
int b1 = team[i].getHeight();
^
hw7.java:106: non-static variable team cannot be referenced from a static context
double a9 = team[i].getWeight();
^
hw7.java:118: non-static variable team cannot be referenced from a static context
pw.println(team[d1]);
^
hw7.java:122: non-static variable team cannot be referenced from a static context
pw.printf("\tThe maximum pay is %15s (%s)\n", currencyFormatter.format(a1), team[b].getName());
^
hw7.java:124: non-static variable team cannot be referenced from a static context
pw.printf("\tThe minimum pay is %15s (%s)\n", currencyFormatter.format(a2), team[c].getName());
^
hw7.java:132: non-static variable team cannot be referenced from a static context
pw.printf("\tThe tallest is of height %2d' %2d'' (%s)\n\n", (d / 12), (d % 12), team[i1].getName());
^
hw7.java:136: non-static variable team cannot be referenced from a static context
pw.printf("\tThe heaviest is %4.1flb (%s)\n\n", a7, team[c1].getName());
^
./Player.java:22: cannot find symbol
symbol : variable pos
location: class Player
return pos;
^
./Player.java:26: cannot find symbol
symbol : variable ht
location: class Player
return ht;
^
./Player.java:30: cannot find symbol
symbol : variable wt
location: class Player
return wt;
^
./Player.java:43: cannot find symbol
symbol : variable col
location: class Player
return col;
^
./Player.java:47: cannot find symbol
symbol : variable sal
location: class Player
return sal;
^
./Player.java:60: cannot find symbol
symbol : variable pos
location: class Player
position = pos;
^
./Player.java:63: cannot find symbol
symbol : variable ht
location: class Player
height = ht;
^
./Player.java:66: cannot find symbol
symbol : variable wt
location: class Player
weight = wt;
^
./Player.java:73: cannot find symbol
symbol : variable experience
location: class Player
experience = exp;
^
./Player.java:76: cannot find symbol
symbol : variable col
location: class Player
college = col;
^
./Player.java:79: cannot find symbol
symbol : variable sal
location: class Player
salary = sal;
^
./Player.java:82: cannot find symbol
symbol : variable number
location: class Player
number = no;
^
./Player.java:99: cannot find symbol
symbol : variable experience
location: class Player
experience = 0;
^
./Player.java:102: cannot find symbol
symbol : variable number
location: class Player
number = 0;
^
./Player.java:118: cannot find symbol
symbol : variable number
location: class Player
number = 0; // no need for the player's number
^
./Player.java:129: cannot find symbol
symbol : variable experience
location: class Player
experience = exp;
^
./Player.java:132: cannot find symbol
symbol : variable number
location: class Player
number = no;
^
./Player.java:143: cannot find symbol
symbol : variable experience
location: class Player
experience = other.experience;
^
./Player.java:143: cannot find symbol
symbol : variable experience
location: class Player
experience = other.experience;
^
./Player.java:146: cannot find symbol
symbol : variable number
location: class Player
number = other.number;
^
./Player.java:146: cannot find symbol
symbol : variable number
location: class Player
number = other.number;
^
36 errors
Re: Trouble with understanding String toString() method.
What have you done to the code now?
I suggest that you strip the code down to 20 lines that compile. Then add a few lines and compile it again. Fix the errors, compile again. When those errors are fixed, add a few more lines, compile, fix errors and continue doing the same until all of the program is typed in.
You should never type in over 100 lines of code without compiling. Start small and compile often.