JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You may not want to use that code for a number of reasons including:
Copying someone else's code for your homework is cheating
It corrupts the data by discarding the newline delimiters
It's a memory hog if the files are large (uses double the file size)
It does horribly inefficient string concatenations
It returns the file size in bytes, which may not be the number of characters, depending on the default encoding, and includes newline delimiters.

The original code is a far better starting place, once all its silly source errors are fixed (capitalisation of "string" or "Out", missing return type etc). It's hard to understand how you can get a stack overflow error with code that can't be compiled.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Have a look at the Oracle tutorials, starting here: http://docs.oracle.com/javase/tutorial/essential/io/streams.html

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Builder_1:
DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules

Post what you have got so far and someone will help you with any detailed questions you may have.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Please don't give up. Programming isn't easy, but it is logical, and you will get there if you work at it in a logical organised way.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Is that XML correct?
shouldn't

<Account AccountNumber="1234567890">

be

<Account>
   <AccountNumber="1234567890">

given how you are parsing it?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I could fix your code. But if you handed that in you would be cheating and get an automatic fail. (Remember that your instructor may read this site as well!)
I'm happy to try to help you do this yourself, but that's all.

Try what I said - indent the code properly and follow the template I gave you. That should help you get all your loops in the right place.

I have to go soon, but hopfully someone else will be able to step in if you need more guidance.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The message is pretty self-explanatory. It probably refers to the degree symbols on lines 63 and 75 (there may be others as well) because there is no degree symbol in your default US-ASCII character set.
The simplest/safest fix is to remove those symbols - they are just part of the display and have no effect on the program logic.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes. Your instructor has given you good advice.
It's not easy to get nested loops right at first. The simplest and most useful thing is to ensure your indentation is always right, so you can see what's in which loop. If I was your instructor I would refuse to waste my time lookng at code that wasn't correctly indented, because it's so hard to understand.

It should look something like like this:

while loop for name or stop{
   ...
   while loop for pay rate {
      ...
   }
   while loop for hours {
      ...
   }
   calculate and print results
} 
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You get the name before you enter the loop, so that's only executed once, at the start of the program. Your main two options are:
1. repeat lines 21/22 just before the end of the loop, so you ask for "name or stop" before starting the next pass of the loop
2. use a different loop structure, eg

while (true) {
   prompt for "name or stop"
   if (input is "stop") break;
   ask for pay etc...

ps: You ask the user to input "Stop", but test for "stop". There's a version of equals that ignores case - see the String API doc for details.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I'm sorry, but you have posted hundreds of lines of undocumented code with no apparent overall structure, multiple main methods, multiple declarations of variables with the same name, code that refers to variables that are not declared in that class, and I have absolutely no idea what the flow of control is to ensure everything is initialised properly.

All I can suggest is that you trace the value of out up to the point where you try to use it and it is null. Presumably you have a plan for how it gets initialised, and you need to find out where the execution deviates from that plan.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Unless there's a duplicate declaration of out somewhere, that implies that the actionPerformed has been called before run gets to line 61.
Maybe it's hanging on the new Socket or maybe it's thrown an IOException (because I can't see any code to handle that Exception)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That's not enough of the code to see why a variable should still be null, but for starters try printing out and txtmsg just before that line to see which is null.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

By "screen" do you mean the console window, as in System.out? If so, be aware that the console output device may be to a printer or a sequential file, in which case "clear the screen" makes no sense, and attempting to execute a "cls" as a system command will have unpredicatable reuslts in such cases (and if you use the wrong command for the OS that happens to be running at the time). Also, as far as I can find out, there is no cls functionality in standard C++ either; it's just that some implementations of C++ have it as a non-standard addition.

Your safest option is just to send a load of newlines to the console device.

It's not worth spending any real effort on this - console I/O is used in the early stages of teaching Java, but you won't ever use it again except as a logging device for errors etc.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
 System.out.print("Type name: ");
 String name = reader.readLine();
 System.out.println("Hello " + name);

System.in is the InputStream associated with the console. You create a BufferedReader using that stream, then you can call its readLine() method to get the user's input.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Is it wrong? In what way? Do you have an error message? Does it goive the wrong results? You must tell us everything you know if you want help.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Exactly the same reason, exactly the same fix. If you want a \ in your Regex then you have to code two \ chars in the Java String literal, because \ is used in Java String Literals for special characters like \n (newline)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Can you explain something for me please?
I thought the transition table for a DFA looks somnething like

table[current state][next token] = next state

so conceptually I'd expect to see something like

// state n is inside a a comment, n+1 is normal parsing
table[*]['{'] = n;    // { takes you to state n from anywhere
table[n]['}'] = n+1;  // } takes you out
table[n][*]   = n;    // anything else leaves you in state n

yet you have a nx3 array, and it contains values like -1

I never did compiler design at college, so maybe I'm just missing the point, but if someone can explain this for me I'd be grateful.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
PrintStream ob=new PrintStream(System.out);
ob.print("Hello World");

Good point stultuske

System.out is a PrintStream, so new PrintStream(System.out) just creates another PrintStream object which is just the same oas the original. SO there's no point doing that.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Because you want to make many Strings in a normal program, Java has special syntax to make it easy, eg.

String name = "Joe";

without that you would have to say something like

char[] letters = {'J', 'o', 'e'};
String name = new String(letters);

So it is in the language to make your life easy. That's all.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

n=input.nextInt();
works because input is a Scanner

n=System.in.nextInt();
does not work because System.in is NOT a Scanner

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Line 8 is wrong. There is no nextInt method in System.in
You must create a Scanner using System.in (just like your previous post)
Then you can use the Scanner's nextInt method to read an int value that you can assign to rno

ps: If you have the answer for your previous post then please mark it "solved"

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

No need for the counter - it's just another thing that could go wrong. You can use the length of the bld String and see if that is 3. Don't forget to reset it back to "" after a wrong entry

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Like mKorbel says:
You can't do this with a 1..3 loop. Each time a button is pressed append it to the combination String. When that results in a String of length 3, check to see if it's right or wrong.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That looks 100% better - you're really on the right track now.
It's especially good that you stopped there and added a line to allow you to test what you did so far. Real programmers test early and test often.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Just have a method that re-enables the components, and call that as part of whever listener is handling the close of the other window.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

in is a public constant in the System class. It refers to an InputStream object that gets the input from the console.

Here's part of the API doc for the System class:

public static final InputStream in

The "standard" input stream. This stream is already open and ready to supply input data. Typically this stream corresponds to keyboard input or another input source specified by the host environment or user.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

"getting an error with it"? That's not helpful. Exactly what error where?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Everything to do with Swing (eg painting the screen, running actionPerformed methods) happens on a single thread - the "Event Dispatch Thread", or "EDT", or "Swing thread". Your actionPerformed is run on the EDT, which means that once your actionPerformed method starts NOTHING else will happen in Swing - no button clicks will be processed, no screen updates, nothing. You just sit in your loop burning 100% of the CPU time waiting for a count of 3 that will never happen because no button can be selected.
In your actionPerformed you just update a variable to record which buttons were pressed, and if there are now 3 buttons then unlock or error. No loops!

Your idea ofappending into a String is OK. In your actionPerformed you can query the ActionEvent to see which button was clicked, then get the text of that button to append. No need for all those horrible repeated if tests.

Your code would be shorted and simpler if you follow the Hint and use an array of 10 buttons rather than 10 separate variables.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Use / as a delimiter for your Scanner

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You declare String test on line 9 of a. When you declare a variable in Java its scope if from the preceeding '{' to the following '}'. In this case that test variable exists just from line 8 to line 13, after which it is discarded. You presumably have another String test declared somewhere else, and that's the one the b uses.

ps If you are no longer waiting for replies to your previous topic, please mark it "solved". Thank you.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You can have 2 classes in one file - only one can be public but you can have as many others as you want. If you want to create one GUI but have that work with multiple connections/sessions then 2 classes is the way to go. If you really want to do it one class that's up to you, but the only advice I can give is the advice I already gave ;)

Just saying you get an exception is no help at all. Every Exception comes with a complete description of what the problem was and exactly where it happened. Instead of just throwing the exception and hoping for the best, catch it and call its printStackTrace() method.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Well, it's not strictly necessary, but my opinion is that in the end it will be the easiest way. It's also good practice to separate GUI from the underlying functionality, so the things you will learn doing it will be valuable.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

A good start would be to separate the GUI code from the newtworking code. Refactor it into two classes so you can create and swap a new instance of the network class without creating a new GUI class instance.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You didn't post the declaration of testgui, but apparently it's not a subclass of Component. The ".this" is even more mysterious.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
if(program.get(i).contains(remp))

the contains method looks for remp anywhere in the line.

if(program.get(i).startsWith(remp))

the startsWith method checks if the line starts with remp. Is that what you wanted?
(NB the parameter for startsWith is a REGEX, but if it's just letters and numbers then the result is the same).

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I don't understand that question.
You asked for the first matching token, and if you present the data in a different order, a different one will be the first. So what's the problem? Maybe there's another part of the specification that you haven't shared with us?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You can break out of the loop as soon as you have found a match, ie:

for (int i ...) {
  if (prog....) {
     display token
     break;
}
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Unless there's some reason why to has to be an ArrayList, by declaring the variable as a List you allow the possibility of (eg) changing the implementation to a LinkedList sometime later, secure in the knowledge that you will not break any existing code.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Most situations call for an ordinary class.
Nested classes are good for event listeners, and for little data structures used inside a class, eg Linked List classes may have an inner Entry class that encapsulates the data and the link. Just ask the two questions:
Is this class needed outside the main class?
Does this class need access to the main class's variables?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

An nested class has access to the variables and methods of its enclosing class, and it can be private to the enclosing class. That's a completely different kind of sharing and encapsulation compared to a separate class. Which you chose depends on what you need.
Consider a Listener class in Swing. You want it to have access to the components of the GUI, but you don't want anyone lese messing with it.

Oracle has a good tutorial on this subject right here

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The variable sizes are handled by the x,y,w,h variables.
The variable number of rectangles is a good reason to use a List rather than an array to hold them. If you haven't covered Lists yet, then just allocate a "big enough" array - eg [10000] and have a variable that contains the number of rectangles actually stored in the array.

So: store all your rectangle data in array(s) or whatever, then in paintComponent loop through them drawing them one at a time.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Arrays and a loop are a sensible way to do this. Don't give up so quickly!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Forget rmic and stubs. They have been obsolete for years. For current tutorial info and a working sample this is a good source of info.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

To hold the info for multiple rectangles you need multiple sets of coordinates (x1, y1, length, depth). You could use arrays to hold them, or some kind of List or Set from the Java API.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Looks like a problem in the roll() method - did you change that to use a local array?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

In rollConsec() you can change thewhile (true) to a do loop with a limited (eg 20) number of iterations

Doogledude123 commented: First time I had to use a do loop :) +3
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Can't see anything wrong there. Try printing the tempRolls array as well as the highestRolls, and look at the FIRST few lines of output. YOu should be able to see there where it's going wrong with just a few data points

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You read the first line (17) then use its tokens to add columns (20). You then read the rest of the lines (22) and use those to add rows (25). That's why the first line of the file goes to the column names.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

In this case, you you add one to both? Or ignore it and not add anything?

That's up the programs eventual user! Personally I think adding 1 to both makes more sense

Either way, would you have to actually store each value, then check after getting them all?

Yes. One pass thru the array to find the highest count, then a second pass to increment all the values that have that count.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

A couple of problems:

you don't re-initialise rolls on each call of roll(). It really should be a local variable in the method.

for(int i = 1; i < 6; i++) //Get highest index
just checks 5 values of i, not 6

(this is a bit subtle... ) You forgot about the case where two values have the same counts (which will be pretty common over 100 rolls). You find the first (lowest) value that has the highest count. If two values have the same count, then second one is never seen as "highest".