• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

A problem using charAt..........

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, this is sort of following form the thread I posted earlier today, but is a different type of problem.
I can read in an int variable form the keyboard, and convert to a String and check how many characters are in it. But now, how can I use charAt to test whether the first character is of value "1", "2", "3", etc........
I've done the following, but i'm getting the compile error ""Say.java": Error #: 361 : variable required, but value found at line 26, column 23" - i'm using JBuilder, if that's of any help.
The code is as follows:-

(Marilyn added code tags)
[ December 23, 2002: Message edited by: Marilyn de Queiroz ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Steve, you cannot use charAt(int) on an int.
An int is a integer primitive not a String object.
Do you really mean args.length? That's the number of arguments present on the command line. args[0].length() is the length of the first argument. E.g, if you do java Say 42 the args[0].length() is 2.
-Barry
Oh! the pleasures of Java-4a and the torments of Java-4b to come Keep at it Steve you'll get there, but read your java books a bit more.
BTW I am still being tortured by Java-4b myself, if that helps a little.
[ December 22, 2002: Message edited by: Barry Gaunt ]
 
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've declared "num" to be of the primitive type "int". As such, num doesn't have a charAt method to call. (Or any other methods, for that matter.)
The message I get from trying to compile your code with javac is "int cannot be deferenced" -- which is a lot more helpful than the compiler message you've reported. I count 25 lines in your program. Where's line 26? If your class is called Test, then why does the error message report a problem in "Say.java"? :roll:
Since these questions are very closely tied to a Cattle Drive assignment, maybe it's better to have these discussions in the Cattle Drive forum?
[ December 22, 2002: Message edited by: Michael Matola ]
 
Steve Jensen
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Barry Gaunt:
Steve, you cannot use charAt(int) on an int.
An int is a integer primitive not a String object.
Do you really mean args.length? That's the number of arguments present on the command line. args[0].length() is the length of the first argument. E.g, if you do java Say 42 the args[0].length() is 2.
-Barry
Oh! the pleasures of Java-4a and the torments of Java-4b to come Keep at it Steve you'll get there, but read your java books a bit more.
BTW I am still being tortured by Java-4b myself, if that helps a little.
[ December 22, 2002: Message edited by: Barry Gaunt ]


OK, but i thought that we had converted the int to a String type using
num = Integer.parseInt ( args[0] );
If so, I thought that charAt could then be used on this.

 
Michael Matola
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Steve Jensen:

OK, but i thought that we had converted the int to a String type using
num = Integer.parseInt ( args[0] );


Nope. Other way 'round. The above code converts the String into an int.
 
Steve Jensen
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Michael Matola:

Nope. Other way 'round. The above code converts the String into an int.


OK then, before i delve back to my books, just as a pointer, what chapters of a Java text book should I be looking at in particular, to solve the problems i'm facing??
 
Michael Matola
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just for future reference, there are (at least?) 3 ways of converting an integer primitive to a String in Java. See lines (A), (B), and (C) below.
 
Michael Matola
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Steve Jensen:

OK then, before i delve back to my books, just as a pointer, what chapters of a Java text book should I be looking at in particular, to solve the problems i'm facing??


For confirmation of how Integer.parseInt works (that it takes a String argument and returns an integer result) the best place is the Java API documentation. If you were passing it anything other than a String or trying to use its result in a context where an int wasn't acceptable, you would have received a compiler error.
 
Michael Matola
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
:roll:
Barry, your post may or may not get edited up, but for what it's worth, I think you're sketching out too much here.
Steve, this can be a tough assignment. Maybe set it aside for a few days and come back to it. Carefully reread the various hints in all three threads: you've been given a couple of gems already.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since it's Christmas and at the risk of getting chucked out of Cattle Drive,

Merry Christmas, Barry! And everyone else too. But now that you mention it, this probably should be in the Cattle Drive anyway, so I'm going to move it there and let the regulars there decide how to deal with you.
[ December 22, 2002: Message edited by: Jim Yingst ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Being totally chicken, I removed that perhaps over informative post. At least I didn't actually code it
Steve, old chap, it's not going to be painless...
my hands are tied.
-Barry
 
Michael Matola
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Barry Gaunt:
my hands are tied.


Well feel free to make all sorts of sly and cryptic comments about the assignment that make perfect sense to anyone who's completed it.
Steve, noone's intentionally leading you astray -- we just don't want to deprive you of the chance to figure out as much on your own as possible. Reread the hints you've been given and ask about each one "what's noteworthy about this information?" or "why did this poster bother mentioning that?"
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Problem is I have visions of Steve hitting his head against the wall in despair...
Tell me I'm wrong Steve.
-Barry
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Originally posted by Steve Jensen:
I can read in an int variable from the keyboard, and convert to a String and check how many characters are in it. But now, how can I use charAt to test whether the first character is of value "1", "2", "3", etc........


You are reading a String from the keyboard, always -- not just in the first three assignments. In the statement
public static void main( String[] args )
notice that the parameter in main() is an array of Strings named args.

Rereading the link from assignment 1 (Hundred) about "get the name from the command line" and the link from assignment 2 (EvenOrOdd) about "convert a string to an int" may be helpful.


/* This if-statement will see if you have entered more than two arguments on your command line.

for example:
java Test 22 10 15 5

It is not checking the length of the argument.

Jim Yingst addressed this in his response to your other thread, A problem determining the length of an int variable converted to a string...........
*/



/*
num is an int, right? You cannot call a method of the String class by using an int. Since args[0] is a String, you could possibly use
args[0].charAt( i )
*/



[ December 23, 2002: Message edited by: Marilyn de Queiroz ]
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK then, before i delve back to my books, just as a pointer, what chapters of a Java text book should I be looking at in particular, to solve the problems i'm facing??

It depends on which text book(s) you are using. Probably the chapter that covers the main() method would help. Also the chapter that covers the various types (such as int, double, String, etc).

If you are using Just Java 2, you have probably already read the recommended chapters
"Read chapters 1 and 4 in "Just Java 2"....
After the first couple of assignments, you will want to read chapters 5 and 7."Preparation for the Cattle Drive
 
bacon. tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic