bibiki 18 Posting Whiz

hey javanoob101,

you have one "monster" class. since you want more than one monster showing on your screen, you instantiate multiple instances of monster class. so, there is one single class, but multiple instances of it.

now, when you do the instantiation, you store all instances on an array. and, inside your class, you could have some boolean variable that is initially true, but turns false once a bullet hits an instance of it. as a result, your machine knows when to print the actual instance of monster and when not.

as I myself am more of a beginner that an experienced programmer, I think I'd share my understanding of solutions offered as beginners are more likely to understand one another than experts :P

bibiki 18 Posting Whiz

with the code you have posted here, I do not know how come you get null printed out. you do not have any System.out.println() or anything in your code. I assume you have some other code.

Anyways, it is a norm to help people in Daniweb but not to their work. However, what I am saying should be very simple to implement:
1. no method can be defined inside the main method. you are doing this wrong building inigtializeArray() inside main.
2.once you correct that, you call initializeArray() inside your main method.(no modification required on your initializeArray method's code)
3. you write a loop to loop through the arrays whose values you want printed on your screen (Surname, Firstname, DoB, Dept).

it is easy to do this if you try to.

bibiki 18 Posting Whiz

you're line 56 is messed up, that's likely why you get a complaint for line 11 as it refers to the method letterSearch(), which contains the line 56. check that line and correct as needed.

bibiki 18 Posting Whiz

with a for loop inserted inside your main method I got the following result:

Brown James 123456 24/3/85 Marketing
Jenkins Susan 234567 11/12/80 Sales
Turner Jane 345678 5/7/72 Sales
Cavendish Paul 456789 22/9/76 Accounts
Williams James 567890 19/1/81 Marketing
Ford Karen 678901 15/8/79 Sales

Other than the for loop inserted, I also called first the method initialiseArray() - if you do not call this method first, you ought to get null for the values inside your arrays. I made MAX_STAFF match the actual number of staff you 'construct' in your initialiseArray() method. And, for that code you offered, the variable NumStaff remains unreferenced, thus unnecessary. Hope this helps.

bibiki 18 Posting Whiz

I think when you instantiate your instance of FileWriter you need to do so in appendable mode. to do that, you write as follows:
new FileWriter(PassengerInfo.txt", true);

let me know if that works.

bibiki 18 Posting Whiz

pmark, James said you need ANOTHER loop. not ONE loop. and he is right. you are reading one single line of your input file on line 23, and then iterating through that line with your loop, and that's it. if you want to read each line, then do as James suggested.

bibiki 18 Posting Whiz

you are welcome jsefraijeen,
if your problem is solved, mark this thread solved then lest someone reacts to this post four years from now like i see happening in other posts.

bibiki 18 Posting Whiz

well, you are basically all done. except that your formula for calculating hypotenuse is wrong. and, inside your main method you do not need to declare multiple instances of Hypotenuse objects. one would suffice.

p.s. what you are basically doing is finding the square root of 3*4 in the first case, which of course returns a number near 3.5

bibiki 18 Posting Whiz

from what I just read online, there is no scope resolution operator in java. google it, and you'll find your answer.

bibiki 18 Posting Whiz

String s = "k" and char c = 'k' are different. you either need to tursn String s into a char or char c into a String before comparing. use proper methods for comparing though. hope that helps!

bibiki 18 Posting Whiz

are you trying to have the same method counting both consonants and vowels? this method you provided does not return the number of vowels. it does count the number of vowels, but it does not return it.

bibiki 18 Posting Whiz

seems like there is some trim() method calling going on somewhere under the hood. there are ways to insert a new line inside a string like using "\n". there might be similar for inserting space. I know there is for inserting a tab. consider that instead of just adding an empty string like this " " to your strings. do let us know if that works!

bibiki 18 Posting Whiz

James, I'm taking my chance to teach you something this time. although last time you deleted the entire contents of a file of mine with that code you sent me. anyways :P

I'll copy paste KIparsky's message:
"To test whether you understand the code after reading James' explanation, there's a very simple change you can make to this which will eliminate about half of the work this program has to do. The change is very simple, it involves one arithmetic operation inserted in the right place.
Can you spot it?"

what I am saying is that you can actually eliminate 3/4 of the work that the following code does:

public class Break{

      public static void main(String[] args){

      int i,j;

      System.out.println("Prime numbers between 1 to 50 : ");

      for (i = 1;i < 50;i++ ){

      for (j = 2;j < i;j++ ){

      if(i % j == 0)

      {

      break;

      }

      }

      if(i == j)

      {

      System.out.print(" " + i);

      }

      }

      }

      }

see what I'm saying this time James?

bibiki 18 Posting Whiz

yes pooja.shinde. James' explanation is correct, but you can make your code more efficient as Kiparsky said. try as an exercise to make your program more efficient. if you make the program more efficient, you will be sure you understand the logic. try first finding the prime numbers between 1 and 50 by hand, using your algorithm, and youll see for sure what we mean.

bibiki 18 Posting Whiz

write anything as an answer to your ii question. do your best, and I am sure someone will help you. it's a norm here to only help those that show effort. you haven't shown any. sorry bud!

bibiki 18 Posting Whiz

kiparsky, if you do the same arithmetic to both loops, you can reduce the work by this program has to do by 3/4.

bibiki 18 Posting Whiz

thank you James. I just tried your code. I think the while loop is diverging or something. but no worries, I'll figure my way around both, your code as well as that of neversleeping. take care!

bibiki 18 Posting Whiz

NeverSleepin, I don't use NetBeans. Anyways, as far as a functioning app goes, I think I can have one. I was just sticking my nose here to see your alternative. thanks for your replies, as well. I think you could and should mark your thread as solved since you solved your problem.

James, your level of expertise is obviously way to higher than mine. While you do understand yourself with a great comfort, I am having trouble to :P
Anyways, I shall find me a solution for this particular way of doing the task.

regards to you all.

bibiki 18 Posting Whiz

I just recently had to do a project at school manipulating files. And, I'd use for this project the classes and methods I used for that other project, and I know they would work perfectly fine. the classes are:
FileWriter y = new FileWriter("TextOutput.txt");
FileReader x = new FileReader("aFileInputIsTakenFrom");
BufferedReader input = new BufferedReader(x);
PrintWriter outfile = new PrintWriter(y);
out of curiosity, I wanted to understand these other classes Neversleepin has used, but I can't make any sense out of all this. I pulled these two lines:
File outputFile = new File("TextOutput.txt");

FileOutputStream fos = new FileOutputStream(outputFile);
out of the for loop (I also pulled out of the for loop the fos.close() method). however, my while loop now diverges, and the content inside TextOutput.txt remains void...

bibiki 18 Posting Whiz

how do you stop at half time? mine is not working :(

bibiki 18 Posting Whiz

I put a system.out statement inside the while loop in lines 30-32, and found out that fis.read() returns numerical values. I expected it to return text. however, the numbers are not input in the textOutput file either. I do not know what's going on... could it be .write() method is not what we need or what? James?

bibiki 18 Posting Whiz

what do you mean e.printStackTrace is useful? did that solve your problem? if yes, how?

bibiki 18 Posting Whiz

hehe, thanks James
you sleep well!

bibiki 18 Posting Whiz

James, I know what you mean.
I missed his declaration of int c variable, so I jumped into conclusion there. I am myself learning, and sticking my nose here to learn more than to teach. Anyways, I don't think I gave an answer.. it was just a suggestion. I am waiting to see what turns to be the solution.

bibiki 18 Posting Whiz

i removed the bracket on line 13 and put it immediately after line 50, and the error was removed. however, the content on TextOutput remains void.

bibiki 18 Posting Whiz

line 29 of your code contains an assignment operator (=), while I believe you intended to have a comparison one (==) check whether that helps. plus, lines 12 and 13 contain brackets, they are either unnecessary or the bracket on line 13 should have been somewhere else.

bibiki 18 Posting Whiz

I am marking this thread as solved, although no solution has been found. I guess, for anyone encountering the same problem, a solution may be found in extended ascii or something.

bibiki 18 Posting Whiz

{ 123
| 124
} 125
~ 126
127
? 128
? 129
? 130
? 131 and so on :)
?
?
?
?
?
?
?
?

bibiki 18 Posting Whiz

well, even for a character corresponding to a decimal value of 130, the result is the same?

bibiki 18 Posting Whiz

hey there,
I have the following code:

import java.io.*;

   public class Karakters{
      public static void main(String[] args) throws IOException{
      FileWriter in = new FileWriter("karakters.txt");
      PrintWriter outfile = new PrintWriter(in);
         char x = 'A';
      
         for(int i = 0; i < 300; i++){
            System.out.println((i + 1) + " " + (char)(x + i));
            outfile.println((char)(x + i));
         }
      outfile.close();
      }
   
   }

This class creates a txt file named karakters.txt, and puts 300 consecutive characters, starting at 'A', in the file, one character each line.

The following class is supposed to generate binary representations of each character using Java's Integer.toBinaryString(character) method. however, this is not working properly. for characters whose ascii values range between 127 and 160 (inclusive), the same value is returned, and it equals 111111 (decimal 63).

import java.io.*;

   public class Aski{
      public static void main(String[] args) throws IOException{
      FileReader x = new FileReader("karakters.txt");
		BufferedReader outfile = new BufferedReader(x);
		int i = 0;
		while(outfile.ready()){
		i++;
		String s = outfile.readLine();
		System.out.println(i + " " + s.charAt(0) + " " + (Integer.toBinaryString(s.charAt(0))));
		}
      }
   }

there are other ranges, like 256 - 365 for which the same occurs. I expect that a character generated as follows:
char x = (char)(270);
would return a binary equivalent of 270 after passed to Integer.toBinaryString() method.

please help! thank you.

bibiki 18 Posting Whiz

thank you jimaat7. that worked perfectly fine.

bibiki 18 Posting Whiz

I am working on this exercise:

Write an application that asks its user to type a complete sentence on one line.
The application then displays in the console window the words in the sentence,
one word per line, less any punctuation.

I have the following code:

import javax.swing.*;
   import java.util.*;

   public class Tokens{
   
      public static void main(String[] args){
      
         String input = JOptionPane.showInputDialog("Give a complete sentence");
      
         boolean processing = true;
         StringTokenizer x = new StringTokenizer(input, "., ?!" );
         while(processing){
         
            System.out.println(x.nextToken());
            if(input == ""){
               processing = false;
            }

         }

      }
   
   }

the while-loop is run one time too many, and as a result I get a noSuchElementException error. I know that my variable input remains the same as I give it initially, so I know my 'if(input == "")' statement always returns false. I need to know if there is any ready to use method to know that all tokens have been generated that I can use instead of my if statement, or should I just build me one?

bibiki 18 Posting Whiz

kramerd, I think that code you just gave me worked fine. It filled a white rectangle on the paintComponent and displayed a button. I assume this will also alow me to use a repaint() inside paintComponent and work properly. thank you very much.
I would also like to add that seems like the order of the methods you wrote is of importance. I switched the order and it failed to work. I am marking this thread solved! thanks again!
update: it is not necessarily the order that has any role. instead, removal of setLayout(new FlowLayout()) seems to have been the solution.

bibiki 18 Posting Whiz

my idea is to draw a rectangle that shall act as a car and move downwards. then, I want to draw an oval that acts as a hurdle and moves from right to left (that is towards the car). this all creates the impression that we have a car traveling along some road. I am trying to build me a game which ends once the car hits any hurdle or the ground. the car shall only move up and down(I want a button to make the car move upwards). i know how to do every thing except that I don't know how to add the button inside my window. I can't seem to make both the button and paintComponent show. a button alone won't do me anygood!

bibiki 18 Posting Whiz

pardon me, it does display buttons but it treats paintComponent method as a component of the same order as buttons displaying it next to the button in a 10by10 pixel square instead of say filling a full window rect.

please take a look at this code and tell me what am I doing wrong. it displays the button but not the paintComponent component.

import javax.swing.*;
   import java.awt.*;

   public class Nc extends JPanel {
   
      public Nc(){
		JFrame y = new JFrame();
         y.getContentPane().add(this);
         y.setLayout(new FlowLayout());
         JButton button = new JButton("OK");
         y.add(button);
         y.setSize(800, 1000);
         y.setVisible(true);
      
      }
   
   public void paintComponent(Graphics g){
	g.setColor(Color.white);
	g.fillRect(0, 0, 800, 1000);
	
	}
      public static void main(String[] args){
         new Nc();
      }
   }
bibiki 18 Posting Whiz

I think you're right kramred, but JPanel won't help in displaying buttons. I want to display both buttons and a paintComponent. how do I go about doing this? anyone?

bibiki 18 Posting Whiz

alright, thank you all for taking the effort to answer. however, I am not understand what's going on. the following code is the best I could do based on your recommendations:

import javax.swing.*;
import java.awt.*;

public class Ok1 extends JPanel{

public Ok1(){
JFrame c = new JFrame();
c.getContentPane().add(this);
c.getContentPane();
c.setLayout(new FlowLayout());
JButton button = new JButton("Gagi");
c.add(button);
c.setSize(500, 500);
c.setTitle("Ok1");
c.setVisible(true);
}

public void paintComponent(Graphics g){
g.setColor(Color.red);
g.fillRect(0, 0, 500, 500);
}

public static void main(String[] args){
new Ok1();
}

}

I know I have c.getContentPane() and c.getContentPane().add(this) on the same class and that that might cause any problems, but I tried other variations, removing each one, andleaving the other... to no avail. I can't seem to be able to produce a red background using paint and put on top of it a button. (I know I can use c.setBackground(Color.red) to reach that effect, but I am curious to do it this way. Reason: there are myltiple panes in Java, I want to see how I manipulate different level panes in the same class, and I want to see where the book's mistake is - remember that I tookd the initial code from a book.)

thank you for your efforts.

bibiki 18 Posting Whiz

I have the following code as taken from the book I read from:

import java.awt.*;
   import javax.swing.*;
/** Frame1 is a frame with a label and a button */
   public class Frame1 extends JFrame
   { /** Constructor Frame1 creates a frame with a label and button */
      public Frame1()
      { JLabel label = new JLabel("Press This:");
         JButton button = new JButton("OK");
         Container c = getContentPane();
         c.setLayout(new FlowLayout());
         c.add(button);
         c.add(label);
         setTitle("Example1");
         setSize(300, 260);
         setVisible(true);
      }
   	
   	
      public void paint(Graphics g)
      { g.setColor(Color.red);
         g.fillRect(0, 0, 100, 100);
      }
      public static void main(String[] args)
      { new Frame1(); }
   }

I expect it to show a red area in the shape of a box on the top left corner of the frame, and the label and button next to it. However, what happens is this: it either shows everything except the red area, or it shows nothing but the frame and it's borders, leaving my jGRASP seeable through the frame area.

I had the same exact problem working with JPanel (this time we're working with JFrame). What solved the problem is puting the getContentPane() in the same position it is now relative to other methods inside the constructor method. However, that's not helping me this time.

Anyone knows what's going on?

P.S. The problem only started after I added to the code the method paint.

bibiki 18 Posting Whiz

it's all fine I suppose, you just need to give the method some parameters! you declared the variables in the header of your multadd method... whenever you write something like int x, or double some, you are declaring a parameter... hope this helps

bibiki 18 Posting Whiz

I have the following code:

import  java.util.*;

public class Final{

private double guess = 1;

public Final(double x){

root(x);
}

public double root(double b){
double rez = -1;
if(Math.abs(guess*guess - b) < 0.001){
rez = guess;
}
else{
guess = (2/guess + guess)/2;
rez = root(b);
}
return rez;
}

public double rooting(double d){

while(Math.abs(guess*guess - d) > 0.001){
System.out.println(guess);
guess = (2/guess + guess)/2;
}
return guess;
}


public static void main(String[] args){

System.out.println(new Final(2));

}

in other words, the two methods do the same job: they find the square root of a positive integer using the Newton's method. And when I call the methods inside the main method using the following code:

Final x  = new Final();
x.root(2);
x.rooting(2);

they both work fine. However, when calling the methods inside the constructor, using such code as:

System.out.println(new Final(2));

what I get in the screen is: Final@9cab16.

Finally, I am aware that this is perhaps out of bounds of a practical question, but I would like to know what is going on.

Anybody with an answer is very much thanked!

bibiki 18 Posting Whiz

that's funny man... :) I still think I will just forget about it until I learn more basic stuff first.

bibiki 18 Posting Whiz

well, it is not solved.. we still don't know what the problem was. but, I think I will mark it as solved anyways since I have the impression that hardly anyone will be able to explain it.

bibiki 18 Posting Whiz

well, now that i tried the same exact file, it worked just fine... with no modification made to it. I don't get it. could be that encoding used to save the file resulted in the Chinese. I tried saving a working file in different encoding, but I can't generate the Chinese producing version now. In any encoding other than ANSI, the script outputs nothing now. Anyways, thanks for trying.

bibiki 18 Posting Whiz

Can someone please explain why is the following code outputting some Chinese instead of breaking down the string passed in the function into an array.

<?php
// define URL
$url = "http://www.somewebsite.com:80/community/columns/trog/article.php?id=79&page=2";
// parse URL into associative array
$data = parse_url($url);
// print URL components
foreach ($data as $k => $v){
echo "$k: $v \n";
}
?>

thanks in advance!

bibiki 18 Posting Whiz

@NormR1:

I put a println() inside both methods, paintComponent, and inside Vizatuesi. surprizingly enough, something is resting the value of size to 500 again... and now that I looked back to my code, I see that BestJewSinceOJ was right. I have two different variables, named the same...

I just altered my Vizatuesi method as follows:

public void Vizatuesi(int size1, Graphics g){ g.drawRect(0, 0, size1, size1);
size = (int)(size1*0.8); }

and everything went fine. Thanks a whole lot to both of you.

bibiki 18 Posting Whiz

well, size is actually not declard in paintCompononet nor in Vizatuesi. variable size is a field (private int size) variable declared outside any method that I wanted to update using the Vizatuesi method, inside a while loop inside paintComponent. this is my complete class (the one that does not work):

import javax.swing.*;
import java.awt.*;

public class Some extends JPanel{

private int size = 500;

public static void main(String[] args){

new Some();
}

public Some(){
//RepaintManager.currentManager(this).setDoubleBufferingEnabled(false);
JFrame x = new JFrame();
x.getContentPane().add(this);
x.setSize(500, 500);
x.setTitle("Katroret");
x.setVisible(true);
}

public void paintComponent(Graphics g){
g.setColor(Color.white);
g.fillRect(0, 0, 500, 500);
g.setColor(Color.black);

while(size>0){
Vizatuesi(size, g);
}

}


public void Vizatuesi(int size, Graphics g){

g.drawRect(0, 0, size, size);
size = (int)(size*0.8);

}

}

please note that the code above compiles well, but it does not show the squares drawn on a white background.

bibiki 18 Posting Whiz

hey there,

I have two pieces of code that I expected would behave the same. However, what I noticed is that one of them works fine, the other does not at all. I need help from someone experienced to explain what's going on under the hood. the code that does not work at all is the following:

public void paintComponent(Graphics g){
g.setColor(Color.white);
g.fillRect(0, 0, 500, 500);
g.setColor(Color.black);

while(size>0){
Vizatuesi(size, g);
//size = (int)(size*0.8);
}

}


public void Vizatuesi(int size, Graphics g){
//while(size>0){
g.drawRect(0, 0, size, size);
size = (int)(size*0.8);
//}

and the code that does work, is the following:

public void paintComponent(Graphics g){
g.setColor(Color.white);
g.fillRect(0, 0, 500, 500);
g.setColor(Color.black);

while(size>0){
Vizatuesi(size, g);
size = (int)(size*0.8);
}

}


public void Vizatuesi(int size, Graphics g){
g.drawRect(0, 0, size, size);
}

to facilitate your analysis, note that my variable size, in the code that works is being reduced in the paintComponent method, whereas it is being reduced inside the helper method Vizatuesi() in the code that does not work.

any insight is much appreciated

bibiki 18 Posting Whiz

NormR1,
this is emberrasing... break happened to be what was missing. thanks a whole lot.

bibiki 18 Posting Whiz

hey there,

I have the following code:

String[][] candidates = new String[4][3];
      
         for(int i = 0; i!=4;  i++){
            for(int j = 0; j!=3; j++){
               switch (j){
                  case 0:
                     {candidates[i][j] = JOptionPane.showInputDialog("Give name of candidate " + i);}
                  case 1:
                     {candidates[i][j] = JOptionPane.showInputDialog("Give address of candidate " + i);}
                  case 2:
                     {candidates[i][j] = JOptionPane.showInputDialog("Give age of candidate" + i);}
               }
            }
         }

I expect it to build a two dimensional array of type string. The aim is to simulate a database that holds the name, address, and age of each of four candidates competeing in an election.

I thought the switch statement would work properly, but what I get is that the application first asks me for a candidates name, then for his address, then for his age, again for his address, again for his age, and again for his age, and only after this does it prompt me for the second' candidates info. and when it begins asking me for the second's candidate info, it again asks me twice for his/her address and three times for his/her age.

numerically, I expect to be prompted 12 times, but what I get is I'm being prompted 24 times.

I tried to build the same using an if statement for each case and it worked perfectly well.

I would greatly apprectiate it if someone told me what's going on? Why isn't switch statement working as I expected it?

thanks in advance!

bibiki 18 Posting Whiz

I have only recently used CCleaner and never used any other such software. it worked fine the two times I used it. however, you seem to be more experienced and I'm sure you'd know better. Knowing that people have difficulties to know what to believe and what not to believe online, I decided to add credibiilty to the advice you gave to Jessica!

kind regards!