Reputation: 215
making a simple algo with two classes. Trying to figure out why it won't print anything. Probably something obvious, but I cannot see it. The program takes 2 inputs, a String and an Int. It repeats the string the amount of times the int equals.
MAIN:
public class Main {
public static void main (String[]args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the string you want to repeat: ");
String str = input.nextLine();
input.nextLine();//Clear scanner memory
System.out.print("Enter the amount of times you want it to repeat: ");
int repeat = input.nextInt();
references.repeat(str, repeat);
}
}
SECOND CLASS:
public void repeat(String str, int n) {
for (int repeatNum = n; repeatNum > 0; repeatNum--) {
System.out.println(str);
}
}
Upvotes: 0
Views: 1047
Reputation: 44448
Since the previous answer was deleted:
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the string you want to repeat: ");
String str = input.nextLine();
System.out.print("Enter the amount of times you want it to repeat: ");
int repeat = input.nextInt();
System.out.println(str);
System.out.println(repeat);
}
}
Output:
Enter the string you want to repeat: dererer
Enter the amount of times you want it to repeat: 5
dererer
5
If you're not having this output, it's because of a localized issue that is not detailed in your post.
If you do exactly this and you still don't get the output as expected: edit your post and detail the exact steps you take.
Upvotes: 1