 |
 |
When posting your question please:- Choose the correct forum for your message. Posting a VB.NET question in the C++ forum will end in tears.
- Be specific! Don't ask "can someone send me the code to create an application that does 'X'. Pinpoint exactly what it is you need help with.
- Keep the subject line brief, but descriptive. eg "File Serialization problem"
- Keep the question as brief as possible. If you have to include code, include the smallest snippet of code you can.
- Be careful when including code that you haven't made a typo. Typing mistakes can become the focal point instead of the actual question you asked.
- Do not remove or empty a message if others have replied. Keep the thread intact and available for others to search and read. If your problem was answered then edit your message and add "[Solved]" to the subject line of the original post, and cast an approval vote to the one or several answers that really helped you.
- If you are posting source code with your question, place it inside <pre></pre> tags. We advise you also check the "Encode HTML tags when pasting" checkbox before pasting anything inside the PRE block, and make sure "Ignore HTML tags in this message" check box is unchecked.
- Be courteous and DON'T SHOUT. Everyone here helps because they enjoy helping others, not because it's their job.
- Please do not post links to your question in one forum from another, unrelated forum (such as the lounge). It will be deleted.
- Do not be abusive, offensive, inappropriate or harass anyone on the boards. Doing so will get you kicked off and banned. Play nice.
- If you have a school or university assignment, assume that your teacher or lecturer is also reading these forums.
- No advertising or soliciting.
- We reserve the right to move your posts to a more appropriate forum or to delete anything deemed inappropriate or illegal.
When answering a question please:
- Read the question carefully
- Understand that English isn't everyone's first language so be lenient of bad spelling and grammar
- If a question is poorly phrased then either ask for clarification, ignore it, or mark it down. Insults are not welcome
- If the question is inappropriate then click the 'vote to remove message' button
Insults, slap-downs and sarcasm aren't welcome. Let's work to help developers, not make them feel stupid.
cheers,
Chris Maunder
The Code Project Co-fou
|
|
|
|
 |
<big><b></b>The computer asks a series of questions to arrive at the number the user has in mind. The user can only respond with “higher”, “lower” or “yes”. The game ends when the user responds with “yes” or “end”. Example: Example: User chooses number 40 in his mind. Computer: Is the number 30? User: higher Computer: Is the number 50? User: lower Computer: Is the number 35? User: higher Computer: Is the number 40? User: yes Note: The program should be written to arrive at the answer asking the least number of questions. import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Random; public class NumberGuess { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub int max=0,min=0; Random rand=new Random(); int ComputerNumber=rand.nextInt(100); while(true) { System.out.println("Is the number:"+ComputerNumber); System.out.println("Please give higher or lower or yes:"); InputStreamReader input = new InputStreamReader(System.in); BufferedReader reader = new BufferedReader(input); String value=reader.readLine(); String yes="yes",lower="lower",higher="higher"; int temp; if(value.equals(yes)) { System.exit(0); } if(value.equals(lower)) { int temp1=ComputerNumber;//57 temp=ComputerNumber+(rand.nextInt(ComputerNumber))+1; ComputerNumber=temp; System.out.println("New number:"+ComputerNumber); } if(higher.equals(value)) { int temp1=(ComputerNumber%10)/2-10;//59 int temp2=(ComputerNumber/10)*10; temp=(ComputerNumber-rand.nextInt(ComputerNumber)); System.out.println(temp1); System.out.println(temp2); ComputerNumber=temp2+temp1+1; System.out.println("New number:"+ComputerNumber); }
}// end }//main end }//
</big>
|
|
|
|
 |
i have created a jar to copy two files using Variable argument whichis working correctly
e.g java jar filecopy.jar file1 file2
i want to provide a -help option in this which will be like below and user must aware what he has to pass in arguments
e.g java jar filecopy.jar -help
how do i do this not getting any idea :new to java,urgent help required !!!!
modified yesterday.
|
|
|
|
 |
You just add the code to check for "-help" as a filename, and branch to the code which displays the help.
|
|
|
|
 |
did you men alike this ...
public static void main(String[] args) {
String filename1 = args[0];
String filename2 = args[1];
if ((!(filename1.length() > 0)) || (!(filename2.length() > 0))) {
System.out
.println("please provide the foldername and string to be replaced ");
return;
}
if(args[0] == "help" )
{
System.out
.println("please run the utility as filecopy file1 file2");
}
can you edit it if wrong
|
|
|
|
 |
Something like that, but you can easily test it.
|
|
|
|
 |
hi guys,
i have a problem in connecting my web application in javaEE, when i run the servlet i have an error : java.lang.NullPointerException
and this is a screenshot for the error : http://im51.gulfup.com/TnfWrg.jpg
this the code where i try to connect to the database
public class login_servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public login_servlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
java.sql.Connection connexion = null;
java.sql.Statement statement = null;
ResultSet resultat = null;
String userQuery="";
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
}
try {
connexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/Dlala", "IPLS", "password");
} catch (SQLException e) {
e.printStackTrace();
}
try {
statement = connexion.createStatement();
resultat = statement.executeQuery("SELECT user FROM Client;");
while (resultat.next()) {
userQuery = resultat.getString("user");
}
} catch (SQLException e) {
} finally {
try {
if (resultat != null)
resultat.close();
if (statement != null)
statement.close();
if (connexion != null)
connexion.close();
} catch (SQLException ignore) {
}
}
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n=request.getParameter("user");
String p=request.getParameter("pass");
HttpSession session = request.getSession(false);
if(session!=null)
session.setAttribute("name", n);
if( n.equals(userQuery)){
RequestDispatcher rd=request.getRequestDispatcher("home.jsp");
rd.forward(request,response);
}
else{
out.print("<p style=\"color:red\">Sorry username or password error</p>");
RequestDispatcher rd=request.getRequestDispatcher("login.jsp");
rd.include(request,response);
}
out.close();
}
}
|
|
|
|
 |
So you need to use your debugger to identify where and why you have a null reference.
|
|
|
|
 |
how to do pagination in java swing properly with defaultable model......i have done this but not working properly...:
please give me the code of pagination in swing? i am trying but not working properly......
|
|
|
|
|
 |
Hello. I am trying to write small java bean. The classes for java beans compile successfully. When I try to use them in my .jsp file I get the error
DatabseDAO cannot be resolved to a type
Here are declarations for these beans
import java.sql.*;
import java.io.*;
import java.util.*;
public class DatabseDAO implements Serializable
{
}
import java.io.*;
public class PersonBean implements Serializable
{
}
And here is how I use them in jsp file
<%
DatabseDAO db2 = new DatabseDAO();
ArrayList persons_list = db2.GetPersons("SELECT * FROM People");
PersonBean person_bean = null;
for (int i=0; i<persons_list.size(); i++)
{
person_bean = (PersonBean) persons_list.get(i);
%>
<tr>
<td><% =person_bean.getPersonId() %></td>
<td><% =person_bean.getPersonName() %></td>
<td><% =person_bean.getPersonAddress() %></td>
</tr>
<%
}
%>
The classes (bean and DAO) are not declared in any package. What is wrong here ?
This world is going to explode due to international politics, SOON.
|
|
|
|
 |
Shouldn't DatabseDAO be declared public?
|
|
|
|
 |
I found the answer.
modified 21-Dec-14 22:26pm.
|
|
|
|
|
 |
Hello, I am doing a project for school and I cannot figure out what is wrong.
int AB = 450;
int BC = 140;
int CD = 120;
int DE = 320;
int EF = 250;
int FG = 80;
String Place1, Place2, Place3, Place4, Place5, Place6, Place7, Place8, Place9, Place10;
double MPG;
double MPH;
double Gallon;
String Trip1;
double Hour1;
int Minute1;
double Money1;
int Dist1;
String String;
Scanner scan = new Scanner(new File("C:\\Users\\Evelyn\\workspace\\ACSL\\src\\testimput"));
NumberFormat numfmt = NumberFormat.getCurrencyInstance();
NumberFormat decfmt = new DecimalFormat("00");
String delims = "[ ]+[,]";
MPG = scan.nextDouble();
Gallon = scan.nextDouble();
MPH = scan.nextDouble();
Place1 = scan.next();
Place2 = scan.next();
Place3 = scan.next();
Place4 = scan.next();
Place5 = scan.next();
Place6 = scan.next();
Place7 = scan.next();
Place8 = scan.next();
Place9 = scan.next();
Place10 = scan.next();
For the program, there is a textfile that contains exactly:
Quote: 25, 3.79, 50
A, C
E, F
C, G
B, E
A, G
However, when I attempt to run it crashes.
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at Prog_2014.main(Prog_2014.java:34)
Does anyone know how to fix this? What am I doing wrong and what must I add.
|
|
|
|
 |
You have not set the comma as a delimiter in your scanner, so it is reading it as a part of the next field.
|
|
|
|
 |
Try adding the following line before the first call to nextDouble .
scan.useDelimiter("\\s*[,\\s*]\\s*");
modified 21-Dec-14 10:23am.
|
|
|
|
 |
Thank you so much! That's very useful. Would you mind explaining the logic and how that statement works so I will be able to do it in the future?
|
|
|
|
 |
Firstly, if you create a delimiter string (as you did in your code) you still need to apply it to the scanner so that the scanner applies it to every line it reads. The default for the scanner is to ignore all whitespace characters (as described in http://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#isWhitespace(char)[^]). If that does not control the input data that you are working with then you need to set a custom delimiter, which can be a simple string of characters, or a regular expression pattern, as described in http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html[^].
The pattern I suggested ("\\s*[,\\s*]\\s*") breaks down as follows:
Actual pattern is: \s*[,\s*]\s*
\s* : 0 or more whitespace characters (e.g. spaces, tabs etc)
[,\s*] : a comma or 0 or more whitespace characters
\s* : 0 or more whitespace characters (e.g. spaces, tabs etc)
I'm not an expert in RegEx patterns so I am sure this can be improved upon. But what you have in your input file is:
25.0, 37.1, 10
So the the basic pattern is:
0 or more whitespace characters
0 or more comma characters
0 or more whitespace characters
a number
0 or more whitespace characters
|
|
|
|
 |
Hello. I am trying to authenticate input from databse using JDBC and then redirect the user to another page accordingly. Here is my code for small servlet that does authentication
public class ServletAuthUser extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
try
{
String sUserName = request.getParameter("username");
String sPassword = request.getParameter("password");
String sURL = "Jdbc:Odbc:ServletsDB_DSN";
String sForName = "sun.jdbc.odbc.JdbcOdbcDriver";
MyDatabase db = new MyDatabase(sURL, sForName);
boolean bIsUserValid = (sUserName.equals("admin") && sPassword.equals("admin")) ? true : false; db.CloseConnection();
if(bIsUserValid == true)
response.sendRedirect("welcome.html");
else
response.sendRedirect("error.html");
}
And here is my welcome page.
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h1>Welcome ADMIN.</h1>
</body>
</html>
I have tried recompiling the servlets and then restarted the apache server several times but my login servlets still shows me the old output that I tried little ago (another servlet which generates data using JDBC). Why is it still showing the same old output despite recompiling the servlet and restarting the server? Please ask for any info if you need. Thanks for any input.
EDIT
I was not writing the deployment descriptor properly.
modified 19-Dec-14 14:13pm.
|
|
|
|
 |
Are you sure that you have copied the new servlet to the server correctly?
|
|
|
|
|
 |
It is unlikely that any response in this forum would be enough to help you, if you don't understand how to create a simple application such as you describe. For a start you will need to design your database and learn how to access it in Java; take a look at http://docs.oracle.com/javase/tutorial/jdbc/index.html[^]. As for the login screen, that is just a matter of adding a few labels, text boxes and buttons to a JFrame and accessing the information provided by the user. There are many useful samples in http://www.codeproject.com/KB/java/[^].
|
|
|
|
 |
thnks Richard
|
|
|
|
 |
What have you achieved so far?
The Eclipse IDE is a Eclipse RCP Application.
You can easily exchange the original login and build your thingy as a Eclipse RCP Plugin.
Lars Vogel has a lot of really good tutorials for you:
http://www.vogella.com/tutorials/EclipseRCP/article.html[^]
regards Torsten
I never finish anyth...
|
|
|
|