This site requires JavaScript, please enable it in your browser!
Greenfoot back
tayyabah72
tayyabah72 wrote ...

2021/5/31

Converting string statement to integar

tayyabah72 tayyabah72

2021/5/31

#
How do we convert a string to integer when we have a print statement and then we are going to create switch case so just before switch case we will convert the string statement to integer?
rocket770 rocket770

2021/5/31

#
String number = "4";
You can print a string as a number or vice versa.
System.out.println("number:" number); // output - number: 4
System.out.println("number: "+4); // output - number: 4
Converting a String object into an int:
int n = Integer.parseInt(number);

// now a switch statement to do different things depending on the value

switch(n) {
case 0: // do something here if n = 0
break; 
case 1:  // etc 
}
tayyabah72 tayyabah72

2021/5/31

#
Here is my scenario in which i want to convert string to int. DECLARE and INITALISE Mode as zero DO DISPLAY INPUT “Which Mode would you like to play? Press 0 for Easy Press 1 for Moderate Press 2 for Hard” GET USER INPUT CONVERT String to Integer then comes the switch case.. Here am i not sure how to convert the string to int
danpost danpost

2021/5/31

#
You can use switch on char value in this way:
String key = Greenfoot.getKey();
if (key != null && key.length() == 1)
{
    switch (key.charAt(0))
    {
        case '0': ; break;
        case '1': ; break;
        case '2': ; break;
    }
}
You need to login to post a reply.