Converting String To Animal?
May 21, 2014
In my Java class, the last assignment we had to turn in was to create a program that would take a users input and create a new type of animal. I went through the different steps in the instructions, and everything seemed to be going well up until the point where we actually took the users input and created the new animal. Specifically, I kept getting an error stating that I could convert a String to an animal.
I'm attaching the main class (Animal), one of my animal classes (Bird) and the class where I'm getting the error (AnimalStuff). I turned the program in as is, so what's done is done, but I'd like to learn what I'm doing wrong and what I can do better. I do know that the loop in AnimalStuff is wrong as it doesn't work right either, but I'm more concerned about the conversion from String to animal, as the entire program depended on this to work, and it doesn't.
public class Animal {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
}
public static String kind;
public static String integument;
[code]....
Is there anything that jumps out at to what I'm doing absolutely wrong?
View Replies
ADVERTISEMENT
Feb 15, 2014
I have this project due and its asking that i print out what type of triangle it is when the user inputs 3 sides. I have most of it done and working, but it pops up different windows instead of using one window for everything. The assignment says it needs all the final info to be in one window. The boolean is coming from another method. I'm unsure how to get it into a string (Or if that's what i have to do). The method must return a boolean true/false.
import javax.swing.*;
public class Triangle {
public static void main(String[] args) {
int side1 = getSides();
int side2 = getSides();
int side3 = getSides();
[Code] ....
View Replies
View Related
Sep 24, 2014
I've started writing a new program that Scans for some strings. I want to specify a random Integer to those Strings in order to do my desired idea. what should I do?!! my codes are here :
import java.util.Random;
import java.util.Scanner;
public class Draw {
public static void main(String[] args) {
System.out.println("This Program is written to solve little problems in families cause of doing unwanted works!!");
[code].....
now I want to Specify an Integer to each person that has been scanned! for example if the first person is " David " , which is a String, in the next step :
Random randomNumber = new Random();
randomNumber.NextInt(101);
int David = randomNumber.NextInt(101);
I want to what should I do?
View Replies
View Related
Feb 19, 2014
How can i convert date to string. Is this the best way
// Create an instance of SimpleDateFormat used for formatting
// the string representation of date (month/day/year)DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
// Get the date today using Calendar object.Date today = Calendar.getInstance().getTime();
// Using DateFormat format method we can create a string
// representation of a date with the defined format.String reportDate = df.format(today);
// Print what date is today!System.out.println("Report Date: " + reportDate);
View Replies
View Related
Feb 13, 2014
I am converting String to date object while converting this i am getting Run time Exception. Here in this code i am using String tokennizer reading a data from a text file. Here is my code
public static void main(String[] args)throws Exception {
FileReader fr = new FileReader("g:abc.txt");
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
[Code] .....
here is an text file which i am reading
priyaRaoDBA13-APR-6002-JUN-92MahdipatnamHyderabad01-FEB-93 -AdminFpriyaM784223680212348
ArifRizwanDevelopment14-FEB-8812-MAR-12Sathya coloneyHyderabad10-FEB-14 -Software EngineerMasdfghManager7842655502123
AFifRizwanDevelopment14-FEB-8812-MAR-12Sathya coloneyHyderabad10-FEB-14 -Software EngineerMasdfghManager7802655502123
AsifRizwanDevelopment14-FEB-8812-MAR-12Sathya coloneyHyderabad10-FEB-14 -Software EngineerMasdfghManager7842631102123
View Replies
View Related
Sep 8, 2014
I am obviously new to programming and Java so I set myself the goal of creating a very simple auto type style of program so it gets the users input and relays it out again but potentially to another window (I am currently testing to notepad). The reason I am trying to make it is because i thought it could be quite simple and I can build on it as a project to make it better.
The issue I am having is that it outputs the first character to the window I am selected (again testing into notepad) but then stops and doesn't output anything else. I tried to figure out what was going on by putting a System.out.println(arr[6]); after the delay method but it just output a line so almost like what I was putting into the array was only storing the first character of the string? I cannot figure out why that would be...
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.Scanner;
public class MyBot
public static void main(String[] args) throws AWTException {
//initialising robot
Robot r = new Robot();
Scanner input = new Scanner(System.in);
[code]....
View Replies
View Related
May 2, 2014
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)
Java Result: 1
How can this be fixed so I don't get this error?
View Replies
View Related
Feb 13, 2014
I'm using the class below to encrypt and decrypt data in my program. It works rather well with the exception that I can't for the mind of me get the recreation of the key to work. When the program runs the first time, it creates a key and saves it to Data.txt. This part works fine, and there are no errors, the encryption and decryption works until I reload the program and it tries to recreate the key using the String in the .txt.
read = loadFile.readline();
byte[] data = read.getBytes("UTF-8");
key = new SecretKeySpec(data, 0, data.length, "PBEWithMD5AndDES");
I've looked at:
java - convert Byte Array to Secret Key - Stack Overflow
java - Converted secret key into bytes, how to convert it back to secrect key? - Stack Overflow
and a number of other sites which didn't work, both of these however suggested the key = new SecretKeySpec. The actual key became: "[B@79fe3f51". Whereas the recreated key became: "[B@3e1d1648".So they are close, but not the same.I create the original key like this:
key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
Encryption.java:
public class Encryption {
public Cipher dcipher, ecipher;
// Responsible for setting, initializing this object's encrypter and
// decrypter Chipher instances
Encryption(String passPhrase) {
[code]....
View Replies
View Related
Mar 3, 2014
I'm stuck with the following error message when i try to convert a String to XML and back to a String.
Exception in thread "main" org.w3c.dom.DOMException: WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it.
at com.sun.org.apache.xerces.internal.dom.ParentNode.internalInsertBefore(Unknown Source)
at com.sun.org.apache.xerces.internal.dom.ParentNode.insertBefore(Unknown Source)
at com.sun.org.apache.xerces.internal.dom.NodeImpl.appendChild(Unknown Source)
at com.innovatrail.consulting.xml.XMLTEST2.main(XMLTEST2.java:66)
Below is my Java code snippet: The code syntactically correct except line 66. If you comment out line 66 in the source code, everything works just fine. The XML representation of my input string is denoted by INPUT 1 and that of my output string is denoted by OUTPUT 2.
package com.Test.xml;
import java.io.ByteArrayInputStream;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
[Code] ....
INPUT 1:
<Collection>
<BookList>
<Book>
<Title>Wonder Man
</Title>
<Author>John Smith
</Author>
<Category>Fiction
[Code] .....
OUTPUT 2:
<Collection>
<Book>
<Title>Wonder Man</Title>
<Author>John Smith</Author>
<Category>Fiction</Category>
[Code] ....
View Replies
View Related
Feb 6, 2014
I am getting run time Exception while converting String to Number....
public int convertToNumber(String numstr) {
int i = Integer.parseInt(numstr);
return i;
}
Here i am calling above Method convertToNumber().
if (validate.hasNumber(req.getParameter(LRSConstant.MOB_NUM))) {
bean.setMobno(ValidateUtils.getInstance().convertToNumber(
req.getParameter(LRSConstant.MOB_NUM)));
}
This is an Exception which i am getting at Run time
java.lang.NumberFormatException: For input string: "9700636702"
java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
java.lang.Integer.parseInt(Integer.java:461)
java.lang.Integer.parseInt(Integer.java:499)
[Code] ....
View Replies
View Related
Nov 2, 2014
I have to use a long primitive type for the input of a credit card number and ID the credit card by using the first number of the input; however, the only way I know for that is to use charAt, which is used for a String. Is there a way to convert long to String, or am I missing a better solution? (There's no code because I'm still doing the pseudocode).
View Replies
View Related
Apr 10, 2015
I am pretty new to Java and am just learning about two dimensional arrays. I think that I understand the concept, but I seem to be having trouble adding stuff to my array. I wanted to make an array to hold both strings and integers, but wasn't sure if I could put integers in a string array. So I thought that I would be able to convert my integers to string and then add them. This however causes an error. This is my code(yes its probably not the best):
static String [][] students = new String [14][4];
static int number = 0;
String fName, lName, fullName;
int test1, test2, test3, test4;
String a, b, c, d;
[Code] .....
View Replies
View Related
Apr 8, 2014
In my java file I made Strings of date and time, but my MYSQL database needs Date and Time of course. I've tried to convert them, but I keep getting this exception: org.apache.jasper.JasperException: java.lang.NumberFormatException: For input string: "2013-02-20"
public class Vogel {
static final String url = "jdbc:mysql://localhost:3306/turving";
public static void Insert(String datum, String tijd, String plaats, String spotternaam, String vogelsoort) {
try {
SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-DD");
SimpleDateFormat formatt = new SimpleDateFormat("HH:MM:SS");
java.util.Date parsed = format.parse(datum);
java.util.Date parsedd = formatt.parse(tijd);
java.sql.Date sql = new java.sql.Date(parsed.getTime());
java.sql.Time sqll = new java.sql.Time(parsed.getTime());
[code]....
View Replies
View Related
Sep 28, 2014
Run the code along with the attached csv file. The GUI contains a short explanation of what I am looking for. I have tried converting the integer array to a string array but the output is not the same as the command line. I receive errors when I compile.
View Replies
View Related
Oct 1, 2014
I would like convert string to date (dd/MM/YYYY) - this is useful for compare dates in TableColumns.
so i use DateTimeStringConverter (the string "01/11/2014" is a value of DatePicker)
DateTimeStringConverter format = new DateTimeStringConverter(Locale.FRANCE,
"dd/MM/YYYY");
Date d1 = format.fromString("01/11/2014");
d1.toString()
I don't obtain the right date but this date = "Mon Dec 30 00:00:00 CET 2013" !!!
View Replies
View Related
Jan 26, 2014
I am getting Run time Exception while converting String object to Date Object ....
java.text.SimpleDateFormat@b9b195a0
java.text.ParseException: Unparseable date: "14-07-2012"
public static void main(String[] args) {
String time = "14-07-2012";
[Code] .....
View Replies
View Related
Jul 8, 2014
I am trying to make a program that calculates the change due in dollars and cents. The user inputs both the amount due and the amount tendered. My program only works with whole numbers?
View Replies
View Related
Feb 23, 2014
I need to turn some strings from:
'1-the-high-street'
to:
'1 The High Street'
But...
if string is
'the-dog-and-duck'
I want to achieve
'The Dog and Duck'
...whereby the 'and' is NOT converted to title case
My code as follows:
String propertyPageTitle = "1-the-high-street";
propertyPageTitle = propertyPageTitle.replace("-", " ");
propertyPageTitle = WordUtils.capitalizeFully(propertyPageTitle);
System.out.println(propertyPageTitle);
prints: '1 The High Street'
How could I handle the second scenario so it does not convert 'and' to titlecase?
View Replies
View Related
Apr 20, 2014
Currently, my program converts Long values to String. And when I test it out, it do print out the correct output. However, when the converted String value is passed over to be written in a text file, it seems that BufferedWriter isn't printing out the outcome that it's supposed to be.
saltVs = Long.toString(saltV);
System.out.print(saltVs); //will print out 79723172
Now the problem is here...It only prints out the last digit of the String value (instead of 79723172).
Here is my FileWriter/BufferedWriter part.
Why is that when I run my program using command prompt, it prints out the output that I wanted, but however when it comes to writing to the file, it doesn't come out right.
View Replies
View Related
Mar 7, 2014
I'am trying to converting string data into xml data using xml beans and StringEscapeUtils.This is work fine but in one case it if the data contains special characters.
Code snippet
--------------------
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
public class ParseXMLData {
public static XmlObject parseXML(String stringXML) {
XmlObject xmlObject = null;
[Code] ....
success case
---------------------------
i/p------<aaa><bbb>This converts string to xml</bbb></aaa>
o/p---<aaa><bbb>This converts string to xml</bbb></aaa>
Failer case
-----------------
i/p----<aaa><bbb>This fails if it contains < symbols</bbb></aaa>
expected o/p----<aaa><bbb>This fails if it contains < symbols</bbb></aaa>
Observed that it converts < to '<' but as per xml rules, if data contains '< ' it fails. I want to convert staring and end tags to xml format and if data in b/w middle of starting and ending tags do'n need to convert it. How to do it.
View Replies
View Related
Nov 3, 2014
I'm very new to Java and looking to expand knowledge base from Excel VBA..I'm looking to add or analyse data from a csv file in a time order fashion. i.e i want to keep/ add a cumulative total. (once i've achieved that i then want to look at a java equivalent of sumif...
View Replies
View Related
Dec 22, 2014
I'm converting from ASP to Java for a legacy site. I believe the first part of the if statement is right but I'm having an issue within the else staetment. I'm getting date is undefined, I've tried using getYear and getDate but have not been successful.
ASP:
If Request("action") = "results" Then
aMonth = Request("selMonth")
aDate = cint(Request("selDate"))
aYear = cint(Request("selYear"))
Else
aMonth = UCase(MonthName(Month(Date()),true))
aDate = Day(DateAdd("d",-1,date()))
aYear = Year(Date())
End If
[code]....
View Replies
View Related
May 16, 2014
I try to use below codes to convert Hex String "1111737999630745" to ASCII.
When I read the result in Notepad++ with HEX-Editor plugin, I find attached image which is different from above String, there is one additional c2 and I am quite confused.
Java Code:
public static void main(String[] args) throws IOException {
Test strToHex = new Test();
File file = new File("D:/filename");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
[Code] .....
View Replies
View Related
Mar 3, 2015
I was reading about the char data type in Java. I know that an unsigned 16 bit integer is used to represent a char. So we can write the assignments below:
char a = 65; // a will get the value 'A'
But the limit of the char value is 65535.
So I've tried out a few things with char and trying to understand them but I'm not sure how they work.
char a =(char) 70000;
char b = (char) -1;
In the first case I thought that 70000 % 65535 would happen internally and the unicode character present at that location would get stored in 'a' but if I do that I get the answer of 70000 % 65535 as 4465. But when I display 'a' it shows me the output as '?'. Clearly '?' isn't at 4465.
In the second case I have no clue what's happening but somehow when I display 'b' it shows me '?' again.
View Replies
View Related
Apr 8, 2015
Here is my code, basically I'll tell you what my program is suppose to do, I want to be able to leave the text box's empty if I like I want to skip 1 or more or all if I like skipping just basically means 0 but getting this error, it's forcing me to type in them all.
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unk nown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at StockControl.addToStock(StockControl.java:86)
[Code] .....
My code :
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
[Code] .....
View Replies
View Related
Jan 28, 2014
This is the code to convert string into unicode but I get an error as "illegal start of an expresssion"while running the code..
import java.util.Scanner;
import java.lang.String;
import java.lang.Character;
public class A
{
public static void main(String args[])
{
[Code]...
View Replies
View Related