|
Please format your code properly when posting answers.
|
|
|
|
|
what to format ? an how? give example with my post
|
|
|
|
|
Like this
double mathsub(double x,double z){
return x-z;
}
surrounding your code with <pre> tags, by using the code button on the edit window.
|
|
|
|
|
Today I was testing the ArrayList of the Java because I've just started learning the Java after C#. Concept that underlies is the generic, similar to the C#, API is somewhat similar.
But there was a problem, that I faced, I started with the String class objects in the ArrayList and I found surprising that the list allows all type of objects in it. For example the following code from my HelloWorld application.
List list = new ArrayList<String>();
list.add(new JavaHelloCollections());
list.add("String data");
list.add(4345345345.9898996756);
for(Object o : list) {
System.out.println(o);
}
Well, firstly it compiles all fair and doesn't only compile it also prints the result; a little doubty result for the float (or double) type data.
javahellocollections.JavaHelloCollections@529e0c79
String data
4.3453453459899E9
I am using NetBeans and I am really confused of how can this happen. Does JVM perform some .toString() checks there or what? Where actually does this type check or type conversion happens?
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
What you have actually created is a simple List that will accept any type. Try
ArrayList<String> list = new ArrayList<String>();
|
|
|
|
|
+1 Thanks Richard.
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
Afzaal Ahmad Zeeshan wrote: Where actually does this type check or type conversion happens?
Although the previous comment pointed out your code problem it didn't answer this question.
The answer is that this occurs in the compiler and only in the compiler. Quite possible and easy to inject something wrong into it if one is not careful or one just wants to circumvent it.
One will often run into this with situations like the following.
void method(List<String> list) { ... }
void method(List<Integer> list) { ... }
Although these look like two different overloads after the compiler gets done with them the signature will resolve to exactly the same overload (the compiler will throw an error about that.)
|
|
|
|
|
Yeah that makes sense. Thanks for your clarification. Now it does make sense for why did I get problems. But, isn't the List generic? Why would it throw error for attaching a type to the List?
Secondly, I am still just learning and not building any application so "careless" won't apply, I was trying to make the application throw error but it accepted it that cause me to ask the question here. I will continue trying more such "careless" attempts in order to learn more.
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
Afzaal Ahmad Zeeshan wrote: Why would it throw error for attaching a type to the List?
Not sure what you mean by "throw error" but the compiler validates types for generics and will produce a compiler error when it finds those.
Other than that it is possible to put the wrong type in a list because the compiled version doesn't have a type. An error will occur later because as the compiler produces code it puts in checks which impact what happens when you get something out of the list. And one of those checks will fail. Seeing such a error isn't a good thing since it can be very confusing.
|
|
|
|
|
need a simple java code for transport ticketing
|
|
|
|
|
|
That depends on the level of the project. You can create a single page application with a single
System.out.println("Ticket booked!");
or you can create an entire application for keeping the ticket records, user records and other transport means information and so on. But first, come up with a basic algorithm and the code that you've tried; atleast the algorithm. Asking a one-liner question doesn't help you or us.
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
Greetings, everything in this code works except when the i input the weight and height the result is not shown in the textbox (v_text).. please help me
package bmicalculator;
import static com.sun.corba.se.impl.orbutil.CorbaResourceUtil.getText;
import java.lang.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.*;
import java.io.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JPanel;
import javax.swing.JFileChooser;
import java.awt.Container;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
public class BMICalculator extends JFrame implements ActionListener, PropertyChangeListener
{
JPanel p_format;
JPanel p_text;
JLabel f_name;
JLabel height;
JLabel weight;
JTextField n_text;
JFormattedTextField h_text;
JFormattedTextField w_text;
JFormattedTextField v_text;
JTextField r_text;
JTextArea a_text;
JButton v_bmi;
JButton result;
JButton r_done;
JButton r_save;
JButton r_reset;
JButton f_open;
JFileChooser chooser = new JFileChooser();
String Height, Weight;
double BMI;
static String output = "Results";
static int jopIcon = JOptionPane.QUESTION_MESSAGE;
boolean bFlag = true;
Font font;
static int f_size = 16;
static int style = Font.PLAIN;
static String font_name = " Harrington ";
double number1;
double number2;
public BMICalculator (String s)
{
super(s);
font = new Font(font_name, style, f_size);
getContentPane().setLayout(new GridLayout(1,2));
p_format = new JPanel();
p_text = new JPanel();
p_text.setLayout(null);
f_name = new JLabel(" Name: ");
f_name.setSize(120,60);
f_name.setLocation(30, 30);
f_name.setForeground(Color.DARK_GRAY);
n_text = new JTextField(40);
n_text.setSize(120,60);
n_text.setLocation(70,30);
height = new JLabel(" Height(meter) : ");
height.setSize(90, 60);
height.setLocation(30,60 );
height.setForeground(Color.RED);
h_text = new JFormattedTextField();
h_text.setColumns(10);
h_text.setLocation(70, 60);
h_text.setValue(new Double(number1));
h_text.addPropertyChangeListener("value", this);
weight = new JLabel(" Weight(kilogram): ");
weight.setSize(90, 60);
weight.setLocation(30, 90);
weight.setForeground(Color.RED);
w_text = new JFormattedTextField( );
w_text.setColumns(10);
w_text.setLocation(70, 90);
w_text.setValue(new Double(number2));
w_text.addPropertyChangeListener("value", this);
v_bmi = new JButton("Your BMI: ");
v_bmi.setSize(90, 60);
v_bmi.setLocation(30,190);
v_bmi.setForeground(Color.BLUE);
v_bmi.addPropertyChangeListener("value", this);
v_bmi.addActionListener(this);
v_text = new JFormattedTextField( );
v_text.setColumns(10);
v_text.setLocation(70, 190);
v_text.setValue(BMI);
v_text.addPropertyChangeListener("value", this);
result = new JButton("Your level: ");
result.setSize(60, 60);
result.setLocation(30,250);
result.setForeground(Color.BLUE);
result.addActionListener(this);
r_text = new JTextField(45);
r_text.setSize(90, 60);
r_text.setLocation(70, 250);
r_text.addActionListener(this);
a_text = new JTextArea(30,45);
a_text.setSize(200,200);
a_text.setLocation(30, 300);
r_done = new JButton(" Done! ");
r_done.setSize(120,30);
r_done.setLocation(30, 350);
r_done.setForeground(Color.BLACK);
r_done.addActionListener(this);
r_save = new JButton(" Save ");
r_save.setSize(120,30);
r_save.setLocation(60, 350);
r_save.setForeground(Color.MAGENTA);
r_save.addActionListener(this);
r_reset = new JButton(" Reset ");
r_reset.setSize(120,30);
r_reset.setLocation(90, 350);
r_reset.setForeground(Color.RED);
r_reset.addActionListener(this);
f_open = new JButton(" Open ");
f_open.setSize(120,30);
f_open.setLocation(120, 350);
f_open.setForeground(Color.BLUE);
f_open.addActionListener(this);
JScrollPane textScrollPane=new JScrollPane(a_text);
textScrollPane.setSize(300,300);
textScrollPane.setLocation(30,300);
p_format.setBackground(Color.PINK);
p_format.add(f_name);
p_format.add(n_text);
p_format.add(height);
p_format.add(h_text);
p_format.add(weight);
p_format.add(w_text);
p_format.add(v_bmi);
p_format.add(v_text);
p_format.add(result);
p_format.add(r_text);
p_format.add(a_text);
p_format.add(r_done);
p_format.add(r_save);
p_format.add(r_reset);
p_format.add(f_open);
p_format.add(textScrollPane);
getContentPane().add(p_format);
}
public static void main(String args[])
{
BMICalculator k=new BMICalculator("SHARIMAH SAKKA Project");
k.setSize(550, 700);
k.show();
k.setVisible(true);
k.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void BMIResult()
{
Height = height.getText();
Weight = weight.getText();
number1 = Double.parseDouble(Height);
number2 = Double.parseDouble(Weight);
BMICalculate(number1, number2);
}
public void BMICalculate(double number1, double number2)
{
try
{
number1 = (double) ((Number)h_text.getValue());
number2 = (double) ((Number)w_text.getValue());
BMI = (number2 / ((number1) * (number1)));
}
catch (NumberFormatException nfe)
{
output += "\n\n Whoa! Input error: must enter valid integers";
jopIcon = JOptionPane.ERROR_MESSAGE;
}
}
void saveToFile()
{
if (chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION)
{
File saveFile = chooser.getSelectedFile();
try
{
PrintWriter out = new PrintWriter (new FileWriter(saveFile));
out.print (a_text.getText());
out.close();
}
catch (IOException e)
{
JOptionPane.showMessageDialog(null, "Could not save the file " + e.getMessage());
}
}
}
void loadFromFile()
{
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
{
File loadFile = chooser.getSelectedFile();
try
{
BufferedReader in = new BufferedReader (new FileReader(loadFile));
a_text.setText("");
String nextLine = in.readLine();
while (nextLine != null)
{
a_text.append(nextLine + "\n");
nextLine = in.readLine();
}
in.close();
}
catch (IOException ie)
{
JOptionPane.showMessageDialog(null, "Could not load the file " + ie.getMessage());
}
}
}
@Override
public void actionPerformed(ActionEvent x)
{
if(x.getSource()==v_bmi)
{
v_text.setValue(new Double(BMI));
}
if(x.getSource()==result)
{
r_text.getText();
if (BMI >= 29)
{
r_text.setText(" High Risk zone(UnHealthy).Please start losing weight!It's a MUST!!! ");
}
else if (BMI <= 25 || BMI < 28.9)
{
r_text.setText(" Moderate Risk zone.Please start going on diet and lose some weight! ");
}
else if (BMI <= 18.5 || BMI < 24.9)
{
r_text.setText(" Low Risk zone(Healthy).Hopefully you can maintain this way! ");
}
else if (BMI < 18.4)
{
r_text.setText(" Need to start eating more. Too skinny and unhealthy for your body. ");
}
}
if(x.getSource()==r_done)
{
String s= "Name: " + n_text.getText()+ "\n"+"Height: " + h_text.getText()+"\n"+"Weight: " + w_text.getText()+"\n"+"BMI: " + v_text.getText()+"\n"+"Result: " + r_text.getText();
a_text.setText(s);
}
if(x.getSource()==r_reset)
{
String s=n_text.getText() +h_text.getText()+w_text.getText()+ v_text.getText()+r_text.getText();
n_text.setText(null);
h_text.setText(null);
w_text.setText(null);
v_text.setText(null);
r_text.setText(null);
a_text.setText(null);
}
if(x.getSource()==r_save)
{
saveToFile();
}
if(x.getSource()== f_open)
{
loadFromFile();
}
}
@Override
public void propertyChange(PropertyChangeEvent x)
{
Object source = x.getSource();
if(source==v_bmi)
{
String b;
b = Double.valueOf(BMI).toString();
v_text.setValue(new Double(b));
}
}
}
|
|
|
|
|
Too much code, so I'm not going to look through all of it.
This caught my attention as "obviously wrong" though: else if (BMI <= 25 || BMI < 28.9) that's really just BMI < 28.9 , the other part is redundant. It's probably meant to be >= 25 ?
But then the logic also doesn't quite work, because clearly there is a gap between 25 and 24.9, a rather large gap at that - if I counted them correctly, there are 28147497671065 doubles that have a value strictly between 24.9 and 25, which implies that almost all of them won't be generated by sensible inputs, but see for example 90kg / 1.90m2.
Also, BMICalculate does some unexpected things, taking nice arguments and then ignoring them and pulling its values from some control.
|
|
|
|
|
which project i will do in third year. Now i am in second year Bs.C IT.......
Please suggest.........
|
|
|
|
|
Anything you can think of that is a) within your skill level, and b) a subject that you understand. If you are really stuck then ask your professor to allocate you something.
|
|
|
|
|
Write a compiler in java.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Hello...
In my project work I was supposed to automatically login and create a ticket to a support system. here are the steps I planned to follow
1. Get HTML content of login page by using Get method with HttpsURLConnection.
2. Parse HTML content using jSoup library and extract form input elements.
3. Get the names of the attributes of the form and prepare a parameter list with custom data (like user name, Password) and remaining application generated parameters.
4. Now Send a post request to Login page url with the parameter list ( which also will have other paremeters which are not supposed to be changed).
5. After post request successful I receive the HTML content of page which occurs after login via BufferedReader with InputStream Object of HttpsURLConnection.
6. Now I will send a "GET" request to the page which has a Ticket opening form and receive HTML content of the page.
7. Parse HTML content using jSoup library and extract form input elements.
8. Get the names of the attributes and preapare a parameter list (which will have the details of ticket which needs to be updated and submitted to webservice to complete ticket creation) this should be a multipart form data ( I set headers accordingly).
9. Then post it to the ticket creation web service.
10. Process will complete If a ticket gets created.
Currently
I am able to successfully login to the website, get the parameters and set all the attributes and formdata. but I'm stuck with few errors. Currently I'm getting InvalidStateException : Already Connected error.
I am running the code on IntelliJ IDEA 14 Community Edition. Can anyone please help me with this error. and also it would be great if you can correct me if I'm going in wrong direction to get what I'm trying to do here.
Lines - 448 through 453 of the file.
Here is the code.
CompleteProgram.java
Thanks,
Sudhir.
|
|
|
|
|
|
|
please how can I develope a web base chat application am a student of computer science that's my project topic design an implementation of webbase chat application pls I need a guide to achieve these....thank you
|
|
|
|
|
What specific help do you need? Have you researched what tutorials there are on this subject?
|
|
|
|
|
the help I need is a guide on how to achieve these project. I hv gone on research on it but what I see are not straight to my understanding.......please I need your assistance here please
|
|
|
|
|
|
OK sir.....I was able to design a program that can save,delet'search,add, using the netbeans java jframe.....I want to create a space to accept an individual image or passport how can I achieve that
|
|
|
|