|
Leela Krishna Yadav wrote: I tried something but it is wrong OK, but we cannot guess what that is supposed to mean. Please edit your question and explain exactly what is wrong, and what you need help with.
|
|
|
|
|
|
You should use "binary chop", not random values. For each response you need to take half the distance between the current and previous guess. If the user says "higher" then add it to the guess, else subtract it. So the sequence for your values would be something like:
Computer : 75 User : chose 53
Lower
subtract half of 75 - 0 : 38 Higher
add half of 75 - 38 : 57 Lower
subtract half of 57 - 38 : 48 Higher
add half of 57 - 48 : 52 Higher
add half of 57 - 52 : 54 Lower
subtract half of 54 - 52 : 53 Correct!
|
|
|
|
|
Thank you Richard for that help i really appreciate it
|
|
|
|
|
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 31-Dec-14 4:09am.
|
|
|
|
|
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?
|
|
|
|
|
Where is your import on .jsp page?
Should be
<%@page import="<PackageName>.DatabseDAO" %>
|
|
|
|
|
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?
|
|
|
|
|
Hello friends,
I am new to Java having understanding of the basics. But, I have given a mini project to make the Login Frame on the eclipse platform and to verify the user login details and thereby showing his access rights on the other frame. I have to use Derby database for it.
I am really confused as I have to submit it in next 3 days
It would be kind if anyone can help me.
Thanks
|
|
|
|