Input From Different Classes - Cannot Cast Int To String
Oct 18, 2014
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'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'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"); %>
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)
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 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??
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');
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 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);
I trying to get this code to get user input instead of reading from a hardcoded array. I'm getting compile errors while trying to get user input. Here's some of the code:
I have to take a users input. The general gist of the problem is I want to convert a decimal number entered by the user inputs into it's binary equivalent....the conversion part I know how to do.
I'm stuck at the user input phase. If the user inputs a decimal number within the correct range (lets say between 0-5000 for this example) the conversion goes ahead as planned and the program outputs on the screen the binary equivalent.
If on the other hand the user inputs a number outside of this range ... OR a String OR and empty space, an error message is given and the user is asked to try again. I don't know how to handle it if the user enters the different types...int or String.
I am trying to create a fortune teller and everything is running fine except the program does not prompt you to answer the questions
Do you like donuts? and What did you have for breakfast?
Here is the code this is in Bluej :
import java.util.Scanner; public class FortuneTeller { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.println("Welcome to Fortune Teller");
I am trying to make a program in which first I am entering number of charachters and then in nextline their is exactly that number of characters should be enter after than program should stop taking input from console..this is I have try so far
I am supposed to write a program on PuTTY using UNIX (I have Windows 8), but I am not comfortable with it yet, so I am using Java through NetBeans (IDE 7.4).
The program has to follow these instructions (ignore the Linux part of the instructions, the rest is in bold):
Write a program on the Linux system using the putty utility. The program should get strings of data from the command line (that is, look for the data in the "args" array of strings). Use a loop to convert each of the strings in the array into a double and add the number to a total. Print the total after all of the strings have been processed.
The program will use try-catch to catch any error that occurs. If an error occurs, the program will print a message saying that the error occurred. The program can end at that point.
You should create the Java program using the nano editor. The input data should be a list of numbers on the line that runs the program.
The problem so far is that I keep getting an error when converting a String value into a new double value. I have yet to code the try-catch method in my program
Java Code:
import java.util.*; public class Program13Strings { public static void main(String[] args) { Scanner scan = new Scanner (System.in); System.out.println("How many lines of data do you wish to enter?"); int size = scan.nextInt();
[Code] ....
*NOTE: right where my code says double newDouble = Double.valueOf(newResponse); is where the error is occurring.
*ERROR: Exception in thread "main" java.lang.NumberFormatException: For input string: "abc"
at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1241) at java.lang.Double.valueOf(Double.java:504) at program13strings.Program13Strings.main(Program13St rings.java:21)
I have to write a program that inputs a string and tests whether or not it is a palindrome. This worked fine, and now I want to make it so I continually enter strings until I tell the program to stop.
The code below compiles just fine, but it doesn't do what I want. Why it doesn't work how I think it should work? (Typing in STOP does not make the program stop.)
Here is the code
import javax.swing.JOptionPane; public class PalTest { public static void main(String[] args){ String S="pony"; while(S!="STOP"){ S=JOptionPane.showInputDialog("Enter a string (STOP to terminate):");
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'm having an issue with my program that needs 2 lines of input when I only want one. First I call Scan.nextLine(); to fetch a word. Then I want Scan.findInLine(word).charAt(number); to scan the string word for a letter instead of having to put another line in the console. How do I make Scan.findInLine(word).charAt(number); scan a single String instead of scanning input typed by the user?
I am having trouble with getting a user input once they enter it in to a JTextArea and then storing it in to a string value. See, I wanted the user to enter an answer in to the JTextArea, and then once I store it in to a string value, I would be able to use it later. I wanted to after storing the variable to print it on to a JLabel, but nothing is showing up... Heres my code.
static String usera[]= new String [10]; for (int i=0; i< usera.length; i++) { //These are the JTextArea names usera[0]= Question1.getText(); usera[1]= Question2.getText();
[Code].....
I already initialized the JLabels earlier in my code, and I did not feel the need to post all my code here. This is where I am trying to get the string variable and then printing it out.
I am having issues with the program below everything works but I can't figure out a way to add code that if a user just hits enter without inputting anything it says "entering in nothing is not a valid choice" I am stuck on how to compare a int to a string ...
//import statements import java.util.*; //for scanner class // class beginning public class Guess { public static void main(String[] args ) { //Declare variables area