Jump to content

  • Log In with Google      Sign In   
  • Create Account


TextIO (Java)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
14 replies to this topic

#1 adderly.sj   Members   -  Reputation: 100

Like
0Likes
Like

Posted 01 May 2011 - 08:10 AM

I apologize if I'm posting this in the wrong section of this forum, but I've searched for "TextIO" and found nothing in "Beginners".

My question is (for anyone here that may be proficient with Java) - how do I go about using this? It's my understanding that I can compile TextIO.java in my main project directory to gain access to it, but Netbeans isn't allowing me to compile it for some reason. If I haven't given enough information here, just let me know and I'll see what I can do to fill you in on what I'm trying to accomplish. Help me help you help me. =)



Sponsor:

#2 boogyman19946   Members   -  Reputation: 1027

Like
0Likes
Like

Posted 01 May 2011 - 10:25 AM

Do you get any errors during compilation? Have you tried using the java.io package for what you need?
"If highly skilled generalists are rare, though, then highly skilled innovators are priceless." - ApochPiQ

My personal links :)
- Khan Academy - For all your math needs
- Java API Documentation - For all your Java info needs :D
- C++ Standard Library Reference - For some of your C++ needs ^.^

#3 adderly.sj   Members   -  Reputation: 100

Like
0Likes
Like

Posted 01 May 2011 - 10:42 AM

UPDATE: I noticed that Netbeans added "package <project.name>" and was able to test my code upon removing that, but not without errors regarding no package declaration.

I did get an error message during compilation of TestIO, but I don't recall what it was. I wasn't aware of the java.io package, but will keep that in mind for future use. Could you explain what that is? Bear with me. I'm just learning.



#4 boogyman19946   Members   -  Reputation: 1027

Like
1Likes
Like

Posted 01 May 2011 - 11:06 AM

Since Java 1.3 (I'm actually not sure of the exact version, but somewhere down the line), the Java standard requires that all your source files be in a package. You can still put your code in the Default Package but if you try to access it from a different package you won't be able to. Packages are simply folders (Oracle says they are "code groupings") where you put your code to have it organized and prevent things like naming conflicts etc. NetBeans creates a package named after the project by default and you should put your code there for now. If you want to use code from a different package, you can simply import it by using the import keyword. If anything, you could read through http://download.oracle.com/javase/tutorial/java/package/packages.html (Package tutorial from Oracle), but the wording might be a little bit tough.


If you could, try recompiling the TextIO and paste any error you get here.

As far as java.io goes, basically the Java language gives you a standard library with classes of various functionality organized into packages. java.io is a input/output package and contains classes that let you perform input and output operations. There are other useful packages like java.awt, java.swing, or java.util. The Java API documentation (so all the packages Java provides for you) are described here. At the top left you have all the packages, below that you have all the classes contained in that package, and in the center you have a fairly detailed description of the class selected and all of it's functions and members. When you get more into programming, you'll find yourself swimming in it, but for now you should just bookmark it and continue on with any learning material you have if any :D.
"If highly skilled generalists are rare, though, then highly skilled innovators are priceless." - ApochPiQ

My personal links :)
- Khan Academy - For all your math needs
- Java API Documentation - For all your Java info needs :D
- C++ Standard Library Reference - For some of your C++ needs ^.^

#5 adderly.sj   Members   -  Reputation: 100

Like
0Likes
Like

Posted 01 May 2011 - 11:42 AM

Since Java 1.3 (I'm actually not sure of the exact version, but somewhere down the line), the Java standard requires that all your source files be in a package. You can still put your code in the Default Package but if you try to access it from a different package you won't be able to. Packages are simply folders (Oracle says they are "code groupings") where you put your code to have it organized and prevent things like naming conflicts etc. NetBeans creates a package named after the project by default and you should put your code there for now. If you want to use code from a different package, you can simply import it by using the import keyword. If anything, you could read through http://download.orac...e/packages.html (Package tutorial from Oracle), but the wording might be a little bit tough.


If you could, try recompiling the TextIO and paste any error you get here.

As far as java.io goes, basically the Java language gives you a standard library with classes of various functionality organized into packages. java.io is a input/output package and contains classes that let you perform input and output operations. There are other useful packages like java.awt, java.swing, or java.util. The Java API documentation (so all the packages Java provides for you) are described here. At the top left you have all the packages, below that you have all the classes contained in that package, and in the center you have a fairly detailed description of the class selected and all of it's functions and members. When you get more into programming, you'll find yourself swimming in it, but for now you should just bookmark it and continue on with any learning material you have if any :D.


That makes much more sense. And I can't stress enough how much I appreciate you defining 'package' for me. I see it everywhere but I haven't really read a description of it lol. Of course, I haven't really looked for one, either.


As for the compilation error:

C:\<directory>\TextIO.java:536: unreachable statement
throw new RuntimeException("End-of-file on standard input.");;
1 error


In addition to the error, Netbeans wouldn't let me compile without me changing 'StringBuffer' to 'StringBuilder' on line 329 (I'll post the code if necessary)

So, are there any significant differences between the java.io and TextIO packages? Pros and cons over one or the other? And I'd already bookmarked the Java API documentation ;) courtesy of the brilliant eBook and tutorials I've been reading, but thank you! My overall intention with Java is developing smartphone apps and whatnot, depending on any other opportunities I find for the language. Is the Netbeans IDE a decent learning environment for that kind of development? Thanks again, for your help! It's much appreciated.



#6 boogyman19946   Members   -  Reputation: 1027

Like
1Likes
Like

Posted 02 May 2011 - 10:26 PM

First off, sorry for my delayed reply, but I was busy with a side project of my own and I got a little over occupied, forgetting anything outside of it.

To get to the point, NetBeans is definitely a good environment to go with. I use it all the time and so far it hasn't disappointed me. Java is also a good language to start with and has good general purpose use. The differences between java.io and TextIO is that TextIO is a wrapper and java.io is native to the language. Basically speaking, java.io is part of the actual language and TextIO is built on top of of it all. As an analogy, imagine a bicycle and a motorcycle. On a bike, you have to pedal manually to get the bike moving, which is equivalent to using the java.io package: you have to do more labor to achieve what you want. The TextIO package is equivalent to the engine on the motorcycle: with a twist of the handle (and messing with the gears, yadda yadda) you get the engine to act on the wheel automatically without bothering you too much.

Anyhow, the compiler says that the line in question will never be reached. I take it that this:


   private static void fillBuffer() {    // Wait for user to type a line and press return,
                                     	// and put the typed line into the buffer.
      StringBuffer b = new StringBuffer();
      out.flush();
      try {
     	int ch = in.read();
     	if (ch == '\n' && possibleLinefeedPending)
            ch = in.read();
     	possibleLinefeedPending = false;
     	while (ch != -1 && ch != '\n' && ch != '\r') {
         	b.append((char)ch);
         	ch = in.read();
     	}
     	possibleLinefeedPending = (ch == '\r');
     	if (ch == -1) {
            System.out.println("\n*** Found an end-of-file while trying to read from standard input!");
            System.out.println("*** Maybe your Java system doesn't implement standard input?");
            System.out.println("*** Program will be terminated.\n");
            throw new RuntimeException("End-of-file on standard input."); // line 536
     	}
      }
      catch (IOException e) {
     	System.out.println("Unexpected system error on input.");
     	System.out.println("Terminating program.");
     	System.exit(1);
      }
      buffer = b.toString();
      pos = 0;
   }

is the code in question. Hmm, I can't tell why the compiler seems to be complaining about it. Perhaps your code is different than the above?

EDIT: This class, while it does import java.io, doesn't really have much to do with file IO. It turns out that it deals with console IO. I didn't notice that because of an exceptional package: the java.lang package. It is included everywhere by default, so everything inside of that java.lang package can be used without importing it.
"If highly skilled generalists are rare, though, then highly skilled innovators are priceless." - ApochPiQ

My personal links :)
- Khan Academy - For all your math needs
- Java API Documentation - For all your Java info needs :D
- C++ Standard Library Reference - For some of your C++ needs ^.^

#7 adderly.sj   Members   -  Reputation: 100

Like
0Likes
Like

Posted 02 May 2011 - 10:57 PM

No worries! I completely understand. Your response in general is appreciated, delayed or not. You've been a major help.


That makes sense. So just to make sure I understand, both packages essentially do the same thing, but java.io requires more effort to get desired results because it's basically the barebones of input/output functions, where TextIO is a wrapper in conjunction with java.io?

And the compilation error was generated around this function:

private static void fillBuffer() {    // Wait for user to type a line and press return,
                     					// and put the typed line into the buffer.
      StringBuffer b = new StringBuffer();
      out.flush();
      try {
 		int ch = in.read();
 		if (ch == '\n' && possibleLinefeedPending)
            ch = in.read();
 		possibleLinefeedPending = false;
 		while (ch != -1 && ch != '\n' && ch != '\r') {
 			b.append((char)ch);
 			ch = in.read();
 		}
 		possibleLinefeedPending = (ch == '\r');
 		if (ch == -1) {
            System.out.println("\n*** Found an end-of-file while trying to read from standard input!");
            System.out.println("*** Maybe your Java system doesn't implement standard input?");
            System.out.println("*** Program will be terminated.\n");
         	throw new RuntimeException("End-of-file on standard input.");; // The compiler generated the error around this line.
 		}
      }
      catch (IOException e) {
 		System.out.println("Unexpected system error on input.");
 		System.out.println("Terminating program.");
 		System.exit(1);
      }
      buffer = b.toString();
      pos = 0;
   }

NOTE: I just realized that, for some unknown reason, the particular statement the compiler was complaining about was terminated twice. I'll remove one of the semicolons and try compiling again after posting this.



Netbeans wouldn't let me compile without changing 'StringBuffer' to 'StringBuilder' in:

private static String readRealString() {   // read chars from input following syntax of real numbers
   	StringBuffer s=new StringBuffer(50);
      char ch=lookChar();
      while (ch == ' ' || ch == '\n') {
          readChar();
          if (ch == '\n')
 			dumpString("? ",0);
          ch = lookChar();
      }
      if (ch == '-' || ch == '+') {
          s.append(readChar());
          ch = lookChar();
          while (ch == ' ') {
 			readChar();
 			ch = lookChar();
          }
      }
      while (ch >= '0' && ch <= '9') {
          s.append(readChar());
          ch = lookChar();
      }
      if (ch == '.') {
 		s.append(readChar());
 		ch = lookChar();
 		while (ch >= '0' && ch <= '9') {
 			s.append(readChar());
 			ch = lookChar();
 		}
      }
      if (ch == 'E' || ch == 'e') {
 		s.append(readChar());
 		ch = lookChar();
 		if (ch == '-' || ch == '+') {
 			s.append(readChar());
 			ch = lookChar();
 		}
 		while (ch >= '0' && ch <= '9') {
 			s.append(readChar());
 			ch = lookChar();
 		}
      }
      return s.toString();
   }

But the compiler only suggested changing the first 'StringBuffer'.

#8 boogyman19946   Members   -  Reputation: 1027

Like
2Likes
Like

Posted 03 May 2011 - 03:36 AM

Indeed, one of the uses of a wrapper is to make something complicated simple (or automated if you will). It's like putting buttons on an electronic device so that it's user can operate it without concerning him or herself with the underlying circuitry. The buttons still use the circuitry to perform their functions, but the user doesn't have to worry about it. There are other uses, but this one is relevant to the code at hand.

Hah, that function is exactly the same as the one I found :) The Java API states that StringBuffer and StringBuilder are actually the same classes, but StringBuffer is designed for programs that make use of threading. Threading is, simply put, multi-tasking, and is a fairly complicated and messy process: hard to implement even for the most seasonal programmers it seems (I haven't had the need for it yet). Technically, both StringBuffer and StringBuilder are legal, but StringBuilder is faster because it doesn't have to synchronize with other tasks.
"If highly skilled generalists are rare, though, then highly skilled innovators are priceless." - ApochPiQ

My personal links :)
- Khan Academy - For all your math needs
- Java API Documentation - For all your Java info needs :D
- C++ Standard Library Reference - For some of your C++ needs ^.^

#9 adderly.sj   Members   -  Reputation: 100

Like
0Likes
Like

Posted 03 May 2011 - 05:00 AM

So, are wrappers written by programmers and shared among the programming community? Or are they comparable to OEM parts for vehicles and whatnot?

Lol! It is! I didn't even notice that you had added the comment next to the line. I removed the unnecessary semicolon and lo and behold, it compiled beautifully.

Also, forgive me for asking so many questions, but since it was mentioned - threading is specifically for multi-core processors, right?



#10 boogyman19946   Members   -  Reputation: 1027

Like
1Likes
Like

Posted 03 May 2011 - 05:09 AM

Some wrappers can be shared and some can be indeed comparable to OEM parts. Wrappers have a whole array of useful functionality, they can also serve as a means to get "cross-platform" functionality (the write once, run everywhere type).

As for threading, I'm not sure exactly why or how it came to be, but my guess is that it came before multi-core processors. Whereas true multi-tasking programs can only be achieved on a multi-core processor, threading is actually implemented by the operating system and can be pulled off on a single core system. The latest OS that I'm sure could run more than one process was Windows 95 and that system I'm pretty sure came before dual core CPUs were invented. Don't worry about asking questions: it's a forum for beginners anyhow.
"If highly skilled generalists are rare, though, then highly skilled innovators are priceless." - ApochPiQ

My personal links :)
- Khan Academy - For all your math needs
- Java API Documentation - For all your Java info needs :D
- C++ Standard Library Reference - For some of your C++ needs ^.^

#11 adderly.sj   Members   -  Reputation: 100

Like
0Likes
Like

Posted 03 May 2011 - 05:31 AM

I honestly didn't expect to leave this forum knowing anything other than what I asked. I certainly didn't expect getting in-depth explanations and so I've happily voted up all of your posts that I found useful.

Hmm, perhaps threading came to be when DDR replaced SDR (or SDRAM)?




In any case, thank you for all of your help and explanations. It's inevitable that I'll have more questions to come, so would you mind if I added you as a friend here? I understand you have projects and other concerns so obviously, I'll seek answers elsewhere before resorting to asking you. But it's always helpful to network. Additionally, it's hard to find experienced people that are able to think on a beginner's level and answer questions in a comprehensible manner. Besides, as a newbie to the forums, I need friends :P.



#12 boogyman19946   Members   -  Reputation: 1027

Like
0Likes
Like

Posted 03 May 2011 - 02:26 PM

Well, when you ask the right questions and give the right answers you may learn more than provided for. Personally, I do a lot of my own research, and I've never really been much of a teacher, much less a question answerer. Thanks for the thumbs up though :)

Wikipedia claims that multithreading came to be some time around early 1990s. I usually skip over the history lessons unless they provide some kind of insight into some kind of inner workings.

And of course you're welcome. Always happy to help as I can. If you wish you can freely add me as a friend (I wasn't even aware there was such a function). Lastly, I must say that I am quite a noob myself, it just so happened that most of the concepts in question I knew a thing or two about. You haven't heard the pros talk just yet, they're the ones that really make you drop your jaws :D
"If highly skilled generalists are rare, though, then highly skilled innovators are priceless." - ApochPiQ

My personal links :)
- Khan Academy - For all your math needs
- Java API Documentation - For all your Java info needs :D
- C++ Standard Library Reference - For some of your C++ needs ^.^

#13 rip-off   Moderators   -  Reputation: 7641

Like
2Likes
Like

Posted 03 May 2011 - 04:49 PM

Whereas true multi-tasking programs can only be achieved on a multi-core processor...

It can also occur on a multi-processor system. Moving the multiple computing units onto a single core is just a recent specialisation of such technology.

The latest OS that I'm sure could run more than one process was Windows 95 and that system I'm pretty sure came before dual core CPUs were invented.

Multi-tasking operating systems and multi-processor systems predate Windows (and even DOS!), but mainly on "mainframes" and not often for consumer systems. Some basic research on the wikipedia suggests that an early, simple context switching computer was available in 1961. Multi-processor systems (not multi-core) were available in the middle of the 1960s.

#14 boogyman19946   Members   -  Reputation: 1027

Like
0Likes
Like

Posted 04 May 2011 - 01:13 PM

Oh thanks for that rip-off! Multi-processor systems completely skipped my mind O.o I've seen a motherboard with 4 CPU sockets before, but I forgot all about them.
"If highly skilled generalists are rare, though, then highly skilled innovators are priceless." - ApochPiQ

My personal links :)
- Khan Academy - For all your math needs
- Java API Documentation - For all your Java info needs :D
- C++ Standard Library Reference - For some of your C++ needs ^.^

#15 Adderly   Members   -  Reputation: 103

Like
0Likes
Like

Posted 04 May 2011 - 09:33 PM

Oh thanks for that rip-off! Multi-processor systems completely skipped my mind O.o I've seen a motherboard with 4 CPU sockets before, but I forgot all about them.

Oh, absolutely. I've seen motherboards with multiple slots for slot processors before it's socket successor was introduced, but I wasn't aware that their functionality was comparable to that of today's multi-core processors. Or perhaps I misunderstood rip-off?






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS