• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

print out question

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, i have a question.

Let's say i define a final int constant like this:



In this example, i will print out two 1's. If i want to print out the text house instead of the number 1, how do i do this? I realize i can always make a method with a return string, but i'm sure there's a smarter way? if java considers house = 1, then how do i swap the 1 with house in print out?
 
Sheriff
Posts: 8545
615
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think there is direct way to do this, what you just showed us here.
But you could have a House object which should encapsulate relevant with the house information and provide accordingly getter methods to retrieve that information.
example:
 
Rancher
Posts: 1059
27
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tom,

Have you by chance programmed in FORTRAN? In FORTRAN you could say 1 = 2, then then if you had x = 1 + 1, then x would equal 4 and if you printed 1 you would actually get 2 printed. In some of todays scripting technologies you may have the ability to setup equivalencies where if you say house = 1, then the converse is true, but in Java what is happening is that a memory space is being set aside and a reference handle is given to it as "house", then the constant 1 is referenced by that handle. The constant 1 is completely unaffected, and to my knowledge there isn't any converse listing saying 1 is referenced by these handles, but there are listings that say these handles do reference these constants and variables.

Les


Tom Gibbins wrote:Hello, i have a question.

Let's say i define a final int constant like this:



In this example, i will print out two 1's. If i want to print out the text house instead of the number 1, how do i do this? I realize i can always make a method with a return string, but i'm sure there's a smarter way? if java considers house = 1, then how do i swap the 1 with house in print out?

 
Tom Gibbins
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Les Morgan wrote:Tom,

Have you by chance programmed in FORTRAN? In FORTRAN you could say 1 = 2, then then if you had x = 1 + 1, then x would equal 4 and if you printed 1 you would actually get 2 printed. In some of todays scripting technologies you may have the ability to setup equivalencies where if you say house = 1, then the converse is true, but in Java what is happening is that a memory space is being set aside and a reference handle is given to it as "house", then the constant 1 is referenced by that handle. The constant 1 is completely unaffected, and to my knowledge there isn't any converse listing saying 1 is referenced by these handles, but there are listings that say these handles do reference these constants and variables.

Les


Tom Gibbins wrote:Hello, i have a question.

Let's say i define a final int constant like this:



In this example, i will print out two 1's. If i want to print out the text house instead of the number 1, how do i do this? I realize i can always make a method with a return string, but i'm sure there's a smarter way? if java considers house = 1, then how do i swap the 1 with house in print out?



Hehe nope, java is my first language. But thanks for the clarification
 
Marshal
Posts: 76887
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can do the same in Forth
2 : 2 3 ; 2 + .
That way you can show that two and two make five.


The two 2s on their own put the value of 2 on the stack, which is added with the + operator and taken from the stack and displayed with the dot operator.
The colon takes you into compile mode and 2 is compiled to 3. The semicolon takes you back out of compile mode.
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can make house an enum:
enum MyItems { HOUSE, ... };
and print MyItems.HOUSE.name()
 
Is that a spider in your hair? Here, threaten it with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic