ToString Method Won't Work
Oct 4, 2014
So I'm working on a project and noticed that my toString() method won't work. This is just an example of the type of code that I have in my real project. THIS IS MY MAIN CLASS
XML Code:
package trialanderror;
import java.util.Scanner;
public class TrialAndError {
public static void main(String[] args) {
Scanner keys = new Scanner(System.in);
String name;
String phonenumber;
[code]....
View Replies
ADVERTISEMENT
Apr 12, 2012
How to overwrite a toString method
View Replies
View Related
Apr 22, 2014
My current assignment involves me outputting these 2 classes. Yet I'm not really sure in what manner I should go about doing this. I have tried creating a separate class and outputting my toString methods there but for some reason I am getting an error. .
The error message is thus;
Exception in thread "main" java.lang.NullPointerException
at Vehicle.toString(Vehicle.java:91)
at Run.main(Run.java:17)
Process completed.
import java.util.Calendar;
public class HireContract {
String reference;
Calendar startDate = null;
Calendar endDate = null;
Vehicle vehicle = null;;
String customerRef;
double rate;
[Code] .....
View Replies
View Related
Nov 10, 2014
Write TestCabAppointment,java class where you will instantiate new CabAppointment objects and read data from RandomAccessFile and create CabAppointment objects and save them in RandomAccessFile You may use FixedLengthStringIO,java class, ICabAppointmentRecord.java interface. Complete the ReadWriteRandomAccessFile.java
CabAppointment.java (this class extends Appointment class)
private int id;
private Address startAddress;
private Address destinationAddress;
private String terminal;
private String description;
[Code].....
View Replies
View Related
Apr 7, 2014
I am looking to pass a long list of results in a String toString() method,this is my code
public void newlist(){
for(int i = 0 ; i <= nbComposant;i++){
System.out.print(ref+i+" (quantity "+quantity+i+")");
}
}
public String toString(){
return newlist();
}
View Replies
View Related
Mar 14, 2014
How do i print override the toString for WebBrowser as i would like to print out the object bc. Tested the program and it is fine if i put it in the main method rather than the WebBrowser constructor.
import java.util.*;
class ListNode <E> { /* data attributes */
private E element;
private ListNode <E> next;
/* constructors */
public ListNode(E item)
{ this(item, null);
[code]...
View Replies
View Related
Oct 21, 2014
So I have to write all the methods for a LinkedListQueue. I've got isEmpty, enqueue and dequeue working correctly (I think) but I'm having trouble with the toString method. I tried to do it recursively and it works if there is only one element in the list, but with multiple elements it throws a StackOverflowerror exception. I've tried it multiple different ways, but I can't seem to figure out how to print it out with out clearing everything. We haven't been taught StringBuilder or .append yet, which I saw a lot of as I was looking for solutions, so I can't use those.
public class LinkedQueue<T>
{
protected LLNode<T> front; // reference to the front of this queue
protected LLNode<T> rear; // reference to the rear of this queue
private T info;
public LinkedQueue()
{
front = null;
rear = null;
[Code] ....
and this is the ITD used with it, for some reason it has the "empty the queue" function as a choice but we weren't assigned that function, so just ignore it.
import java.util.Scanner;
public class ITDLinkedQueue
{
public static void displayMenu()
{
System.out.println("(1) display menu");
System.out.println("(2) check isEmpty");
System.out.println("(3) enqueue");
System.out.println("(4) dequeue");
[Code] ....
View Replies
View Related
Feb 20, 2014
Our goal is to write a pretty simple program, one that takes the 12 digit UPC code entered by a user and to not only spit it back out in a format with dashes using toString, and also returns the first digit, a 2 more groups of digits numbering 2-6 and 7-11, and finally display the 12th digit. It then performs an equation to check the last digit and make sure the UPC code is correct.
However, being so new to java (I only learned visual basic before), with this I was introduced to two new concepts that for some reason I simply cannot grasp for the life of me: Using and calling the toString method, and calling on methods that are created in a completely different class file.
The first section of code is my UPC class, which is meant to contain all my methods as well as the toString to be called on:
public class UPC
{
// Instance variables
private int itemType; // digit 1
private int manufacturer; // digits 2,3,4,5,6
private int product; // digits 7,8,9,10,11
private int checkDigit; // digit 12
[Code] .....
View Replies
View Related
Jan 19, 2014
Write a class encapsulating the concept of a course grade, assuming a course grade has the following attributes: a course name and a letter grade. Include a constructor, the accessor and mutator, and methods toString and equals.Write a client class to test all the methods in your class.
how to test and finish the toString and equals method in this code ?
package labmodule7num57;
import java.util.*;
public class LabModule7Num57 {
// Constructors//
private String name;
private String letterGrade;
public LabModule7Num57 (String name,String letterGrade) {
[code]....
View Replies
View Related
Aug 11, 2014
I need making the toString() method return a String rather than display a message to the screen. Also, I'm not supposed to call the toString method in my demo class to test it, so what should I do instead?
public class cupDispenser {
String location;
int noOfCups;
cupDispenser(String location,int cups)
{
this.location=location;
this.noOfCups=cups;
}
public String getlocation()
[Code]...
View Replies
View Related
Jul 15, 2014
I am following this article [URL] .....
And here is my code
public class SimpleThreads {
// Display a message, preceded by
// the name of the current thread
static void threadMessage(String message) {
String threadName =
Thread.currentThread().getName();
[Code] .....
I want to know How join() and interrupt() method is working on this program?
View Replies
View Related
Nov 6, 2014
I have a paint program in Java where I can draw objects. The objects are stored in an arrayList. In the menubar the user can chooce "Back", which means the last item in the arrayList is removed. After that I want the program to loop through the arrayList and draw the remaining items.
My problem is that when I try to do that it will not work from the menu (menuItem2). If I instead add the code to one of the colorpanels (yellowPanel), from where the user can pick colors, it works fine.
menuItem2 uses ActionListener and yellowPanel uses MouseListener.
public class PaintProgram extends JFrame implements ActionListener {
public ArrayList<Draw> shapeList = new ArrayList<>();
int startX, startY, endX, endY, w, h;
private JPanel topPanel;
private JPanel bottomPanel;
private JPanel magentaPanel;
private JPanel greenPanel;
private JPanel bluePanel;
private JPanel blackPanel;
[code]....
View Replies
View Related
Dec 1, 2014
This was shown in class the other day and I'm still not sure of how exactly it works, even after looking at it through a debugger.
public void sort(){
for (int i = 0; i < size; i++) {
Node temp1 = head;
Node temp2 = head;
Node temp3 = head;
while(temp2.next!=null){
temp3 = temp2.next;
[Code]...
Again, I didn't write this code I'm just trying to understand it. Why specifically do three different temp values need to be used instead of just one like in most sorts?
It is part of a larger program that can be found below.
import java.util.Random;
public class MyQueue implements IntegerQueue{
class Node{
Node next;
Integer data;
Node(Integer data){
this.data = data;
[Code]....
View Replies
View Related
Mar 28, 2014
I faced with a problem, when I use method reference as a function in Collectors.groupingBy I get an error: "no suitable method found for collect". But! if I replaced method reference with a lambda expression everything works fine.
Here is the code sample:
interface Iface{
public int getPropertyOfClassA();
public void setPropertyOfClassA(int propertyOfClassA);
}
class A implements Iface{
private int propertyOfClassA;
[Code] ....
Change "C::getPropertyOfClassA" with "objC -> objC.getPropertyOfClassA()" and it works!
View Replies
View Related
Sep 1, 2014
I am trying to remove the duplicate elements from ArrayList using .contains() if elements are primitive datatype it works but user-defined datatype does not work.
public class UserBean {
String name;
String address;
public String getName() {
return name;
[code]....
View Replies
View Related
Jul 15, 2014
how to write the toString to convert this output :
List:
side: 14 color: java.awt.Color[r=0,g=0,b=255]
side: 18 color: java.awt.Color[r=255,g=0,b=0]
side: 12 color: java.awt.Color[r=255,g=255,b=0]
side: 18 color: java.awt.Color[r=255,g=0,b=0]
side: 16 color: java.awt.Color[r=0,g=255,b=0]
side: 10 color: java.awt.Color[r=255,g=200,b=0]
side: 10 color: java.awt.Color[r=255,g=200,b=0]
to this output
side:14 color:#0000FF
side:18 color:#FF0000
side:12 color:#FFFF00
side:18 color:#FF0000
side:16 color:#00FF00
side:10 color:#FFC800
side:10 color:#FFC800
here is how i have it written now
public String toString()
{
return "side: " + side + " color: " + color + "";
}
View Replies
View Related
Apr 21, 2014
I am trying to display the contents of my Object but when I compile it I get this:proj4.TellerQueue@1db9742
I am pretty sure it's because I havent created my own toString method but I could be wrong. If this is the case, what do I need to do to properly display the variables of my object? Right now my class, BankQueue, contains another object called Person, which contains a String and int variables. I want to do something like this:
class BankQueue{
Queue<Person> q = new LinkedList<Person>();
public String toString(){
return q.;//display contents of the q
[code]...
but Im not sure how to convert these into Strings.
View Replies
View Related
Mar 3, 2015
I really want to know it. When we write ...
>>> Student s = new Student("Roll No", "Name");
>>> System.out.println(s);
Then it gives output according to the way that toString() of Student is implemented,right ? What if I wanna do the same for arrays?? I mean to ask, let us say I have array int[] arr = { 1, 2, 3, 4, 5}. When I say System.out.println(arr), it should give the output as it gives for collections [1,2,3,4,5].
I know that Arrays.toString(int[]) can work for us in this regard. But my question is that can we write our own toString() method so that when I print the "arr" (an Object itself) it should call the toString() that we implemented like a callback function.
View Replies
View Related
Jan 26, 2015
I've been trying to make a class that has a toString method that displays the board at the same time displays the value of each index of a 2d character array inside of the code. My professor has made the client print the method. I assumed since he was printing it on the client we had to return a string that gives us this.The number 2 is part of the 2d character array while the rest of the index values are just space characters.
___________
| | | |
|___|___|___|
| | | |
|___|___|___|
| | | |
|___|___|___|
| | | |
|___|___|___|
| 2 | | |
|___|___|___|
I had trouble figuring out how to return this so I started testing it out in another file.
public static void main(String[] args) {
// TODO Auto-generated method stub
char[][] myHidingPlaces;
myHidingPlaces = new char[5][3];
myHidingPlaces[0][0] = 'p';
char play = '1';
for (int i = myHidingPlaces.length-1; i >= 0 ; i--) {
[code]...
View Replies
View Related
Apr 27, 2015
To string Returns the last character doubled ... How do i stop it from doubling the last number?
public class CreditCard
{ char c = 0;
public CreditCard(String cardNumber)
{
for( int i=0; i<cardNumber.length(); i++)
{
c = cardNumber.charAt(i);
[Code] ....
View Replies
View Related
Jan 2, 2015
what im trying to do is modify the Coin class to override the toString() method so that it indicates whether the coin is face up or face down. For example, "The coin is face up." This is what i have so far:
public class Coin {
public static void main(String[] args) {
Coin coin = new Coin();
for (int i = 0; i < 1; i++) {
coin.flip();
System.out.println(coin);
[Code] .....
View Replies
View Related
May 22, 2014
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.lang.Comparable;
import java.util.*;
import java.lang.*;
public class MyLinkedListSort {
[Code] .....
Output is :
name is nulland salary is0
name is nulland salary is0
name is nulland salary is0
name is nulland salary is0
Expected output :
Name is Crish Salary: 2000
Name is Tom Salary: 2400
Name is Ram Salary: 3000
Name is John Salary: 6000
Where i going wrong
View Replies
View Related
Apr 19, 2015
Trying to convert 2D array to String using toString() to be able to print the array but when I try to use it I just get the memory location
public class Forest
{
private int h;
private int w;
private double p = 0.7;
private int[][] f;
Forest(int w, int h)
[code]....
View Replies
View Related
Jun 18, 2014
The problem is as follows: I've been running java script using git bash and at one point it tells me that I need to increase heap size because apparently it takes more than 1gb to run. However although I manually increase it via control panel>programs>java nothing changes. I've been told that I need to ' figure out how to use 64bit java (and a VM that supports 64bit) in order to increase the maximum memory allocation (heap size) to something north of 2g.'
My laptop has 8GB worth of RAM (Windows 7)so it shouldn't technically have problems running anything above 1G. But there is apparently a 1GB wall But increasing the heap size to above 2G results in an error stating that VM doesn't initialize within git bash, I thought that my java is 32 bit so I downloaded a 64 bit java. Now how and if it is even possible to allocate a program to run under 64 bit java and if I should delete the other java version.
View Replies
View Related
Dec 13, 2014
how does recursion works, I don't understand why it prints al numbers going up again?This is the code
void print2(int n){
if (n<0){
out.printf(" %d",-1);
return;
}
out.printf(" %d", n);
print2(n-1)
out.printf(" %d", n);
}
this should be the output if n is 6: 6 5 4 3 2 1 0 -1 0 1 2 3 4 5 6.
View Replies
View Related
Mar 6, 2015
I need to create a chart for work and comes across this sample from mbostock's chord diagram (bl.ocks.org/mbostock/4062006).
I only need to revise the tick label (names, display positions) but I don't know how to do it as I have 0 experience with Java.
My sample data for the var matrix is
[0,100,100,100]
[0,0,99,98]
[0,0,92,84]
[0,99,0,0]
which shows flows between country A, B, C and D.
I would like to revise the tick labels, so that:
1) Instead of showing 0K 5K 10K, I would like to show the country names (A - D), with no number or tick. I figured out this one by just commenting out all the codes relating to ticks.
2) Place the country names in the center of the arch. I don't know how to do this one.
View Replies
View Related