So I have to make a code that asks the user how many times it wants the word "nonsense" to be printed on a new line, I managed to get it to work but its reverse, if i write 100 it prints once and 1 it prints hundred times, how can i solve it so it make the user input tell the programme how many times it should print out the line.

my code is:

import java.util.Scanner;

public class fix {

    public static void main(String[] args) {
    Scanner scan = new Scanner (System.in);
    int antal = 0;
    int i = 1;
    System.out.println("Enter how many times we should print out the word nonsense");
    antal = scan.nextInt();

    while (100 > antal)
    {
        System.out.print(" \nnonsense");
        antal += i;
    }
    }
    }

You are 99% of the way there.
It's really hard as a beginner to think about the logic and the Java code at the same time! Step back from the computer and just run through that on a sheet of paper. You'll soon see why its going wrong, and how it should work. Then (and only then) go back to the computer and fix the code to give the right logic.
(ps that's what the most experienced programmers do when they hit a logic problem, so don't be embarassed!)

Try below program

import java.util.Scanner;

public class loop {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int antal = 0;
        int i = 0;
        System.out.println("Enter how many times we should print out the word nonsense");
        antal = scan.nextInt();

        while (i < antal) {
            System.out.print(" \nnonsense");
            i++;
        }
    }
}
    Thank you for your reply, however I don't seem to find the issue eventhough its probably a small one, could you give further hints? is it in the while loop?

Yes. it will be in while loop. While condition should be i < antal(no of times you will be entering through key board). But you have given antal += i;
But it was supposed to be i++;

Seems to work now, however since if i press in 4 it prints 3 and 5 4 is it because it starts from 0,1,2,3...?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.