Im trying to loop through a hashmap of objects. They are defined as People objects. People has two subclasses , Instructor and Student. As I am looping through the map of People, I am searching for class Instructor. If I find it, I want to access its method getDepartment in a println by casting to Instructor. When I do I get a runtime error:
Exception in thread "main" java.lang.ClassCastException: java.util.HashMap$Node cannot be cast to uStaff.Instructor
at uStaff.PersonApp.menu(PersonApp.java:108)
at uStaff.PersonApp.main(PersonApp.java:21)
//Instantiate the different Person, student and instructor objects
Person thisPerson = new Person(01,fName,mName,lName,email,ssn,age);
Student thisStudent = new Student(02,"Stacey","Marie","Morgan","smorgan@gmail.com","213-45-6789",20);
thisStudent.setMajor("music");
Instructor thisInstructor = new Instructor(03,"Joe","Douglass","Wells","joe@drumhaven.com","555-98-3029",46);
thisInstructor.setDepartment("Computer Science");
I want to write formatted output on a notepad file using ObjectOutputStream but I am not getting it in human readable formatted form
Here is my person class
public class Person implements Serializable { private String firstName; private String lastName; private int age; public String getFirstName() { return firstName;
[code]....
I want to know how to use printStream.print() like method to write formatted output.
I'm developing an application to track the status of a production flow-line and have hit a bit of a snag. When attempting to read saved data I run into this:
Exception in thread "main" java.lang.ClassCastException: flowline.End_Of_File cannot be cast to flowline.Operation at flowline.Station.checkLoadPreviousStationStatus(Station.java:91) at flowline.Station.main(Station.java:212) Java Result: 1
I've been reading up on different methods to saving and retrieving data and have decided ObjectInputStream would be the best option.
The save method works fine, I opted to use a EndOfFile class to determine when I've reached the end of the input stream. The problem is, when my loop encounters this object, it doesn't terminate the loop.
public void checkLoadPreviousStationStatus() throws FileNotFoundException, IOException, ClassNotFoundException, EOFException, TempArrayOutOfBoundsException{ Object loadOpn = null; End_Of_File eof = new End_Of_File(); File f = new File(fileName);
[Code] .....
The Operation cast is a cast to the objects my LinkedList contains. The highlighted line is where the exception occurs.
I am getting "Type safety: Unchecked cast from Object to LinkedList<EventData>" in eclipse for a piece of code stated below
public LinkedList<EventData> loadFromFile(File file) { queue=new LinkedList<EventData>(); //Some piece of code return (LinkedList<EventData>)queue.clone(); //--->getting warning here }
I know that because clone() method is returning Object, hence compiler doesn't have type information that's why showing warning. I don't want to suppress this warning instead i want to fix it.
I ve got a 2d array and I want to cast it in an 2d arraylist. I ve create a function that cast an array to arraylist. My problem arises, when I tried to parse the whole 2d matrix to the arraylist. I use the following code:
Java Code: double sums[][] = computeSums(lab, projections); ArrayList<ArrayList<Double>> lists = new ArrayList<ArrayList<Double>>(); ArrayList<Double> nu = new ArrayList<Double>(); System.out.println(sums[0].length); for (int i = 0; i < sums.length; i++) {
The problem is that only the first matrix sums[0] is copied to the 2d arraylist sums.length times. How is is possible to store all the different sums matrices to the arraylist??
I'm actually trying to complete the excersise of the Servlets and JSP book in page 303 but I'm getting the following error in Eclipse Cannot cast from String to ArrayList(JSP).Here is the code
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ page import="java.util.*" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Hobbies Sharing</title> </head>
[code]...
The error as it appears in line <% ArrayList al = (ArrayList)request.getParameter("Names"); %>
When does an internal cast actually happen? I am aware that compound assignment operator do an internal cast. Does it happen in Assignment 1?Assignment 2?Assignment 3?Assignment 4?
Java Code:
public class Parser{ public static void main( String[] args){ byte b=1; short s=1; int i=1; s=b;//Assignment 1 s<<=b;//Assignment 2 b<<=s;//Assignment 3 s +=i;//Assignment 4 } } mh_sh_highlight_all('java');
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean at javax.swing.plaf.synth.SynthTableUI$SynthBooleanTa bleCellRenderer.getTableCellRendererComponent(Synt hTableUI.java:731) at javax.swing.JTable.prepareRenderer(JTable.java:573 1) at javax.swing.plaf.synth.SynthTableUI.paintCell(Synt hTableUI.java:684)
I am working on a program where I want the user to input multiple classes.
One int, and one String.
Can this be done? if so, how?
I have a tried to get input from both, like in the code below:
Java Code:
import java.util.Scanner; public class ForumFlowchart { public static void main(String[]args){ //Creating scanner. Scanner input = new Scanner(System.in); //Get information about job System.out.println("Type in Int"); int i1 = input.nextInt();
[Code] ....
If I keep my input in the same class, I get the error "Can not cast int to string". My question is, it is possible to get an input from both an Int and a String in the same program?
I have a msg object that contains an ArrayList<Integer> collection. However, in order to send the elements in the array over the udp socket, it needs to be sent as a byte[] array. So why am I using ArrayList<Integer> over byte array in first place? Well when I receive data from socket from embedded c program, I need to get an unsigned representation of the data, and thus I need to store it in integers, since bytes in Java are unsigned and unsigned chars in c that are greater than 127 will yield incorrect values in java. But when I send an ack back over the socket, I need to send the data back as bytes. So I convert the ArrayList<Integer> to a byte array:
Java Code: byte[] data = msg.toByteArray(); DatagramPacket response = new DatagramPacket(data, data.length, packet.getAddress(), packet.getPort()); public class Gprs { ... public byte[] toByteArray(){
[Code] ....
The problem is I get an "Cannot cast from Integer to byte" when trying to cast the integer to byte: data[i] = (byte)m_data.get(i);
Create an equals method that takes an object reference and returns true if the given object equals this object.
Hint: You'll need 'instanceof' and cast to a (Geocache)
So far I have:
public boolean equals(Object O){ if(O instanceof Geocache){ Geocache j=(Geocache) O; if (this.equals(j)) //I know this is wrong... but I can't figure it out return true; }
else return false; }
I think I have it correct up to the casting but I don't understand what I'm suppose to do with the this.equals(). Also I'm getting an error that I'm not returning a boolean... I get this all the time in other problems. I don't get why since I have to instances of returning booleans in this. "returns true if the given object equals this object" makes no sense to me. I assume the given object, in my case, is 'O'. What is 'this' object referring to?
I am new to Java and have read books, the Java docs, and searched the Internet for my problem to no avail. I have an Array of objects that contains strings. How can I get the object's strings to print in a list so that the user can select that object to manipulate its attributes? For example, the user can select "Guitar 1" from a list and manipulate its attributes like tuning it, playing it, etc. I have a class called Instruments and created 10 guitar objects.Here is the code:
Instrument [] guitar = new Instrument[10]; for (int i = 0; i < 10; i++) { guitar[0] = new Instrument("Guitar 1"); guitar[1] = new Instrument("Guitar 2"); guitar[2] = new Instrument("Guitar 3"); guitar[3] = new Instrument("Guitar 4"); guitar[4] = new Instrument("Guitar 5"); guitar[5] = new Instrument("Guitar 6");
Now lets say that I want to access a method 'addInterest()' that is in the 'SavingsAccount' class I would have to do: '((SavingsAccount)s).addInterest();'
The question I have is why do I have to cast 'b' to SavingsAccount? Isn't the actual object reference of 'b' already an instance of 'SavingsAccount' class? How does the 'BankAccount' affect the object itself? I'm really confused as to what class is truly getting instantiated and how BankAccount and SavingsAccount are both functioning to make the object 'b'.
I don't understand why the object reference variable 'a' cannot be recast from a thisA object reference to a thisB object reference.Is it the case that once a reference variable is linked to a particular object type then it cannot switch object types later on.I am facing the Java Associate Developer exam soon and I am just clearing up some issues in my head around object reference variable assignment,
class thisA {} class thisB extends thisA { String testString = "test";} public class CastQuestion2 { public static void main(String[] args) { thisA a = new thisA(); thisB b = new thisB();
I am trying to get this to where I can type in a name and it will search through each object and print back the corresponding object info.
Java Code:
import java.util.Scanner; public class MyPeople { public static void main(String[] args) { Person[] p = new Person[] { new Person("Chris", 26, "Male", "NJ", "Single"), new Person("JoAnna", 23, "Female", "NJ", "Single"), new Person("Dana", 24, "Female", "NJ", "Single"), new Person("Dan", 25, "Male", "NJ", "Single"), new Person("Mike", 31, "Male", "NJ", "Married") };
Task:The main method of the class Things below creates an object called printer deriving from the class PrintingClass and uses that object to print text. Your task is to write the PrintingClass class.
Program to complete: import java.util.Scanner; public class Things { public static void main(String args[]) { String characterString; Scanner reader = new Scanner(System.in); PrintingClass printer = new PrintingClass(); System.out.print("Type in the character string for printing: "); characterString = reader.nextLine(); printer.Print(characterString); } }
// Write the missing class here
Note: In this exercise the solution is part of a conversion unit where many classes have been declared. Because of this the classes are not declared as public using the public attribute.
Example output
Type in the character string for printing: John Doe
John Doe
My Class: class PrintingClass { public void print(){ System.out.println(characterString); } }
I have just started working with linked lists. I have a linked list of Objects and I want to be able to search for a specific object. But currently my code continues to return false. Also how would I go about removing the first index of the linked list.
public static void main(String[] args) { LinkedList<Cookies> ml = new LinkedList<>(); int choice = 0; while (choice >= 0) { choice = menu();