Relating int's to string
how would i relate an int to a string. so for example, if i were playing a game where i have a row of 6 stars
then i prompt the user for a number to remove stars(i can do this)
say he says 4
how would i make it so then i could change that
to
**
It's an extremely important skill to learn to read the API and become familiar with the tools you will use to program Java. Java has an extensive set of documentation that you can even download for your convenience. These "javadocs" are indexed and categorized so you can quickly look up any class or method. Take the time to consult this resource whenever you have a question - you'll find they typically contain very detailed descriptions and possibly some code examples.
Java� API Specifications
Java� 1.5 JDK Javadocs
Similar Messages
-
How can I convert an int to a string?
Hi
How can I convert an int to a string?
/ad87geaoHere is some the code:
public class GUI
extends Applet {
public GUI() {
lastValue = 5;
String temp = Integer.toString(lastValue);
System.out.println(temp);
showText(temp);
private void showText(final String text) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
tArea2.setText(text + "\n");
} -
Warning [deprecation] setStatus(int,javax.lang.String)
warning [deprecation] setStatus(int,javax.lang.String) in HttpServletResponseWrapper
can I find a jee.jar to remplace j2ee.jar to take the resolution of this problemThe resolution of this problem isn't to fiddle about with .jar files, but to change the code to call the method now recommended instead of the deprecated method. Or just ignore the warning - the code will still work.
BTW surely it's java.lang.String? -
How to convert an int to a String
Hi, just wanting to know is anyone could help me with convertin a primitive int into a String, currently I have
String mod = Integer.valueOf(n);
but with this i keep getting an error which says, - cannot resolve symbol - method valueOf(int)
Can anyone please help me. thanks.it should be
String mod = Integer.toString(n); -
How do I convert an int to a String?
How do I convert an int to a String?
You can also use any of these methods if you need to get more complicated:
Integer.toString(int i)
Integer.toBinaryString(int i)
Integer.toHexString(int i)
Integer.toOctalString(int i)
Integer.toString(int i, int radix) -
i want to know how to convert an int to a string .
I have tried toString() but it says can't dereference an int.
any ideas ????
thanksWhat I mean by the object being null is, say for example, you have the following method:
public String combine(Object o1, Object o2){
return o1.toString() + o2.toString();
}This method will throw a NullPointerException if either or both o1 and/or o2 are null. If you use it like so:
public String combine(Object o1, Object o2){
return String.valueOf(o1) + String.valueOf(o2);
}This will always work. (I know someone out there would say that this would return a String like "nullnull" if both are null, and so forth and so on, but hey, you get my drift.) I'm also not saying the you can't not check if either o1 or o2 is null before proceeding, so the following also works:
public String combine(Object o1, Object o2){
String s = null; // I'm using this instead of StringBuffer for
// simplicity's sake so don't get this wrong
if (o1 != null){
s = o1.toString();
if (o2 != null){
s += o2.toString();
return s;
}As you can see, there's no right or wrong way in programming as long as you achieve the result. The only thing that would matter is how clean your code is, how efficient your code is, and how maintainable your code is. -
How to convert an int array to string array?
Hi,
Can anyone please help me with this question?
int number={4,6,45,3,2,77};
I like to convert this into a list and sort
List mylist = Arrays.asList(number);
Collections.sort(mylist);
But first I need to convert the int array to String array.
Please advise. Thanks.If you want to convert your int array to a String array, you have no choice but doing a conversion element by element in a for loop.
However, if the sort method doesn't behave as desired, you can use the one with 2 parameters, the second parameter being a comparator class.
Check the javadoc for more information : http://java.sun.com/j2se/1.3/docs/api/java/util/Collections.html
/Stephane -
Hi everybody,
ive got a class that creates a new int. i want the name of the int to be a parameter that i can send into the class. so for example:
public testObject(String intName)
then in my main I could say:
testObject grader = new testObject(gradeOne);
and the testObject class would make a new int called gradeOne.
does anybody know how i could accomplish this?Well you can hava a java.util.Map that maps Strings (for the names) to Integers (for the values).
You could encapsulate that map within another class if you feel that helps the design, and provide a method that expresses the relationship pretty easily, e.g.,:
public class NamedInts {
private Map mapping = new HashMap();
public void addNumber(String name, int value) {
mapping.put(name, Integer.valueOf(value));
// other methods, presumably, as well
}Is that what you meant? -
Hi,
when i try to do the following its an error
int totalmarks;
String Totalmarks;
Totalmarks = totalmarks.toString();
could any one tell me how to do it correctly
Thank youA lot of alternatives, but you can't do new String(int..).
1. Integer.toString(totalmarks)
2. String.valueOf(totalmarks) will call Integer.toString(totalmarks)
3. ""+totalmarks will use the StringBuffer and append "" and totalmarks which again will call String.valueOf(totalmarks) which again will call Integer.toString(totalmarks) -
How to parse if input by user is a int or a string.
EDIT: First issue has been fixed, now i'm trying to figure out how to make the program look at the user input. There where the user should input a number (E.G. What is you're first guess line, etc). I want the program to reconize when the input is NOT a number and to tell the user that the input is wrong, to enter a number between 1 and a 100. I don't mean to write an if statement that will tell the program number > or < than this number. I want it just to give a message out ONLY when the input is NOT a number but words symbols etc, whenever they are NOT integers. I'm smashing my head trying to figure out this one ;_; please help. Here is the updated code so far.
import java.util.Scanner;
import java.util.Random;
public class Assignment3b
public static void main(String[]arg)
// Defining a new Scanner object and a new Random object
Scanner kb = new Scanner(System.in);
Random random = new Random();
// Defining replay variables
char replay = 0;
String input;
// Main game loop
do
// Generate random number
int randNumber = random.nextInt();
int maxvalue = 100;
randNumber = random.nextInt(maxvalue) + 1;
// Introduce the user to the game and ask for an initial guess
System.out.println();
System.out.println("Let's play a game.");
System.out.println("I'll pick a number between 1 and 100, and you try to guess it.");
System.out.print("\nWhat is you're first guess? ");
int number = kb.nextInt();
// Begin the "For Loop" which will ask the user to try again 9 additional times for a guess
// depending on the user's input.
for(int tries = 1;tries <= 10; ++tries)
if (tries == 10)
System.out.println();
System.out.println("Sorry, you didn't guess the number in 10 guesses.");
System.out.println("You lose, my number was " + randNumber);
System.out.println();
// Consume the remaining newline
kb.nextLine();
// Ask user to play again
System.out.print("Would you like to play again (y/n)? ");
input = kb.nextLine();
replay = input.charAt(0);
// Condition if user chooses to end the game at this point
if (replay == 'N' || replay == 'n')
System.out.println("\nThank you for using my guessing game program.");
System.out.println("Have a nice day.");
System.out.println();
System.exit(0);
// If the user chose a number too low to the one generated randomly, display this.
else if (number < randNumber)
System.out.print("That's too low, guess again: ");
number = kb.nextInt();
// If the user chose a number too high to the one generated randomly, display this instead.
else if (number > randNumber)
System.out.print("That's too high, guess again: ");
number = kb.nextInt();
// If the user chose a number that matches the one generated randomly, the program will take the following actions
else if (number == randNumber)
// Consume remaining newline
kb.nextLine();
// If user has guessed the number, display this and ask to play again.
System.out.println("\nYou got it in " + tries + " guesses! My number was " + randNumber);
System.out.print("Would you like to play again (y/n)? ");
input = kb.nextLine();
replay = input.charAt(0);
// Condition if the user chooses to end the program here
if (replay == 'N' || replay == 'n')
System.out.println("\nThank you for using my guessing game program.");
System.out.println("Have a nice day.");
System.out.println();
System.exit(0);
break;
// Condition that will cause the game to restart indefinitly until the user chooses to end the game.
} while (replay == 'Y' || replay == 'y');
Thanks a lot in advance :)
Edited by: 805148 on Oct 25, 2010 6:49 PMThanks i'll be looking into this. By the way, I do seem to get the grasp around the programming ambiance that it is favored to hint and help as to make people think for themselves furthermore. I am certainly not reluctant to think, but I have been spending many hours on all these small details, there's only so much I can do when I have been in computer science for 7 weeks learning basic java programming. I can check my book check the net, it's not about being lazy I can think all I want, but if i don't know certain codes or methods, simply because I didn't learn them and am unable to find them via my book or the net because I can vaguely type righteously what exactly I am searching for, I really can't do much more but ask for someones assistance which in return I do expect to be more concise rather than a vague hint to help me push the wheel further. For some I may understand, but I ask questions really only when I have juiced my brain out. I like figuring things out on my own but you can't figure everything on you're own.
Regardless thanks for the suggestion i'll be looking into it now that I have something a bit more concise to work with. -
How to substract an int from a String?
Hi,
Here is my problem,
I have to substract 4 integers out of a single string (in a single input using the JOptionPane.showInputDialog)
these 4 integers are seberated by spaces (any number of spaces)
I have to sign each number to an int variable
also I have to multiply these 4 integers with doubles to get a final double variable result
I need urgent help please
(hint: it's about string manuplation)
ThanksHi,
Here is my problem,
I have to substract 4 integers out of a single string
(in a single input using the
JOptionPane.showInputDialog)
these 4 integers are seberated by spaces (any number
r of spaces)
I have to sign each number to an int variable
also I have to multiply these 4 integers with doubles
to get a final double variable result I have no idea what you're asking. Can you clarify? Maybe provide some sample input and what the output will be.
I need urgent help please I promise you, nobody here cares about your urgency, and mentioning it is guaranteed NOT to get you help any faster. If it has any effect at all, it will be the opposite--some people might decide to delay or skip answering your question simply out of irritation at you mentioning your urgency.
(hint: it's about string manuplation)Yeah. We got that, thanks. -
I have to get the user to input a name and five int's (scores for a test) using a scanner
so it would look like this "Casey 98 96 95 84 92"
how would I pick the integers out of that?This is what the assingment says
Repeatedly (i.e. in a loop) process the input file, placing the results into the output file in a formatted way. In particular:
Read the student name and five exam scores for that student from the input file. You may assume that each line of the file starts with a String representing the student's (last) name, followed by whitespace followed by the five exam scores, each separated by whitespace. Your program does not have to handle the case where there are fewer that six items in the line or the case where the information in the line is not in the order specified.
Does that change anything? -
Hi,
I am curious if there is an easy way to extract an int value from a String.
For example, "Rb23" to get 23. The length of the part in front of the integer is variable.
Thanksthis seems to work:
public int extract(String s) {
String result = "";
char c[] = s.toCharArray();
int j = c.length-1;
if (!Character.isDigit(c[j])) {
return 0;
do {
result+=c[j--];
} while (j > 0 && Character.isDigit(c[j]));
return Integer.parseInt(new StringBuffer(result).reverse().toString());
}it will return 0 if no numbers are found at the end of the string -
Int from a string??
OK, before my contacts dry out & I go blind staring at this, please point out the friging error here.
I have the same baasic code for other numeric conversions but this one does not compile, why does javac not like "Int"?
TestNumericTypes.java:122: cannot resolve symbol
symbol : variable Int
location: class biz.TestNumericTypes
anInt = Int.valueOf(s).intValue();
^
1 error
public static boolean TestanInt (String s, boolean popup)
try
anInt = Int.valueOf(s).intValue();
System.out.println("TestanInt: anInt = " + anInt);
j = true;
catch (NumberFormatException nfe)
if (popup = true) // display on screen
alertuser(" (int)");
j = false;
} // end-catch
return j;
} //close method
//-----------------------------------thanks,
-- HSC --The class is called "Integer". You can look it up in
the API docs:
http://java.sun.com/j2se/1.4.1/docs/api/java/lang/packa
e-summary.html
"anInt = Integer.parseInt(s)" is considered better
style and more efficient than "anInt =
Integer.valueOf(s).intValue()", by the way.thanks for the conversion tip, and the correct class name - sometimes I need another to see silly mistakes like this. -
How to convert an int variable into String type
hi everybody
i want to know how to convert an interger variable into string variable
i have to implement a code which goes like this
Chioce ch;
for(int i=0;i<32;i++)
// here i need a code to convert the int variable i into a string variable
ch.add(String variable);
how do i convert that int variable i into a String type variable??
can anyone help me?Different methods:
int a;
string s=a+"";or
String.valueOf(int) is the better option because Int.toString() generated an intermediate object to get the endresult
Ema
Maybe you are looking for
-
Using OIDs for SNMP GET queries
I'm having a tough time using the OIDs from the BEA-WEBLOGIC-MIB.asn1 file to perform SNMP GET requests on the console. Regardless of which OID I try, it throws a "NoSuchInstance" and fails to return data. This is Weblogic 6.1 on a Win2K machine. I e
-
Assign values to bapi structure dynamically
Hi, I have to use the BAPI, BAPI_MATERIAL_SAVEDATA to extend the material to different plant specific views. I have to use the structure BAPI_MARC and other structures in order to do this. But the values to be passed to the structure BAPI_MARC are no
-
am using iphone5s.some time the iphone is showing less signals?when i compare my phone with another phone by keeping side by side the signals are very less?
-
OIM AD Provisioning Issue -urgent prodution issue
Hi, We are facing this wierd issue where in user's manager get back the approval screen with Approve button activated even after they have hit the approve button. This is causing partial provisioning to trigger. For the first time provisioning proces
-
HT3739 HOW DO YOU CREATE A STACK?
Does anyone know how to create a stack in the dock?