i'm not familiar using that command...Could you give me some tricks of using it
In your code you have been using NumberFormat. It's the same thing. Have you looked at the link? Just read the API and use the methods. Experiment a little.
i'm not familiar using that command...Could you give me some tricks of using it
In your code you have been using NumberFormat. It's the same thing. Have you looked at the link? Just read the API and use the methods. Experiment a little.
Well I don't believe there has something to do with your machine. The problem was the code (and I didn't see it. sorry.)
I believe the problem was the use of both nextLine and nextInt. After you called the nextInt, the loop repeated itself and tried to call the nextLine.
But for some reason that I can't explain it skipped the line you entered or it read on its own an empty line:
nextLine
By adding in both places the nextLine the problem was fixed.
Actually the whole thing was my mistake.
I am sorry.
I was using a wrong jdk and version of your code for testing and I am getting the same errors you get.
I tried some things to fix it with no luck.
Finally I came with this idea:
Replace the nextInt with this:
n=Integer.parseInt(in.nextLine());
Enter you Name:
You have:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.StringBuffer.charAt(StringBuffer.java:162)
at beffer.Main.main(Main.java:33)
Java Result: 1
Of course the program exited because you didn't enter anything the second time:
Enter you Name:
aa bb cc
You have: aa bb cc
Capitalize name: Aa Bb Cc
Middle name is: vBb
First name: Aa
Last name: Cc
Number of Words :3
Enter 0 or 1 to continue:
0
Enter you Name:
dd ee ff
You have: dd ee ff
Capitalize name: Dd Ee Ff
Middle name is: vEe
First name: Dd
Last name: Ff
Number of Words :5
Enter 0 or 1 to continue:
1
Enter you Name:
aa bb
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.StringBuffer.substring(StringBuffer.java:801)
at stam.Main.main(Main.java:47)
You have: aa bb
The third time the program exited with exception because I entered 2 words instead if 3. In your case you entered nothing. You just pressed enter
In my PC the code run OK. Try run it again and post what messages you get
I believe it's because you didn't enter a name the second. Your code is very poor for handling error inputs. Try this:
do{
System.out.println("Enter you Name: ");
String Name=in.nextLine();
System.out.println("You have entered: "+Name);
The code is correct. You think it's wrong because the code waits for you to enter more values and while it does that it displays nothing. You should always have print messaged before reading from the console:
do{
[B]System.out.println("Enter you Name: ");[/B]
String Name=in.nextLine();
......
[B]System.out.println("Enter 0 or 1 to continue: ");[/B]
n=in.nextInt();
} while (n<2);
[B]System.out.println("Finished!");[/B]
I don't think it's possible. Whenever you type something it is stored in the variable, therefor you need an if statement, but I would suggest to wait for other people responses as well in case someone has a better answer.
Actually there is something that can simulate that.
Write a thread that keeps running in parallel with your main. Whenever you read the input set a local variable defined in the Thread. The Thread in its run method will keep checking the value of that variable in an endless loop. If "quit" is found you will break from the loop and call the quit in the run method.
Although you will need to be careful not to leave any unfinished business in the main, so you might want to pass more variables to the Thread from the main, so it will monitor the state of the main
The code doesn't compile. Post the correct code
Given the information it is difficult to understand why it doesn't work and I believe there isn't anything else you can tell me so I would suggest first:
Remove completely the update from the code.
Do you know how to use the Vector class? How to put objects inside it?
java.util.Vector
In your first loop after you get the data from the DB and convert them, put them in a Vector. Then outside the loop close the ResultSet, the Statement and print the Vector:
class Data {
public String after = null;
public int length = 0;
public int number = 0;
public String id = null;
public Data() {
}
public Data(String after, int length, int number, String id) {
// set the arguments to the global variables
this.after = after;
....
}
Vector v = new Vector();
while (res.next()){
before = res.getString("content");
id = res.getString ("id");
oldnumber = Integer.parseInt(res.getString("number"));
chapter_number = Integer.parseInt(res.getString("chapter_number"));
c.SetBefore(before);
after = c.Converte();
length = c.getLength();
number = c.newNumber(chapter_number, oldnumber);
Data data = new Data(after, length, number, id);
v.add(data);
}
rs.close();
stm.close();
// print the Vector using a for loop
Data d = (Data)v.get(i);
System.out.println(d.after+","+d.length+","+.....);
Once you are satisfied by the results of the print from the Vector use another for loop to run the update
If you are worrying why they are not printed orderly then they are not suppose to. They are threads and they are executed in parallel. So this output shouldn't be wrong:
10 8 6 9 4 7 2 5 3 1
Sorry for my double post but when you are done with your solution, there is a much smarter - but more difficult - way you can try:
String [] words = {"Alpha", "Bravo", ....};
Now when you get each character you can use its ASCII equivalent, then normalize that value to be between 0 to 25 and use it as index. In that way you will not have to write entire code with if statements:
char ch = stringArray[i];
int index = // convert ch into an int and normalize
System.out.print(words[index]+" ");
This is totally unnecessary:
[B]stringArray.toString();[/B]
System.out.println("Alpha");
It doesn't do anything. You don't need it. And put the if statements in the for loop:
for(int i = 0; i < stringArray.length; i++) {
char ch = stringArray[i];
if ((ch=='a')||(ch=='A')) {
System.out.print("Alpha ");
} else if ((ch=='b')||(ch=='B')) {
} else if (...) {..
....
} else {
// IN CASE IT IS NOT A LETTER
System.out.print(ch+" ");
}
}
With switch:
for(int i = 0; i < stringArray.length; i++) {
char ch = stringArray[i];
switch (ch) {
case 'a':
case 'A':
System.out.print("Alpha ");
break;
case 'b':
case 'B':
System.out.print("Bravo ");
break;
....
....
...
default:
System.out.print(ch+" ");
}
}
We don't know what the "Convertor" does. The code seems OK, are you sure the loop doesn't stop?
Try printing the data you get from the ResultSet and compare them with what data you have in the database.
Also I would suggest to close the Statements: stm, stm1 as well
nf.format(calcTaxes (grossPay))
How do you define the "nf"?
I would suggest to write a simple program with only a double number and the Formatter, and try to print it the way you want. Once you accomplish that, transfer the Formatter to your main method
can anyone please help me start the min,max and avg off?
with some examples. ill appreciate that a lot
Assuming that you have a while loop from which you read the numbers from the keyboard, then:
Scanner in = new Scanner(System.in);
int sum = 0;
int count = 0;
int min=0;
int max=0;
System.out.println("Enter numbers");
while (in.hasNextInt()) {
int curr = in.nextInt();
System.out.println("Number given: "+curr+". Next:");
sum = sum + curr;
count++;
}
System.out.println("Sum of all numbers: "+sum);
System.out.println("Numbers given: "+count);
You have what you need to calculate the average. As for the min, max, initialize them with first number you will enter. Think how you will use the "count" to accomplish that.
Then you have all you need to use Jocamps advice:
then for succeeding inputs, compare if that is less/greater than mini and max respectively.
I want to create a login jsp form that connect with servlet and mysql for database. And I'm using netbeans and glassfishV2. Please help me to solve this problem.
I am sorry for not contributing much to your question apart for my comments to pardon_garden code.
The link that I provided should be able to help you start. Also try Peter_budo's link
Because my amateur code still works.
By using the command db.close() the following method is invoked
public void close() throws SQLException { dbCon.close(); }
and the connection is closed.
And the Statement? Where do you close it?
Also you have a Statement declared globally and inside the method I described you open one locally, and you don't have methods to close neither of them.
Also you do this:
public int executeUpdate(String s) throws SQLException {
int count = stmt.executeUpdate(s);
return count;
}
Where do you create the "stmt"? I am sorry but I didn't see it. I could be wrong.
To pardon_garden
ResultSet rs = s.executeQuery(sql);
return (rs == null) ? null : rs;
First of all, this: ResultSet rs = s.executeQuery(sql);
never returns null
And also this is very "not inteligent": return (rs == null) ? null : rs;
If it is null return null, if it is not null return itself???????
It is exactly the same as return rs;
Also you do this: Statement s = dbCon.createStatement();
but I don't see where this instance can be closed?
The same goes for the "doQuery" method where you define 2 unnecessary variables even though you have global as well.
Peter_budo has created this amazing tutorial:
JSP database connectivity according to Model View Controller (MVC) Model 2
Why did you post this amateur code?
write a program that will accept an integer number from 0-9999. Then, convert this number into words
Start a new thread
how can i mask the textbox for searching the date and amount for my text file?
This still doesn't make any sense.
What is the format of your file. Based on what you say why don't you read the file line by line and then do whatever you want?
i already tried that one...
but i need the masking for searching some data in my file....
thanks
Can you elaborate that please?
Have you tried using another attribute than "disabled".
The input "text" has an attribute "readonly". Search what other attributes the checkbox has.
@javaAddict. hey man how u doing. I need a favor. I wrote some code in Java to convert java objects to JSON objects that can then be processed in javascript. would you mind Testing it?
This also goes to anyone who is interesting.
it makes extensive use of the Java Reflection API but you don't have need a great deal of Reflection knowledge to test it.
I am sorry, I am not familiar with JSON, not have I ever used what you are trying to do.
Sorry for not being able to help you
java programming is the latest program around the world.
i am a student of computer programing our assessment are too near, i need to learn java programs in just four days starts from tomorrow ... could you?
Providing private lessons is not what we do here. Check the Read Me thread with the tutorials on top of this forum, and we will answer specific questions about any errors you might get with your code
creating a factory class just to instantiate a singleton object is redundant. factory classes are there to ease the creation of several classes not one
Actually the article I was referring said exactly that. The factory class I mentioned is to be used to create more than one class, but one instance of each class.
The best we to access a singleton object is via a factory method defined by the class itself i.e. getInstance. (see GOF design patterns for more info).
So if we combine our ideas, the class would have a public constructor, but also a static method capable of creating and returning only one instance of the class; the programmer is free to follow any path they choose depending on their goal
In case you haven't noticed we have October. Tanning is out of the question, unless you plan to use the application at the South hemisphere.
I don't agree. let me give you the definition of static members and Singleton classes from a design point of view. static members are those that exist independent of every instances of that class. thus you would only use static members if they result to the same thing or same action for every instance of that class. Singleton objects exist because for every other object in the same context there should exist only one of it self.
I guess you already know this. its a programming flaw to to create a utility class and also make it possible to create instances of it. You should always prevent this by making its constructor private and provide no way of access an instance of such class (utility classes are only for convenience and should follow strict design rules).
If an instance is needed then it should not be a utility class
Well I also agree with you. If a class will have only static methods then the constructor should be declared private so no one can instantiate it.
Of course my concern was, is it better instead of having static methods to have a singleton class. In that way it will also be impossible to have many instances.
In one of the links I read, and I agree with it, was that you should not create your class to be as singleton, but if you want it to be initialized once then create a factory class with a method that …
static java methods is a bit slower than default methods .!
I've found in another forum someone saying the opposite, although I believe he was referring to C#.
Where did you get that information?
I encountered some code at work that got me thinking.
There was a util - class that had some methods that had nothing to do with the state of the class. They were all "util" methods. Meaning that they could all be declared static with no problem because the only thing they did was some calculations and return the result.
But methods were declared non-static and the class was created as singleton. So even though an instance was needed to call them, because the class was singleton only one instance was created in the end.
I asked the programmer why the methods weren't declared static, and I was told that it is better to a have the class as singleton, than have the methods static.
I search the net for an answer:
http://forums.sun.com/thread.jspa?threadID=672775&start=0&tstart=0
http://blogs.msdn.com/scottdensmore/archive/2004/05/25/140827.aspx
http://www.ibm.com/developerworks/webservices/library/co-single.html
And the conclusion that i came is that it depends on the way you want to use the singleton class. Sometime it is better and sometimes you should avoid it depending on the implementation and how you want to use it.
Now I believe that the methods should be static in the case I described. What I would like, is a response about this comment:
I was told that it is better to a have the class as singleton, than have the methods static
Do you agree or not?
No need to start new thread, just report it and I can change format from code snipped to thread as I did now. Nevertheless I know your intention was the best.
I am sorry.
I am new in java programming i want to ask help on how to code a program method using endWith. Thanks.
Start a new thread, don't hijack other's threads. Especially to post twice the same thing when the Original Poster hasn't got his answer yet. Do you think it's nice for people to answer your question instead of his in his own thread?
I believe that OneRowNim.java is correct
Why do you believe that, when you have created a new class named OneRowNim2 and in your main you call the old one?
First of all next time use separate classes for database transactions:
class DBManager {
public static int insert(..... arguments) throws SQLException {
// open new connection
// execute query
// close connection
}
}
Never do that:
Statement stmt = con.createStatement();
stmt.executeUpdate("CREATE TABLE ACCOUNT " +
"(AccName VARCHAR(32), AccNum INTEGER, Deposit FLOAT, " +
"Balance FLOAT, Withdraw FLOAT)");
You never create the database using java code. I saw that you placed that code in the main class. Does this mean that whenever you start your application a new table will be created?
The database has to pre-exist. Create it by running the query at an sql command prompt.
This is wrong:
rs1.next(); //moves the cursor forward one row
//txtA1.setText(rs1.getString("AccName"));
//txtA2.setText(rs1.getString("AccNum"));
//txtTextArea.append("Account Holder's Name :"+rs1.getString("AccName"));
// txtTextArea.append("Account Number :" + rs1.getString("AccNum") + "\n");
st1.close();
rs.next returns true or false depending if the query returned a row:
if (rs.next()) {
// use rs.get... methods to get the info.
} else {
// no rows returned
}
If the query returns more than one row:
while (rs.next()) {
// use rs.get... methods to get the info.
// put the results into a Vector
}
Also I would suggest to change this: Select * from ACCOUNT where AccName Like '"+accname+"'"
to this: Select * from ACCOUNT where AccName = '"+accname+"'"
Don't forget when you open a connection to close it after the transaction. From what I see you open the connection at the main and then you …
You need t o implement a for loop. I assume that you have been taught how to calculate the sum using a for loop.
This is the same thing to you will need to change the firmula:
sum = 1 + 2 + 3 + 4 + ....
double sum = 0;
for (int i=1;i<N;i++) {
sum = sum + i;
}
First of all first print the query that you run:
String query = "Select * from ACCOUNT where AccNum like '"+number+"'";
System.out.println(query);
rs1 =st1.executeQuery(query); //SQL statement
It will print:
Select * from ACCOUNT where AccNum like '5'
But AccNum is declared as number, but when you write '5' it treats it as char.
So you need to adjust your query to be like this:
Select * from ACCOUNT where AccNum like 5
Also you don't need to define the method as abstract since you have it in an interface. If the class was abstract some can be implemented and others defined as abstract.
but in interfaces all methods are declared like this:
public interface AShape
{
public static double DEFAULT_SIZE = 1.0;
public double calcPerim();
}
Also you don't need the "perimeter" variable. The method will calculate the perimeter and return it, You will implement it in your classes that implement the interface.
How do I convert the Integer to String by:
1. Asking the user to input the number (Eg: 704 = Seven Hundred and Four)2. Display the output in JTextArea.
Thanks for your support.
Start a new thread. This is a 3 year old thread.
Actually I made a mistake.
You don't need to declare it static in the interface, just use the:
AShape. in front
If it was not an interface but a simple class then you would have in order to call it like this:
AClass.variable
sorry
Try:
System.out.printf("The Value for X is: " + VarX);
// OR
System.out.println("The Value for X is: " + VarX);
I am not familiar with printing using: printf. I know that it is used to format the output, but I have never used it before. You might want to try the API:
You can access it because you write:
AShape.DEFAULT_SIZE
If DEFAULT_SIZE is declared as static it means you don't need to initialize the class in order to call it. So whenever you call:
AShape.DEFAULT_SIZE you have access to the DEFAULT_SIZE of the AShape class.
If TestShapes implemented the AShape then you didn't have to put the name of the class ahead, you could just call the DEFAULT_SIZE right away like this:
System.out.println(DEFAULT_SIZE);
But now that it doesn't implement the interface you need to "call" the class fist:
System.out.println([B][U]AShape[/U][/B].DEFAULT_SIZE);
First of all start a new thread with your new code. This kind of thread is only for posting working code as an idea of sharing code for educational purposes.
Start a new thread and don't select code snippet.
Post your new code.
Then we will talk
please i need it now
Now, now, now, now, ...
You forgot to add this:
<option [B]value="user"[/B]>User</option>
I haven't read your code; just stopped reading when I reached that part.
This is a site that helped me a lot to learn html and java script:
w3Schools
Also for "select" use this: request.getParameter("role")
It is used whenever you submit One value.
Use the one that returns an array when you expect more than one "value" per "name"
i am trying to learn servlets so i downloaded tomcat server.I learned that i have to do this...set path=c:\tomcat\lib\servlet.jar
or click the exe file tomcat6 in the bin folder. I tried to connect with localhost8080 it is not working.I am not getting Tomcat home page.Any help will be great.
Two questions?
- Did you start the server?
- What url are you using?
i want the code for avl trees using dictionaries in java... with total insert , delete, display options
Why did you create 2 identical threads? What were you trying to accomplish?
hi everyone i'm new to programming and i have to say its fun and frustrating at the same time could anyone help me fix this??
This is a 5 year old thread. That were you thinking posting here? Do you random peek threads and start writing irrelevant things in them?
If it says it is missing a return statement, then add one since your method is declared to return a String.
If you have any more questions start a new thread.