That is because you haven't defined such a constructor in your Lottery class.
Come on you could have figure that out. Put some effort into it
That is because you haven't defined such a constructor in your Lottery class.
Come on you could have figure that out. Put some effort into it
Thanks man, yea that is true but I'm making sure because it is my assignment.
Can't you compile ti and run it?
Also try to add some elements in the ArrayList and then use a for loop to print them.
Do you know how to find the API for the ArrayList?
One way that came to mind is use 2 for-loops
Try closing the writer: //writer.close();
Why did have that line in comment?
Remove the comment
you will want something like;
for (int index=0; index <numbers.length; index++){ //Statement printing the number ie. 0, 1, 2 ,3...etc for(int index1=0; index<numbers.length; index1++){ //statement printing thestars } //statement toprint new line }
you cannot put the printstatement comtaining the | and the \n in the inside for loop because it will print it everytime and you only want it before and after the stars respectively.
Beside the fact that the second for loop is wrong, the others (stultuske, Fuze) have tried to help him out by giving him hints on how to solve it on his own.
It would be very easy for them to post the correct code, but nanna wouldn't have learned much that way.
Move the code that reads the input in the while loop
Try this:
post only the code of the Television class.
As the printStacktrace states:
...
at java.util.Scanner.nextInt(Scanner.java:2050)
at Television.setChannel(television.java:28)
...
There is an error at the method: setChannel of the Television class at line: 28. And the problem is with the way you use the nextInt method of the Scanner class. Better use the 'next' method and convert what you get to int.
It wasn't very difficult to figure that out, so post only the code of the Television class.
AND USE CODE TAGS. Click the button: '#' that is above where you post your reply
Don't worry everybody does that mistake. And I am tired of replying to it.
Just use the equals method.
When you use '==' to compare String (remember Strings are Objects) you compare to see if they are the SAME objects. They are not, but they have the same value:
if (Input1.equals("P")) {
.....
}
String s1="a";
String s2="a"; //2 Different objects.
(s1==s2) //false
(s1.equals(s2)) //true
BUT
String s1 = "a";
String s2 = s1; //they are the same
(s1==s2) //true
(s1.equals(s2)) //true
Also you will need to have all of your IFs as 'else if':
if () {
} else if () {
} else if () {
} .... {
} else {
System.out.println("");
System.out.println("Error - Input not recognised!");
System.out.println("");
}
And now that I looked at it, Yes the problem IS the 'while'. I was about to say that it made no difference even if it was 'if',
BUT:
When you use While and the query doesn't return anything, the variables: 'mobileno' that are inside the while don't change value, so when later you do: if(mobileno.equals(request.getParameter("mno")) && password.equals(request.getParameter("pwd")))
It will return true even if there is no entry in the database because you use the old values that 'mobileno'
So use the if statement
When you are creating a new user, are you checking if the user already exists before performing an INSERT?
Perhaps the mobileNo should be declared as Primary Key. Then if you try to insert a new row that has the same mobileNo it will throw an SQLException.
Or if you unfamiliar with this, you can try quering with mobileNo and if the query returns something don't INSERT.
Tha above are suggestions and questions for the logic used in the: newregister.jsp
thanks for your help. its working now
Did you put BufferedReader in a try-catch block? Do you want an example?
Do you know how to catch Exceptions? I assumed that you do.
Once you get the file: File file = fc.getSelectedFile();
use the code to read the file's lines.
File file = fc.getSelectedFile();
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = reader.readLine();
while (line != null) {
//do whatever you want with the line read.
//perhaps append it to the TextArea.
line = reader.readLine();
}
reader.close();
Your imports are fine.
With this code you can read files:
File fileName;
//OR
String fileName;
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line = reader.readLine();
while (line != null) {
//do whatever you want with the line read.
//perhaps append it to the TextArea.
line = reader.readLine();
}
Read the file for its data.
Then use that data to query the database: Run an UPDATE to record you want to update.
Learn how to read files.
Learn SQL.
Learn how to run queries with java.
Every time you do: Time application = new Time();
you create a new object and therefor you reset the values back to zero.
So create only ONE object (do this once: Time application = new Time();
)
And then use the application instance to change its values.
By the way, the Time class is totally wrong. The get, set methods are not suppose to print anything:
class Time {
private int seconds = 0;
....
....
public Time() {
}
public Time(int seconds, ......) {
this.seconds = seconds;
.....
.....
}
public int getSeconds() {
return seconds;
}
public void setSeconds(int seconds) {
this.seconds = seconds;
}
}
Now if your teacher wants the set methods to return boolean, then:
public boolean setSeconds(int seconds) {
if (seconds<0) {
return false;
}
this.seconds = seconds;
return true;
}
How about some code
I have tested and here is an example:
public static void main(String[] args) {
BufferedReader lnr = null;
String line = null;
int N=1000;
int totalRecords = 0;
String [][] array = new String[N][];
try {
lnr = new BufferedReader (new FileReader("accountList.txt"));
for (line = lnr.readLine(); line!=null; line = lnr.readLine()) {
array[totalRecords] = line.split(" ");
totalRecords++;
}
lnr.close();
} catch (Exception e) {
System.out.println("An error has occured: "+e.getMessage());
//e.printStackTrace();
}
for (int i=0;i<totalRecords;i++) {
for (int j=0;j<array[i].length;j++) {
System.out.print(array[i][j]+" ");
}
System.out.println();
}
}
2-D arrays in java don't exist. Actually what you do is create a single array and each element has another array:
String [][] array = new String[N][]; //2-D array with N rows
array[totalRecords] = line.split(" "); // the row: [U]totalRecords[/U] will have the array returned by the split method.
So the array[totalRecords]
has another array inside it and to access it you do:
array[totalRecords][0],
array[totalRecords][1],
array[totalRecords][2]
You initiall set the max row to be N=1000 because you don't know the size of the file. But you use the totalRecords variable to count the lines, because inside the loop you don't an int variable so you must do:
array[totalRecords] = ...
totalRecords++
in order to place the next line to the next row.
When you print the values you don't use the N at the first loop because not all rows of the array have value. You use the totalRecords. Remember you have used to count the …
It was printed because it was the accountId of the second line of the file. You are the one who wrote the code that loops through each line of the file:
for (....) {
}
With the above code you loop each line and get the values from each line:
account[0], account[1], account[2], ....
Why would you have it any other way? What would be the point of getting only the first line and not the rest of the lines of the files?
Now if you want to store the entire file in a single array, then Yes, use a 2-D array:
int N=1000;
String [][] array = new String[N][]
....
....
int count=0;
for (....) {
array[count] = line.split(" ");
count++;
}
I don't know if the above will work. You might want to test it.
If it doesn't then declare the 2-D array: [N][5] and in the loop take the line in a different 1-D array and put the values in the 2-D array.
I have tried this:
public static void main(String[] args) {
BufferedReader lnr = null;
String line = null;
try {
lnr = new BufferedReader (new FileReader("accountList.txt"));
for (line = lnr.readLine(); line!=null; line = lnr.readLine()) {
String account[] = line.split(" ");
System.out.println(account[0]);
}
lnr.close();
} catch (Exception e) {
System.out.println("An error has occured: "+e.getMessage());
//e.printStackTrace();
}
}
The file has these:
12345 1234 200000 Jerry Lopez
55555 1234 200000 Jerry Lopez
And it printed:
12345
55555
for (line = lnr.readLine(); line!=null; line = lnr.readLine()){
String account[] = line.split(" ");
//print whatever you want
//and do whatever you want with the elements:
//account[0], account[1], account[2], account[3], account[4]
}
Look at your code:
for (line = lnr.readLine(); line!=null; line = lnr.readLine()){
String account[] = new String[4];
int count = 0;
int accountNumber[] = Integer.parseInt(account[count][1]);
int pinNumber[] = Integer.parseInt(account[count][2];
int balance[] = Integer.parseInt(account[count][3];
String name = account[count][4];
System.out.println(accountNumber[0][1]);
}
You read the line BUT inside the loop you don't do anything with it. Instead you call this: Integer.parseInt(account[count][1]);
.
The account array is not 2-D and the Integer.parseInt does not return an array.
Now inside the line you have something like this:
12345 1234 200000 Jerry Lopez
So If you use the split method:
String [] array = line.split(" ");
The elements of the array would be:
0: 12345
1: 1234
2: 200000
3: Jerry
4: Lopez
Also for reading from a file try this shorter version:
BufferedReader lnr = new BufferedReader (new FileReader("accountList.txt"));
When comparing Strings, almost at 99.99% of the cases, you will need to use the equals method:
String s1 = "aa";
String s2 = "aa";
System.out.println( s1.equals(s2) ); //it will print true
System.out.println( s1.equals("aa") ); //it will print true
System.out.println( "aa".equals(s2) ); //it will print true
System.out.println( s1==s2); //it will print false
The equals compares the values of the objects. 's1', 's2' are not the same objects but they have the same value. So use the equals.
When you use the '==' you compare the objects themselves. So like I said:'s1', 's2' are not the same objects so '==' will return false
BUT
String s1 = "aa";
String s2 = s1;
System.out.println( s1==s2); //it will print true
So when you compare Strings: next[0] == ID
use : next[0].equals(ID)
In Physics everything is measured in meters and seconds.
So after you get the speed and distance, Convert both to meters/sec and meters. After you do the division you will have a clean value in seconds which can be easily converted to minutes and hours.
If the final value has decimal points: 65.33 seconds then keep them at the seconds value or round them or have another variable for miliseconds:
1 minute and 5.33 seconds or
1 minute and 5 seconds or
1 minute and 5 seconds and 330 milliseconds
Yeah I used proper coding for my powers and square roots, yet when I compile it still returns an error message saying i need a ; after fToC = (5 / 9)(f - 32);.
fToC = (5.0 / 9.0)*(f - 32)
You need to put commands such as this in methods: System.out.println("Input temperature in ºF ---> ");
Also in java if you want the power of a number use: Math.pow(double a, double b)
the variable q is not declared in the class A and I dont want to declare it. Is there anyway this can be done? I tried that and it says "non-static variable cannot be referenced from a static context" If also someone could share with me what this means. Thanks alot
BestJewSinceJC probably meant this:
C c = new C();
c.q;
That is how non static variables are accessed.
You can do the above anywhere you want (provided that q public)
And again, this can be done:
C c = new C();
c.q;
c.o;
c.p;
This NOT:
B b = new B();
b.q;
These calculation need to go inside the methods:
//The getArea method returns the value that is stored in the area field
double area = (PI * radius * radius); //Formula to get the area of the circle
public double getArea()
{
return area;
}
//The getDiameter method returns the value that is stored in the diameter field
double diameter = (radius * 2); //Formula to get the diameter of the circle
public double getDiameter()
{
return diameter;
}
//The getCircumference method returns the value that is stored in the circumference field
double circumference = (2 * PI * radius); //Formula to get the circumference of the circle
public double getCircumference()
{
return circumference;
}
Try it this way:
public double getArea()
{
return (PI * radius * radius);
}
public double getDiameter()
{
return (radius * 2);
}
public double getCircumference()
{
return (2 * PI * radius);
}
With the way you had it the variables were initialized when the object was created but then if you changed the radius, these values (area, diameter, ...) weren't changed.
Regardless of whether the elements have been separated by a split, regular expressions are the easiest way to provide a match pattern for the phone number that allows for the variability that was described.
Then follow Ezzaral suggestion if you are not familiar with none of the two recommendations
> And it worked. Although the file saved in my file system has extension .gif ??
The file name doesn't matter; its content does. I could have very well named it 'image.exe' and it still wouldn't have mattered. Maybe you overlooked the declaration
File saveFile = new File("SemiTransparentSquare.gif");
in which the file name is pretty much a constant.
Oops
You cannot because q is defined in C not in B
You cannot because it is declared private.
class A{
private int o,p;
protected B bClass;
protected C cClass;
public void method() {
System.out.println(cClass.o+";"+cClass.p);
//this is wrong: bClass.q
}
}
Also none of the above will work because you define o,p,q to be private. They need to be public or have public get methods that return them.
Also:
C cObj=new C(1,2,3);
cObj.o
cObj.p
cObj.q
If they were public
The r.toString() returns a String, it does not print. You need to call System.out.println() with argument r or r.toString()
Try saving it to a .png instead of a .gif, that should (hopefully) work.
I changed in the code this: ImageIO.write(cp.bi, "gif", saveFile);
into this: ImageIO.write(cp.bi, "png", saveFile);
or ImageIO.write(cp.bi, "jpeg", saveFile);
And it worked. Although the file saved in my file system has extension .gif ??
Well for starters it always helps when debugging to add a ex.printStackTrace() in the catch:
System.out.print ("Caught a null pointer exception - ");
System.out.println (ex.toString());
ex.printStackTrace();
After doing so, I noticed that none of your aruments is null. This is what I got:
java.lang.NullPointerException
at com.sun.imageio.plugins.common.PaletteBuilder.findPaletteEntry(PaletteBuilder.java:310)
at com.sun.imageio.plugins.common.PaletteBuilder.getIndexColorModel(PaletteBuilder.java:296)
at com.sun.imageio.plugins.common.PaletteBuilder.getIndexedImage(PaletteBuilder.java:145)
at com.sun.imageio.plugins.common.PaletteBuilder.createIndexedImage(PaletteBuilder.java:77)
at com.sun.imageio.plugins.gif.GIFImageWriter.write(GIFImageWriter.java:564)
at com.sun.imageio.plugins.gif.GIFImageWriter.write(GIFImageWriter.java:492)
at javax.imageio.ImageWriter.write(ImageWriter.java:598)
at javax.imageio.ImageIO.write(ImageIO.java:1479)
at javax.imageio.ImageIO.write(ImageIO.java:1521)at ColorForms.TopPanel.SaveImageToFile(TopPanel.java:87)
at ColorForms.TopPanel.actionPerformed(TopPanel.java:46)at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6041)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5806)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4413)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4243)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2440)
at java.awt.Component.dispatchEvent(Component.java:4243)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
I have never used these classes before so I am not sure how they need to be instantiated. I believe that maybe when the java methods are called
(from the stack trace:
at javax.imageio.ImageWriter.write(ImageWriter.java:598)
at javax.imageio.ImageIO.write(ImageIO.java:1479)
at javax.imageio.ImageIO.write(ImageIO.java:1521)
)
something that was not supposed to be is null when they use your arguments. I cannot say much so you would better wait for someone better to see this and figure it out
Sorry
Give me a minute to run the code
if (cp.bi == null)
What if cp is null ?
Try to print everything before you run anything:
File saveFile = new File("SemiTransparentSquare.gif");
System.out.println("saveFile: "+saveFile);
if (saveFile == null)
System.out.println ("saveFile is null");
System.out.println("cp: "+cp);
System.out.println("cp.bi: "+cp.bi);
if (cp.bi == null)
System.out.println ("cp.bi is null");
ImageIO.write(cp.bi, "gif", saveFile);
Inside the run method of the Thread class have a boolean method take value true when it finishes. That variable will be declared as an attribute of the class.
When you call the thread have a while loop checking that variable when it becomes true you will exit the loop
From what I see they are comma separated, so use StringTokenizer or String.split() to separate into its elements and check each one of them if they are phone numbers or not
When you have a degree as a parameter you don't know if it is in Celsius or in Fahrenheit. So you will use the scale to determine that and calculate the results accordingly.
So don't put any calculations in your get Methods.
When you set the degree (by Constructor or be a set Method), use the scale to see if it is Celsius or Fahrenheit and use the appropriate formula:
public void setDegrees(float degree)
{
if (scale=='C') {
celsiusTemp =degree;
fahrenheitTemp = celsiusTemp * 5/9-32;
} else {
fahrenheitTemp =degree;
celsiusTemp = fahrenheitTemp * 9/5+32;
}
}
This is wrong: 'F' = scale;
This will work: scale = 'F';
Meaning that your set methods should have arguments and will pass that argument to the class variable.
Use the instructions of the assignment to create the 4 constructors. Based on the input calculate the fahrenheitTemp, celsiusTemp like I did in the setDegree. If no input specified the assignment tells you what values to put.
And you declared the same Constructor twice which gave you an error:
public Degree(float degree)
{
temp = degree;
scale = 'C';
}
public Degree(float degree)
{
temp = degree;
scale = 'F';
And the other mistakes are because of missing and/or misplaced brackets
Can you provide an example of what the lines might look like?
You mean this? :
public class A {
public void method_A1 {
//calling method from B
newMethod_B1();
}
}
public class B extends A {
public void newMethod_B1() {
}
}
No it cannot be done. What I wrote above is wrong. Although however you can do this, but has nothing to do with inheritance:
public class A {
public void method_A1 {
B b1 = new B();
b1.newMethod_B1();
}
}
You can create and call any class from anywhere, but like I said it has nothing to do with one class extending the other
I observed that many programmers do not create separate functions.
They write the whole program in the main function.
What do you mean by saying: "many programmers". Because this is not the right way to do it. In any language. You must write small functions that are called in the main.
Most programmers that write large pieces of code in an efficient manner, write things in separate functions that are organized in different classes depending on their functionalities.
These functions are not necessarily used only once in main. They could be used by other different function more than once. So if you want to make a change, you only change the function. If you had most of the things in main then it would be very difficult to change or correct something.
Also you wouldn't have to repeat the same code all over again when you can only call one function
So the second approach is more preferable even in small programs
Actually I just thought of a VERY DUMP way to do it. It is not smart from programming terms.
I have already told you a way that might be done (running the appropriate DOS command using java) but if it is too difficult you can always try this:
for (int i=0;i<100;i++) {
System.out.println();
}
It doesn't do what you want but it will look like it
Hi Shubhang and welcome to DaniWeb :)
I am sorry, but I don't understand what you are asking. Can you please post some code to show us what you mean?
I think he is saying that when the application is running, in DOS for example, the window becomes too crowded with all the System.out.println messages. So he would like a way to "clear" the command window from the messages.
But I don't know if there is a way to do it purely with java. Of course there are DOS and UNIX commands that do that, so IF there isn't another way, you might want to try and calling those from your program. But this I think will get a little more complicated than that you actually wanted.
Of course if there is another way feel free to correct me.
public class A {
public void someMethod_A1() {
}
public void someMethod_A2() {
}
}
public class B extends A {
public void newMethod_B1() {
//BOTH are the same:
someMethod_A1(); //calls the method from super class
super.someMethod_A1(); //exactly the same as the above
}
// BUT if you also declare this:
public void someMethod_A2() {
}
//AND
public void newMethod_B2() {
this.someMethod_A2(); //calls the method from the class B (this class)
super.someMethod_A2(); //calls the method from the class A (super class)
}
}
I didn't quite understand what you are asking in your final post so I posted this example.
Let us know if you have any comments on it.
You cannot use what you declare in B inside A.
Only the other way around since it is B that extends A, so B can access what you declare in A
BufferedWriter writer = new BufferedWriter(new FileWriter("filename.txt"));
writer.write("This is a line");
writer.newLine();
writer.close();
Also: BufferedWriter
FileWriter
Write a separate method that takes a String as argument and removes all spaces:
Input: +0 69 34 55 --> Result: +0693455
And sometimes the first 0 is missing, and they appear as 868242602. and sometimes they might be in the form +353868242602.
Then another method that takes the above result and:
If it starts with: +353 remove it from the String
Then if 0 is missing added to the String.
Then check if the result is a number (long)
Then if it has length 10
Then if it starts with 08
Then if the 3rd character is [3,5,6,7,8]
And now you are sure that what you read is a number.
And use that ONE method to check every line in the file
Post part of the code where you get the exceptions, as well as the printStackTrace()