PatrickSharp 3 Newbie Poster

... i really know nothing about boolean arrays.

Thanks for editing your original post :) Having the CODE tags makes it easier to read and follow. Nobody will code your homework for you, but you will get hints!

Here is your hint:

26 letters in the alphabet
52 possibilities (Upper and Lower case)

Your array is built for recording that information.

PatrickSharp 3 Newbie Poster

Hi All,
I wish to source a Std Serial Comms Component,
hopefully freeware, for Delphi 6 or 7
Can someone pass a link or email me?
telkomgjl@telkomsa.net

Thanks!
Gerhard

http://tpapro.sourceforge.net/

PatrickSharp 3 Newbie Poster

Dear All,

Could you tell me what is wrong or missing for my coding?

Cheers,

...

Is there a specific problem that you are running into? The code works just fine with Lazarus and Delphi both.

PatrickSharp 3 Newbie Poster

...
So if i ran the code and entered "abc" then i would get an error. I would rather it just tell me that it the input was invalid and ask for the input again.

Since code isn't your thing.. here is some psuedocode:

set valid input false

while (not valid input) 
do
  prompt user for input value.
  get input value from user.
  if input value is what you are looking for 
    set valid input true
  else
    display error message
end

That is the logic that you are looking for. Try wrapping some code to it.

PatrickSharp 3 Newbie Poster

In addition to Jamesonh20's suggestion, you should wrap the input routine in a while loop.

The while loop would exit once you have retrieved a correct value from the user.

PatrickSharp 3 Newbie Poster

Thanks a lot for going over that.... i see where that would be a problem. I will keep my eyes open for real life applications and future assignments. (of course it was set to 100 because of the assignment needs).

lets say i did have the user set it for me.. would i then need to write the for loop with the "array".length and start my index at 0 to stop an out by one error?:

for(int i = 0; i<=sID.length;i++){   
      System.out.println("Enter Student ID: ");
      sID[i] = scan.nextInt();
      outputFile.println(sID[i]);
      }//end of for

Greatly appreciated
thanks
Zoxx98

Using the length of an array for looping is a good habit to get into. There are situations that you will run into where you simply do not know, or can't control the length of an array.

Looks to me like you may be able to understand and use another looping mechanism in your programs. It's called the For-each loop in Java :) It's pretty cool as well!

Consider the following:

for (int studentID : sID) {   
      System.out.println(studentID);
      }//end of for

and the equivalent basic structure

for (int i = 0; i<=sID.length;i++) {   
      System.out.println(sID[i]);
      }//end of for

The first example is so much more easier to read don't you think? It's great to use with arrays that are already populated with values.

BUT...

There are some limitations for it's use. It's read only, works only with single structures, you can not see other elements of the array …

PatrickSharp 3 Newbie Poster

Firstly, I would like to thank you for demarcating your loop and logic structures. Legacy and maintenance programmers will appreciate that effort.

The actual documentation that is provided within the code is very light. You will hear will hear as many viewpoints on this as there are people who program :).. There is something that java programmers use to help with this however. It's called Javadoc, and it will require some amount of work on your part, but it is one of the really good habits to get into. Take a look at this website :

http://java.sun.com/j2se/javadoc/writingdoccomments/

Besides making your program easier to understand by others, it helps when using some SCCS (Source Code Control Systems) setups. At the very least, it will help you. You may completely understand the code now, but in 3 weeks it could look completely foreign to you.

Secondly, the main function header typically includes a string parameter array. Your program does not use it and it is a contradiction to my next point, however; there are some IDE's that will not be able to handle it. Here is the example:

old

public static void main()throws IOException {

new

public static void main(String[] args)throws IOException {

lastly. I would like to point out a habit that is really good only if you use the code completely, but leaving out the implementation sucks big time. What I'm talking about is error handling.

Your function headers are …

PatrickSharp 3 Newbie Poster

yes thanks, that helps me understand it a little better. im still not really figuring out how to call the functions properly but i guess ill just mess with it a little more and see what i can do.

Here is a resource that you can look at for an example of how to create your implementations of your Clock class, and how to call the methods (functions) of it.

http://www.javacoffeebreak.com/java102/java102.html

It will most certainly help you to understand classes enough for your assignment. Read the entire document, and study the design of the class 'Account' structure. It has a similar type of structure to your clock Class, with some hints in it as to how you would deal with modifying your states (variables) in the class. It also contains examples of creating objects and how to call the methods of the instance.

PatrickSharp 3 Newbie Poster

i missed lecture today and this is what they did in class. im not sure how to complete this. hope you all can help me out.

My guess is that you missed the lecture because you were held over in Engrish?

This is your assignment:

Complete the Clock class. You have been supplied with a nice template for which (as mentioned above in another response) you need to supply the implementation code for the mutator and accessor methods. You only have 3 states to mess with, so it should not take a lot of effort.

In addition, you need to complete the ClockDemo class which will demonstrate the following:

1. Creating 2 different instances of the class Clock.
(create Clock object C1 with h:m:s = 0:0:0)
(create second Clock object C2 with h:m:s=12:35:59)

2. Adding the value of the second class to the first and printing both using the printTime() method.
(add C2 into C1)
(print C1)
(print C2)

3. Modifying the first instance values 3 different times, printing the result after each modification.
(increase clock C1 by 1 seconds)
(print C1)
(increase clock C1 by 100 minutes)
(print C1)
(increase clock C1 by 10 hours)
(print C1)

4. Just for kicks, calling the printTime() method of the second object one more time.
(print C2)

Hopefully this provides the clarity you were asking for.

PatrickSharp 3 Newbie Poster

the thing is that i must use Date for this assignment
if someone can give me an example please how to use Date and get a yyyy-mm-dd date format that would be great
thanks

Using the information readily available at the website I supplied for you:

Date currentDate = new Date();
         SimpleDateFormat amazinglySimpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
         String omgAFormattedDate = amazinglySimpleDateFormat.format(currentDate);
         System.out.println(omgAFormattedDate);
PatrickSharp 3 Newbie Poster

Have a go at this website:

http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html

There are other packages, such as Joda-Time that have a multitude of date handling things.

PatrickSharp 3 Newbie Poster

...
ok so i have this so far what im trying to do is when the user of the client types in the name say luke it sends the object to the server ive done that it receives the name but then i carnt use the methods i have above to then get the name object sent and compare it with the data in the file then send back the correct patient object

There are some elements to saving and loading of objects that you have not included. Do a Google search on "Java load save object".

Would it not be more efficient to send just the state (variable) portion of the object as a data stream instead?

BestJewSinceJC commented: Good help. +4
PatrickSharp 3 Newbie Poster

...Your response to my post is overboard and childish...

As was your first and second responses to me cupcake :)

.And you added nothing to this thread other than insults. I'll leave it at that.

True, I did forget to include the code. Good catch on that one. I'm sure all of your friend is impressed!

BestJewSinceJC commented: useless nuisance. -1
PatrickSharp 3 Newbie Poster

Unfortunately, I am still unable to get the program to run. I also removed the public declaration from the stack class. I also don't know how to change the '\n' to the end of the file. I do not know much terminology in Java.

in your source, change:

while ((ch = (char)br.read()) != '\n')

to

while ((ch = (char)br.read()) != (char)-1 )

Here is the program with the changes

import java.io.* ;
   
    class Stack {
      private int maxStack;
      private int emptyStack;
      private int top;
      private char[] items;
   
       public Stack(int size) {
         maxStack= size;
         emptyStack = -1;
         top = emptyStack;
         items = new char[maxStack];
      }
   
       public void push(char c) {
         items[++top] = c;
      }
   
       public char pop() {
         return items[top--];
      }
   
       public boolean full()  {
         return top + 1 == maxStack;
      }
   
       public boolean empty()  {
         return top == emptyStack;
      }
   }
    public class Stackmain {
   
       public Stackmain() {
      }
       public static void main(String[] args) throws IOException {
         Stack s = new Stack(10); // 10 chars
         char ch;
      
         FileInputStream fstream = new FileInputStream("test.txt");
      
         DataInputStream in = new DataInputStream(fstream);
         BufferedReader br = new BufferedReader(new InputStreamReader(in));
         String strLine;
      
         while ((ch = (char)br.read()) != (char)-1 )
            if (!s.full()) s.push(ch);
         while (!s.empty())
            System.out.print(s.pop());
         System.out.println();
      
      } 
    
   }

The program works fine...

PatrickSharp 3 Newbie Poster

^ What? I already told him why his file wasn't printing. Your reply makes no sense. Did you read the previous replies to this thread?

...Like I told you in my previous advice, you should exit your while loop at the end of the file, not at a newline. Had you listened to that advice, your program would be working right now...

I see no useful information in your post, only a lame attempt at a mini lecture. It must be very important for you to get that "solved" gold star, right?

My post, was to show that there is indeed output happening with his code with only a single modification. You of all people, should have been smart enough to pick up on that.

So, I was addressing his concern about no output and usage of his s.pop. My apologies for not being as smart as you think you are.

PatrickSharp 3 Newbie Poster

do i need to do s.pop() inside of the while loop and then print outside of it?

It appears to work. Prints out the first 10 characters of the file test.txt

I did have to change the stack class by removing the public declaration.

Here is the contents on my test.txt file:

this is a test data file that contains important stuff.

EDIT: it prints the first 10 characters of the first line backwards, which is what I assume you are trying to do.

PatrickSharp 3 Newbie Poster

Hello,

I am studying for SCJP 6 Exam, and came across this question which shows code as asks for the output

9.       switch(x) {
10.         case 8: s += "8 ";
11.         case 9: s += "9 ";
12.         case 10: { s+= "10 "; break; }
13.         default: s += "d ";
14.         case 13: s+= "13 ";
15.       }

step 6: case 9 appends 9 to string s

step 7: for loop, y is set to 1

Read up on how a switch statement works. Step 6 and 7 is your trouble spot.

In step 6 (case 9: ), s takes on the value of "9 ". The program then continues on and executes the code in the (case 10: ) portion of the switch statement.

Look very close at the switch statement usage on
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/switch.html

PatrickSharp 3 Newbie Poster

so when i type in to the server side luke it returns john and when i type in john it returns john it always returns the last object saved.

Take a look at your datafile, I'm sure you will find that the last patient object within it, belongs to john.

Nowhere in your code, are you creating separate instances of the Patient class! You are re-using a single instance of it.

PatrickSharp 3 Newbie Poster

Why is it that when I invoke the Derived constructor from the main method, I get a compilation error pointing to the Derived constructor of the Derived class. I never called the Base constructor of the Base class. Therefore why is the error happening?.

class Base 
{
     private Base() 
     {
          System.out.print("Base");
     }
}
public class Derived extends Base 
{
    public Derived() 
    {
         System.out.print("Derived");
    }

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

private constructors are used in classes when no objects (or descandents) are expected to be created from them.

You would only use a private constructor for classes that offer only static members to a caller.

PatrickSharp 3 Newbie Poster

...

public Object readPersons() throws IOException {
        Patient patient = null;
        try {
            ObjectInputStream in = new ObjectInputStream(new FileInputStream("PatientData.data"));
            for (int i = 0; i < 200; i++) {
                patient = (Patient) in.readObject();
            }
            in.close();

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(TestData.class.getName()).log(Level.SEVERE, null, ex);
        } catch (EOFException eof) {
        }
        return patient;

    }

but now the problem is what do i put where the write object is? and also how to loop round the file for the patients name :S

if ( ??? .equalsIgnoreCase(patient.getName())) {
                out.writeObject( ??? );
            }

More clarification is needed here.

Are you trying to load the entire patient database (200 records assumed) as objects? If so, you will need to make an array of the patient type, and load them individually.

Patient [] patient = Patient[200]

Then, on the loop iteration through the file, you would do something like:

for (int i = 0; i < 200; i++) {
      patient[i] = (Patient) in.readObject();
   }

A sample search might look like:

int foundIndex = 0;
  // Seach through persons array...
for (int i = 0;i<200;i++) {
   if (patient[i].getName().equalsIgnoreCase("luke")) {
      foundIndex = i; // assign found object
      break;
   }
}

etc...

Does this sound like what you are trying to accomplish?

Another option that I think you may be wanting appears that your function is returning a singular object (patient) ,loaded from the file of 200 records. You are not checking for a name during the read, so this my not be correct..

PatrickSharp 3 Newbie Poster

How about trying just replacing the spaces with :

URLString.replaceAll(" ","%20");

I'm personally OK with spaces in filenames, except if they live on a webserver for this very same reason :)

IF you are requesting the filename with a Get or FTP request, then replace the spaces with %20 .

PatrickSharp 3 Newbie Poster

Ah, if only a nickle I had for each urgent problem needing to be solved fast...

/** imports
*/
import java.net.URLDecoder;
import java.net.URLEncoder;

// ...

String addr = "http://www.blah.blah/image with spaces.gif";
		
String junk = URLEncoder.encode(addr,"UTF-8"); 
System.out.println(junk);
junk = URLDecoder.decode(addr,"UTF-8");
System.out.println(junk);

Your output is:

http%3A%2F%2Fwww.blah.blah%2Fimage+with+spaces.gif
[url]http://www.blah.blah/image[/url] with spaces.gif
PatrickSharp 3 Newbie Poster

Forget about stringTokenizer. Even SUN recommends against using it in favor of split. Here is (tested) working solution...

public class SplitTest{

	public static void main(String[] args) throws Exception {

	/** According to Sun's website at: 
		http://java.sun.com/j2se/1.4.2/docs/api/java/util/StringTokenizer.html
		
		"StringTokenizer is a legacy class that is retained for compatibility reasons 
		although its use is discouraged in new code. It is recommended that anyone 
		seeking this functionality use the split method of String or the 
		java.util.regex package instead."

      ===============
		
		So, even Sun (Oracle) recommends using the String.split() static method.
		
		Keep in mind that your token will be consumed by split,
		so if you are concerned, you will have to replace it. 
		If you are looking for special chars, like decimal points, make sure to escape them.
		
		Here is a working solution...
		*/
		
		String origToken = ".";
		String s = "I born on 21.02.1995. So, now my age is 15.";
		
		// Chop up the string based on the split token (in this case a special one!)
		String [] tokens = s.split("\\.\\ "); // Notice the padding here.. 		
		for (int i=0;i<tokens.length;i++) {
			// Since the token is consumed in split process, we must re-attach it
			// It's called the ternary operator..google it
			System.out.println((tokens[i].endsWith(origToken)) ? tokens[i] : tokens[i] + origToken);
		}
		
	}
}

-Patrick