I think he's trying to get a servlet running, not sure though
Or a simple GUI.
damu, you need to better describe your question and what you are trying to accomplish. An example would help
I think he's trying to get a servlet running, not sure though
Or a simple GUI.
damu, you need to better describe your question and what you are trying to accomplish. An example would help
System.currentTimeMillis();
It returns the current time in milliseconds. So you can call this method before the java method you want to count and then call it again and get the difference
Good call.
I don't know if this solves the problem. We will need thijo to reply with an update of his problem
Because you haven't converted correctly the code.
You put the line in the ArrayEntry[0] and the rest of its values are null.
You check the ArrayEntry[0] if it is "." which is not because it contains the entire line.
You don't need the array, the line's value is stored in the parameter. That parameter alone you need to work with.
And you stilled haven't check my suggestions about the String class. Why don't you take a look at it
This is how you declare methods:
public static int getRank(String line) {
int rank;
......
return rank;
}
And call it:
String [] name = new String[i]; //remember the "i" ?
int [] rank= new rank[i]; //remember the "i" ?
....
for(int counter=0;counter<i;counter++) {
System.out.println(LineArray[counter]);
rank[i] = getRank(LineArray[counter]);
...
}
The LineArray[counter] is a String that has the current line from the for-loop. It is that line that you enter as a parameter to the method in order to get the rank. Inside the method you will deal only with the argument, because it has the value that you pass it when you call the method:
The argument "line" has the value of the argument when you call it. When you call it like this: getRank(LineArray[counter]);
"line" has the value of: "LineArray[counter]" . So inside the method you will deal only with local "line" variable of the method.
public static int getRank(String line) {
int rank;
......
return rank;
}
I have already told you which methods to use from the String class and how to use them
I haven't written this kind of java code before but I am familiar with the redirect command: '>'
Maybe this is your problem:
Process p = Runtime.getRuntime().exec("java Add2number>C:\\Program Files\\Java\\jdk1.6.0_03\\bin \\out.txt");
Have you noticed a space between \\bin and \\out
...\\bin \\out
Can you try it with this:
Process p = Runtime.getRuntime().exec("java Add2number>C:\\Program Files\\Java\\jdk1.6.0_03\\bin\\out.txt");
I haven't used FileChooser much, but have you tried these methods:
setApproveButtonText(String approveButtonText)
Sets the text used in the ApproveButton in the FileChooserUI.
showDialog(Component parent, String approveButtonText)
Pops a custom file chooser dialog with a custom approve button.
It is : main(String [] args)
Also I think that you have the formula for surface_area wrong
Take a look at the Java I/O package. I am sure it's a monster containing hundreads of classes but it present as many ways as there can be to write to a file in Java.
OR
You can use a PrintWriter over an OutputStream like the FileOutputStream. The FileOutputStream can be opened on a File object, or a string that specifies the filepath.
OR
For a quick example check here
Together almost the same post
You can use BufferedWriter to write to a file:
FileWriter fWriter = null;
BufferedWriter writer = null;
try {
fWriter = new FileWriter("fileName");
writer = new BufferedWriter(fWriter);
//WRITES 1 LINE TO FILE AND CHANGES LINE
writer.write("This line is written");
writer.newLine();
writer.close();
} catch (Exception e) {
}
Also check these APIs
BufferedWriter
The BufferedWriter's super class: Writer
FileWriter
First of all loop through the existing array like you are already doing and try to "extract" the 3 values from each element. Write separate methods for that.After you are successful you can create 3 arrays and put these inside with each loop:
String [] name = new String[i]; //remember the "i" ?
....
for(int counter=0;counter<i;counter++) {
System.out.println(LineArray[counter]);
name[i] = getName(LineArray[counter]);
...
}
The getName(LineArray[counter]);
will take as argument the element of the array and return the name. You will have other methods for the "sport" and the "rank".
The best way is to create an object "Athlete" with attributes name, rank, sport and have a method that takes as argument the element of the array and return that object. Then you will put that in an array of "Athlete" objects. If you are not familiar with that leave for now
Why do you say that this didn't work:
while (!(lineRead.equals(null)) && (i<LineArray.length))
//lineRead !=null did not
If lineRead is null you will get an Exception.
Why this thing doesn't work and what problems do you have?
while ( (lineRead!=null) && (i<LineArray.length) ) {
}
Also the same applies to this:
finally {
....
if ( read!=null) read.close();
....
}
By the way, this was smart:
while ( (lineRead!=null) && (i<LineArray.length) )
Now you will never exceed the length of the array. But I would like to suggest that at the for-loop to use the i as max index:
Instead of this: for(int counter=0;counter<LineArray.length;counter++)
Use this:
for(int counter=0; counter<i; counter++)
Because if the file has less lines let's say 10, only the first 10 elements of the array will have values. The rest will be empty (null).
So you will need to loop only till you reach the number of lines that were inserted in the array, which is the "i" value.
Now that you have the array ready, use the suggestions in my previous post on how to use the "subString" and "indexOf" methods on order to get what you want
Let me explain this code a little and then I will tell you your mistake:
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("fileName"));
String line = reader.readLine();
while (line!=null) {
System.out.println("The Line is: "+line);
// do calculations with line
line = reader.readLine();
}
} catch (Exception e) {
} finally {
}
Read the first line: String line = reader.readLine();
Checks if the line is null or not. If it is not continue: while (line!=null) {
Then inside the loop you do the calculations.
AT THE END OF THE LOOP you read the next line, and check if it is null or not: while (line!=null) {
. If it is not null continue.
This code must not be altered. The LAST command of the while loop must be the readLine. Because after that you go to the top, check the line and continue with the loop only if it is not null. If you read the line in the while you must not do any more calculations, because you haven't checked the new line. You need to go to the top. That's why it is the last command. And it must always executed.
while (line!=null) {
//ALL CALCULATIONS ARE DONE HERE WITH THE LINE
//YOU GET THE NEXT LINE ONLY HERE AND GO TO THE TOP TO CHECK IT
line = reader.readLine();
}
With this code:
while (line != null ){
//System.out.println(test);
if (test.equals("")){
test=read.readLine();
}
else{
line[i]=test;
i++;
System.out.println(line[i]);
}
}
…That is not the code I gave you for reading a file.
Well, since you have copied that code directly to main, you need to add this as well:
finally {
try {
if (reader!=null) reader.close();
} catch(Exception e) {
}
}
I was expecting for you to put that code in a method that throws an IOException. But putting it in main might be enough for you now.
You got that error because the reader.close();
throws an Exception. But it is best to put it in the finally block in order to make sure that it is always executed.
I just gave him the quickest way to get the output he was looking for - which seems to be what he wanted, judging by his reply.
I will have to agree with verruckt24.
When you said to change the double to int (or long, I don't remember) your explanation was wrong. So the OP assumed that his code was wrong, which wasn't.
We are not here to give quick solutions but to help others understand and learn
What is the point of double threading? Wasn't one thread enough for you?
I have never used arrays in Java, although I used them in Python but whatever u wrote up there i understand some of it because I have reading lots of blogs and online articles about java arrays.
and about the line problem my file is formatted in this way
1. JOHN CARTER in Sprint Race.
Then forget about the split method and check the String API for the methods:
indexOf
subString
Find the index Of the first '.' dot and take the number using the subString
Then find the index Of 'in' and use the subString to get the name and the sport
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("fileName"));
String line = reader.readLine();
while (line!=null) {
System.out.println("The Line is: "+line);
// do calculations with line
line = reader.readLine();
}
} catch (Exception e) {
} finally {
if (reader!=null) reader.close();
}
If you are unfamiliar with using arrays, I would suggest to concentrate on that first, before going to write the code for reading from the file. Once you have understood the arrays very well, you may proceed.
Also it is best to have your file formatted like this:
1;JOHN CARTER;Sprint Race
19;JOHN CARTER;Sprint Race
Because of the split method. Try to run this example:
String s = "1;JOHN CARTER;Sprint Race";
String [] array = s.split(";");
for (int i=0;i<array.length;i++) {
System.out.println(array[i]);
}
There is a link at that page:
http://db.apache.org/derby/docs/dev/getstart/
I don't use the databases that NetBeans has. I use mySQL server, and all I need to do is create a database there using sqlplus.
How good you are with sql? Because NetBeans offers ready made easy solutions (auto generated code).
I would suggest to follow the link provided because I don't know anything else.
Also this is a link I tell a lot of people to follow. It is a JSP tutorial, but it can help you create your own database independent of IDE
http://www.daniweb.com/forums/thread141776.html
You will need to have the mySQL server as described
hi any one online
Hi, anybody home chandanapiit?
Are you having problems displaying this?
String labelText =
"<html><br>Total value of Supply Inventory: " + (supplies.getTotalValue(inventory));
JLabel TotalValue = new JLabel(labelText,JLabel.CENTER);
container.add(TotalValue);
Then the answer is simple.
First, your class extends JFrame, but inside you create a new JFrame and put the inventory elements in it, and you display that.
But the above code:
JLabel TotalValue = new JLabel(labelText,JLabel.CENTER);
container.add(TotalValue);
You add the "TotalValue" label to the "container". That "container" is only initialized and not used anywhere. You display the local JFrame:
JFrame frame = new JFrame();
frame.getContentPane().add(new JScrollPane(textArea)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
But the total value you put it in the container taken from the class you extend. And you don't have code that displays it.
You don't need to create a new JFrame because your class is already a JFrame.
When you do this: frame.getContentPane()
is like doing this: Container container = getContentPane();
because you call the getContentPane of "this" class, which is a JFrame.
I am not familiar with using contentPane. I use another approach when it comes to GUI, but try this:
//JFrame frame = new JFrame();
container.add(new JScrollPane(textArea));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
....
....
container.add(TotalValue);
pack();
setLocationRelativeTo(null);
setVisible(true);
instead of this:
JFrame frame = new JFrame();
frame.getContentPane().add(new JScrollPane(textArea)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Um, no, the problem here was not how the array was initialized with default values.
You are right. I have missed the part of the code where the poster asks the user for input.
Usually I initialize the lowest, highest values with the first value of the array, like the poster. But in this case the poster did so before putting any values, which was the problem as you mentioned
Well I am not afraid to say that I understand what you mean or what
this thing is:
"Start Java DB manually"
You initialize the array, but you don't put values in it. So by default the values that it has are zero because the int variables are initialized to zero.
This might not seem serious but if the array was String it would null. So next time, you don't just create the array, you put values in it as well
Java is case sensitive
Apparently you are tying to use methods that don't exist.
You don't just call methods just because you like it. You need yo declare them first
You wrote the code. Does it compile? If it does, is it so difficult to run it?
Have you created the database?
@javaaddict
in your code:String s="abcd"; for (int i=0;i<s.length;i++) { System.out.println( s.charAt(i) ); }
I get an error stating that the < operator cannot be applied, I tried making it a string and an int, but it didn't work either way.
Try this:
String s="abcd";
for (int i=0;i<s.length();i++) {
System.out.println( s.charAt(i) );
}
I keep forgetting that for Strings this is the right one:
s.length()
Also what you wrote will work but only for 5-digit numbers. Try modifying this example to accomplish what you want
String s="abcd";
for (int i=0;i<s.length();i++) {
System.out.print( s.charAt(i) );
}
System.out.println();
Firstly gabec94 I would like to note a few things that I have observed while going through this thread. You are not taking any heed of the advice and help given to you and just want to stick to the method of doing something in the langauge that you have worked with in the past.
You are are right.
I have already given him an example on how to convert the String to int, so he can use his beloved modulus but he hasn't made any effort to use it.
Also the same example you gave him with the charAt(). But just because he hasn't used the language before, shouldn't stop him from thinking the solution you gave him. The method and the example was already given.
Did you run the examples and understood what they do?
You cannot use modulus to a String. You need to convert it to number:
String s="12345";
int i = Integer.parseInt(s);
This was the worst attempt at disguising homework I've ever seen. Lol.
I always like to give the benefit of the doubt. The OP might not go to that school, and accidentally found about that site, searched it a little and found that assignment and decided to post it.
Unlikely but possible
First of all.
Whenever you get an error, always check the API of the class you are trying to use. I have never used the Scanner class but obviously you are trying to use a method that doesn't exist. So check the API for Scanner class so you can see which methods it has.
Second,
The String API tells you what arguments they take what they return, and what they do.
Also run this example:
String s="abcd";
for (int i=0;i<s.length;i++) {
System.out.println( s.charAt(i) );
}
What kind of error, and didn't you notice the methods:
subString, indexOf, or charAt.
at the String API?
Well assuming that this is not homework, I think that it is a good idea.
I know that most of us, including stephen84s, can do this in 30 seconds, but others might learn from it and newbies may test their skill.
Of course the OP could also add the link from which he took the code
who said i wud bs last assignment just want it out the way enit
What you said makes no sense. Please use proper English
The method compareGuess should be inside a for-loop not a for-loop inside the method. The way you have you iterate, but the value "userRandom" doesn't change, so the same if is executed all the time.
You need to have the method inside the loop and ask the user each time to enter a new number. Then, if it is not found print the appropriate message and continue with the looping. If found use the "break;" command to exit the loop and set a boolean flag to true.
If the number is not guessed correctly the loop will finish and outside the loop you will check the boolean flag if it was true or false? Don't forget to instantiate the boolean variable correctly
The line read is in the strLine
variable. Since you have already defined your array, you can simply put that value inside the array. Of course within each while-loop run you will need to increase the index of the array so you will put the next line to the next element of the array.
Create a public method that takes as argument an int and returns the element of the array at that place, and call it from main. You need to do that because you have defined the array as private which I think is a good idea.
That is correct assuming that the number of the lists will be fixed as well as their length.
A more dynamic approach will be a little more complicated:
Calculate first the number of possible combinations by getting their length and then have 1 for loop:
for (int i=0;i<12 /*number of combinations*/ ;i++) {
}
Inside you will have code that generates ONE possible combination. Maybe you could use a while loop in order to get each element from each list by increasing an index (row, or col) and by checking if they have reached the length of each sublist
Also you don't need this in your class:
private Circle circles;
In your class you define the ArrayList of circles, so you don't need that circle object. Everything will be put in that list and you will have methods that add and read or print from that list
You can have a method that adds a Circle object to that list. Maybe the method will have a Circle as a parameter and you can add that to the list.
Also pay attention on how you name your variables: private Circle circles;
You define a ONE Circle object and the variable name is plural: cirlces
cirlces would be a good name for your ArrayList if you haven't named it storage, which is also a good name
And to close once you have finished with this class you will instantiate it once in your main method and start calling the add method as many times as you like in order to put circles in it
Sorry hinasaeedali,
I can't help you. Some time you are asking you are facing problem while making shopingCart and now you are asking how to get parameter. You didn't clearly mention your problem.
As i understood you want to know how to get request parameter when you click on submit button.
then use request.getParameter("Pass here parameter name")
As you mentioned you are facing problem with jsp not in another language. I think you just started jsp.I would like to request to others if somebuddy able to understand you.
Regards
Nishant
Given the fact that this is a JSP (not a javascript) forum then, your solution was correct as far as getting the parameter, when you submit a form by clicking a submit button
But since this is very trivial, I would suggest for you hinasaeedali to find a good book about JSP and Servlets, before continuing. Also look at the first post of this forum that explains database connectivity with JSP:
http://www.daniweb.com/forums/thread141776.html
This link is at the top of the JSP forum. Instead of insert it does a select, but it show how to pass data from the jsp to the servlet, run the query and then return back to the jsp.
So all you have to do is change the query and the database configuration to match yours:
If the existing code in this thread doesn't cover you, start your own thread.
Also it is best to show what you have done so far if you want to get any help
Use code tags, say at which line you get the error, and
Obviously from the compiler message:
cannot find symbol method calculateCelsius()
You have not declared such method.
The compiler tells you exactly what is your problem and at which line. Is it so difficult to read it?
First of all in java is very simple.
Classes to use: Date, SimpleDateFormat, Thread.
And no we will not write the code. As instructed to other forum you posted (http://www.daniweb.com/forums/thread176644.html), first try it yourself and we will see what we can do.
Also if you want this done on a web page, try a different forum
Post your driver code and what errors you get
sry i didn't see dat it was 11 months older now
sorry ..........
Also the guy was trying to cheat at an online exam. If you look at a recent post he says: "I want the solution within 30 minutes or I will fail".
Meaning that despite your good heart, you shouldn't post the solution, because this is against the forum rules. We do not give ready solutions just because someone has asked for it.
We only help those that show an effort; we don't hand in their homework.
Keep trying to help people and don't let this thread get you down
Not to mention that you failed to explain what is your problem. You just posted some piece of code without saying anything.
Also you were the one that hijacked a 11 month old thread, whithout explaining anything and confusing everybody