Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialEi 123
6,924 PointsHow to remove the dot in while loop?
Hello everyone, I started to study JAVA, So sorry for the question.. I practice the WHILE LOOP, so I have this code:
import java.util.Scanner; public class Class {
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
System.out.println( "Type in a message" );
System.out.print( "Message: " );
String message = keyboard.nextLine();
double n = 0;
while ( n < 10 )
{
System.out.println( (n+1) + "." + message );
n++;
}
}
}
so, I want to get a result somthing like that: 10. 20. 30. and etc..
but I get: 1.0. , 2.0., 3.0. and etc..
what I should do to remove this dot, between 1 and 0...? thank you very much for your help :).
3 Answers
Luca Cunico
Courses Plus Student 14,196 PointsThis is one way:
import java.util.Scanner;
public class Main {
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
System.out.println( "Type in a message" );
System.out.print( "Message: " );
String message = keyboard.nextLine();
int n = 0;
while ( n < 10 )
{
System.out.println( (n+1) + "0" + "." + message );
n++;
}
}
}
This is the other way:
import java.util.Scanner;
public class Main {
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
System.out.println( "Type in a message" );
System.out.print( "Message: " );
String message = keyboard.nextLine();
int n = 10;
while ( n < 101 )
{
System.out.println( (n) + "." + message );
n+=10;
}
}
}
On my output I got:
10.<Your_message>
20.<Your_message>
as you requested
Ei 123
6,924 Pointssorry but I have not got the right result yet, now I get: 11.0, 12.0, sorry.. again I need to get: 10. , 20. , 30.,...and etc..
Ei 123
6,924 PointsOK, now it's work,
Thank you very much, it's very help me :)
Luca Cunico
Courses Plus Student 14,196 PointsYou're welcome! Have fun!
Luca Cunico
Courses Plus Student 14,196 PointsLuca Cunico
Courses Plus Student 14,196 PointsI set the while condition to 101 on the second example cause I assume you want to end the program at 100.<Message> as your first program ended at 10.