• Post Reply Bookmark Topic Watch Topic
  • New Topic

Chars and Ints  RSS feed

 
Bod McLeon
Ranch Hand
Posts: 78
IntelliJ IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to print a string and chars and ints like this:



So on this a char is being generated then a number and this happens three times.
Is there a way to print the char then the number then the char then the number.. ect
Why because this is going to be a number plate.
So for example the first char is C then the number is 43, char is G, number is, 3 char is D, number is 4.
I then want it to print C43G3D4
The problem is you can't print ints and chars together so is there actually a way to do it or not?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't have to create a new Random object each time you want to generate a random character or integer. Just create a Random object once and then call nextInt() on it every time. In fact - you're already doing that, because each time you're using the Random object that the variable r refers to. You're not using all the other ones that you are creating in lines 8, 11, 14, 17 and 20. Remove those lines.

You could use a StringBuilder to create the complete number plate string. Call append() on it each time you add a random letter or number. For example:
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
way to print the char then the number then the char then the number.. ect

Where is the print statement you are having problems with? 

Are you asking about how to build a String that you want to print from those different data types?
There are several ways:
use a StringBuilder
Put Strings between the int and char values to force the compiler to do the conversions for you. For example: 123 + " " + 456
 
Bod McLeon
Ranch Hand
Posts: 78
IntelliJ IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i don't know where my text was but I said Thanks! It really shortened my code!
 
Bod McLeon
Ranch Hand
Posts: 78
IntelliJ IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
way to print the char then the number then the char then the number.. ect

Where is the print statement you are having problems with? 

Are you asking about how to build a String that you want to print from those different data types?
There are several ways:
use a StringBuilder
Put Strings between the int and char values to force the compiler to do the conversions for you. For example: 123 + " " + 456


At the moment i don't have a print statement but I want it to pop up on a JOptionPane which I'm half way through doing i just don't know how to print chars and ints together.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forget about the option pane for the time being, and then forget all the misinformation you have been given about chars. You have been told that chars are letters, but they aren't. They are numbers, as you can see if you go through the Java® Language Specification (=JLS) (beware: the JLS can be difficult to read). You see that a char is a number in the range 0…65535 which can be interpreted as a Unicode character, but if you look up the + operator in the JLS, you find that the chars are used as numbers. Various possible solutions:-
  • 1: Put the char into a StringBuilder which is intended for making Strings and that will convert it to a String. Already suggested by somebody. Probably the best solution.
  • 2: Convert each character or number to a String equivalent with the String#valueOf method.
  • 3: Start with an empty String and add the char to it. String s = ""; s += c;
  • Once you have managed to turn part of the output from a number to a String, everything else will be turned to Strings with the + operator.

    Once you have got your numberplates working, you can then consider the option pane problem. I have had two problems with option panes in the past:-
  • 1: They may need instructions about where to appear, so you may need to pass null as the first method argument if you don't have a GUI.
  • 2: They can appear in the centre of the screen, but behind something. If you can't find your option pane, minimise everything else and it may come out of hiding.
  •  
    Bod McLeon
    Ranch Hand
    Posts: 78
    IntelliJ IDE Java Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks guys!
     
    Campbell Ritchie
    Marshal
    Posts: 56529
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That's a pleasure
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!