File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Why can't I use TextIO.getInt() for my simple age getting program? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why can Watch "Why can New topic
Author

Why can't I use TextIO.getInt() for my simple age getting program?

James Allen A.
Greenhorn

Joined: Mar 10, 2013
Posts: 24
This is my first crack at manipulating code. I'm trying to use TextIO.java instead of Java.Util.Scanner and use an integer type variable instead of a string. Why can't I simply declare my variable as an integer and output it in the same format? Here is my code:

import java.util.Scanner;


public class Main {

public static void main(String[] args) {
TextIO.putln("Hello World!");

TextIO.putln("What is your age?");
int age = TextIO.getInt();
TextIO.getInt();

TextIO.put("Your age is ");
TextIO.putInt(age);

}


}
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Hi Allen

A very warm welcome to the ranch. Mistakenly you put the code inside the quotes instead of code tags. Anyways, this is the right one:



What is TextIO class by the way? Never heard of it. Which package is it in?


~ Mansukh
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10908
    
  12

As Mansukhdeep implied, there is no TextIO in the java API. That means you must have gotten it from somewhere else, and we don't know what that would be. Did you get it from a teacher? From a website?

also, saying "why can't i do XYZ" is kind of pointless. What happened when you tried it? Either the compiler or the JVM will tell you EXACTLY why you can't do it. you will have gotten some kind of error message. Post the full and complete text, and perhaps we can help interpret it for you.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
James Allen A.
Greenhorn

Joined: Mar 10, 2013
Posts: 24
Thanks for your responses. Sorry for the ambiguity in my question and ignorance to the forum's features.

I downloaded the TextIO class from here. So far as I understand, the TextIO class is primarily for input/output and is an alternative to System.in and System.out. The beginner java book I am reading (don't have the title on me) suggested that it has some advantages over Scanner.

The code I posted above is intended to get the user input (years old) and print out user's age. So long as I keep the variable a string my code runs fine, but when I try to make my 'age' variable an integer it fails to return my age to me after a carriage return during run-time.

I hope this reply is more sufficient for explaining my issue. Any help is much appreciated.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

There is no putInt(..) method in the file you provided. Just check. I presume that must be the line you are having problems with.(line no. 14). If not, please share the exception / error details. That way we can guide you better.
James Allen A.
Greenhorn

Joined: Mar 10, 2013
Posts: 24
That was precisely the line I was having a problem with. I am using Eclipse and missed the what should have been obvious reminder about the syntax error. Subsequently, I tried using TextIO.put() which states that an integer is a valid parameter but continued to get a "?" print out instead of the age when I ran it.

I google'd the TextIO.java class and looked at examples. In the examples I found, the author just used System.out.print for output and stuck to TextIO.java for getting user data. This was really my first attempt at problem solving in java and I hope it wasn't to painful for those of you that helped! So thanks and bear with my posts in the future!
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

This was really my first attempt at problem solving in java and I hope it wasn't to painful for those of you that helped! So thanks and bear with my posts in the future!


No No! Not at all buddy. Don't ever hesitate to ask anything. We are all here to learn and help. So don't be formal.

I am using Eclipse and missed the what should have been obvious reminder about the syntax error.


If these are the first few of your tryouts at writing Java code, as you said, then my suggestion would be get rid of the IDE for now. Use a simple text editor to write the code yourself. Starting from package statements to imports, defining classes, methods instance variables etc. The reason is IDEs like Eclipse assist you with many things. So, you will never fully understand what is really happening under the hood. Write the code yourself in the editor, compile it and run from the command prompt. Make mistakes and get your hands dirty. Then you can home in on the exceptions and errors. It will not only teach you Java coding as such, but many other things that are a must know for a novice like classpaths, jar file issues etc.. Which book are you referring to by the way?
surlac surlacovich
Ranch Hand

Joined: Mar 12, 2013
Posts: 296

Mansukhdeep Thind wrote:
I am using Eclipse and missed the what should have been obvious reminder about the syntax error.


If these are the first few of your tryouts at writing Java code, as you said, then my suggestion would be get rid of the IDE for now. Use a simple text editor to write the code yourself. Starting from package statements to imports, defining classes, methods instance variables etc. The reason is IDEs like Eclipse assist you with many things. So, you will never fully understand what is really happening under the hood. Write the code yourself in the editor, compile it and run from the command prompt. Make mistakes and get your hands dirty. Then you can home in on the exceptions and errors. It will not only teach you Java coding as such, but many other things that are a must know for a novice like classpaths, jar file issues etc.. Which book are you referring to by the way?


That's really a good suggestion, but it's applicable only for people with huge level of motivation (or rich experience with other programming languages), rest won't be able to get out of this "dirt" and abandon java at all. I personally started with Netbeans, and despite that IDE did 95% of work for me, there was a huge mass things that I need to learn first, besides classpaths, jar problems and starting jvm myself.
James Allen A.
Greenhorn

Joined: Mar 10, 2013
Posts: 24
Interesting feedback. I appreciate the suggestions from both of you. Quick history since I think it may be relevant. I'm 30 now and began tooling with HTML when I was 15 or so, so I have a bit of grasp on simple text editing. Also, I worked in a machine shop and got introduced to the fundamentals of G-code for CNC machines which seemed to share some similarities with Java (and probably many other scripting languages?). Aside from that, I'm pretty much a newbie to Java and this is my first real crack at any programming language besides HTML (if that falls under the category).

About IDEs, I definitely see the advantage of them but as I'm working from a netbook for the time being it may make sense to take my first steps using Notepad or something less memory dependent than Eclipse.

The book I'm reading has been adapted to Android App form but seems to match what I'm seeing here.

I like the writing. Seems straight forward without talking down to me. Get what I mean?

Any thoughts or recommendations on reading material? I'm mostly interested in gearing up to write an Android app eventually.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

I like the writing. Seems straight forward without talking down to me. Get what I mean?


You mean to say some authors talk down to people? Interesting viewpoint. I hope we don't fall in that category.
James Allen A.
Greenhorn

Joined: Mar 10, 2013
Posts: 24
Probably not the best expression. I just mean to say the writer doesn't dumb it down too much and belabor the point

I'd like to find an IDE that is more lightweight than Eclipse to run on my slow netbook. Any suggestions?
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

I'd like to find an IDE that is more lightweight than Eclipse to run on my slow netbook. Any suggestions?


Try either this or this one here. Let me know how each one fares.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Probably better to go back to the command line.
surlac surlacovich
Ranch Hand

Joined: Mar 12, 2013
Posts: 296

Campbell Ritchie wrote:Probably better to go back to the command line.

Hardcore. But if OP has such a rich experience as working at machine workshop, so he knows nuts and bolts of computers, I suspect he can prefer that way. Please put me right if I'm wrong.

@OP: try Sublime text editor, I like it.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
The problem with IDEs is that you have to put so much effort into learning the IDE, which would be better used for learning Java. Also you don’t learn the command‑line options well if you use an IDE.
surlac surlacovich
Ranch Hand

Joined: Mar 12, 2013
Posts: 296

Campbell Ritchie wrote:you have to put so much effort into learning the IDE, which would be better used for learning Java

You will have to learn to use IDE anyway. But I agree that if you plan to go deep into java it's better to start from fundamentals.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
By the time you are writing large enough applications to need an IDE, you will know the Java. Then you will have brain‑space enough to learn the IDE as well.
James Allen A.
Greenhorn

Joined: Mar 10, 2013
Posts: 24
I checked out both DrJava and JEdit and I'm going to give DrJava a try for a little while. I like the sound of the command-line purist approach, and I grew up on MS-DOS so I'm familiar with command-line environment, but I tend to be a bit disorganized and so I like that an IDE can help with that.
surlac surlacovich
Ranch Hand

Joined: Mar 12, 2013
Posts: 296

Jayeemsuh Allen wrote:I checked out both DrJava and JEdit and I'm going to give DrJava a try for a little while.

That's all "noname" IDEs to me. I've never used any of these before and didn't even hear any suggestions to start using it.
If you plan to start with IDE, it would be wiser to invest your time in learning popular and proven IDE.
James Allen A.
Greenhorn

Joined: Mar 10, 2013
Posts: 24
That's all "noname" IDEs to me. I've never used any of these before and didn't even hear any suggestions to start using it.
If you plan to start with IDE, it would be wiser to invest your time in learning popular and proven IDE.


Noted! As I mentioned though, the hardware on my netbook is pretty meager so I need something pretty lightweight. I'm using DrJava for now and it seems to be doing the trick for compiling and running simple input/calculate/output type programs. That's mostly all I'll be doing for the time being. Maybe I'll upgrade my PC and IDE once I get some more experience.

On a somewhat unrelated note, I'm looking to read up on conventions for style and specifically how to form good habits with indentation. I'd like to start these habits early and have them stick. Any suggestions on where I can learn this sort of thing?

Here is some code that I'm looking to practice on:

surlac surlacovich
Ranch Hand

Joined: Mar 12, 2013
Posts: 296

I've got a lot of questions about your code.
Do you really need these uninitialized vars? Is it really needs to be double instead of float, int instead of short (byte)? Can you opt out of TextIO custom class, to make folks from the ranch understand the code?
Personally I like to stick to standard APIs to show the code (otherwise dependent I need to share dependent classes).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
surlac surlacovich wrote: . . . Is it really needs to be double instead of float, int instead of short (byte)? . . .
That is not at all a good suggestion; floating‑point arithmetic is done with doubles as a default and integer arithmetic with ints. In the days when memory was expensive, it might have been worth being economical with it, but that has not been the case for, probably, twenty years.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
surlac surlacovich wrote: . . . Can you opt out of TextIO custom class, to make folks from the ranch understand the code?
Personally I like to stick to standard APIs to show the code (otherwise dependent I need to share dependent classes).
TextIO appears simply to be a utility class which writes things to the command line and reads from it. Its methods have simple names, so we can easily work out what they mean.

Agree it would be better to write your own utility class for text input and simply use System.out and System.err for output, but OP specifically said he wanted to use TextIO.
James Allen A.
Greenhorn

Joined: Mar 10, 2013
Posts: 24
Agree it would be better to write your own utility class for text input and simply use System.out and System.err for output


Sadly, I don't even know what a utility class is but I'll look into it. I did take a quick look at Scanner and how it is used for simple input/output. I used it successfully, but I am drawn to the simplicity of TextIO for the simple needs I have now.

That is not at all a good suggestion; floating‑point arithmetic is done with doubles as a default and integer arithmetic with ints. In the days when memory was expensive, it might have been worth being economical with it, but that has not been the case for, probably, twenty years.


Interesting! I simply used type double and int because they were used for variables with similar purposes in examples that I saw.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
They are used in examples so often because they are the default types in Java.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why can't I use TextIO.getInt() for my simple age getting program?
 
Similar Threads
User Input
Laundrette
Doubt in this()
Infinite loop
Immutable Wrapper Class