How to concatenate string in a for loop using scanner? [duplicate] - java

This question already has answers here:
How do I concatenate two strings in Java?
(23 answers)
Closed 6 years ago.
package exercises;
import java.util.Scanner;
public class SentenceBuilder {
public static void main(String[] args) {
final int MAX_WORDS = 5;
Scanner scan = new Scanner(System.in);
String word ="";
for (int i = 0; i < MAX_WORDS; i++){
System.out.println("Please enter word "+(i+1)+" of "+MAX_WORDS);
word = scan.nextLine();
}
System.out.println(word);// im stuck on how to concatenate the result
}
}

Inside the for:
word = word + scan.nextLine();

Related

Why does my while loop break after I add one object to the list [duplicate]

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 6 months ago.
I'm doing the Java MOOC fi course. I'm supposed to make a list of show names and durations, and print the ones that are shorten than certain duration.
I want the while loop to end after I enter a blank name but it ends after the first object I add to the list.
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// implement here your program that uses the TelevisionProgram class
ArrayList<TelevisionProgram> programs = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Name: ");
String programName = scanner.nextLine();
if (programName.isEmpty()) {
break;
}
System.out.print("Duration: ");
int durationOfProgram = scanner.nextInt();
programs.add(new TelevisionProgram(programName, durationOfProgram));
}
System.out.println();
System.out.print("Program's maximum duration? ");
int maxDuration = scanner.nextInt();
for (TelevisionProgram show : programs) {
if (show.getDuration() <= maxDuration) {
System.out.println(show);
}
}
}
}
replaced
int durationOfProgram = scanner.nextInt();
with
int durationOfProgram = Integer.valueOf(scanner.nextLine());
and now it works but i still don't get why

My code's output is not the array I input [duplicate]

This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 1 year ago.
I am trying to run the following code in order to input an array of any size I desire:
import java.util.Scanner;
import java.util.Arrays;
public class testing2 {
public static void main(String[] args) {
double[] inputs = new double[5];
int currentSize = 0;
System.out.println("Please enter values, Q to quit:");
Scanner in = new Scanner(System.in);
while (in.hasNextDouble())
{
if (currentSize >= inputs.length)
{
inputs = Arrays.copyOf(inputs, 2 * inputs.length);
}
inputs[currentSize] = in.nextDouble();
currentSize++;
}
inputs = Arrays.copyOf(inputs, currentSize);
System.out.print(inputs);
}
}
But when I input any array, my output is just a combination of symbols such as: [D#13fee20c, and this is not the output I am hoping for. Could anyone help me?
You're not printing correctly:
for (int i = 0; i < input.length; i++)
System.out.print(input[i] + " ");
}

How do i use a for loop to do this output? [duplicate]

This question already has answers here:
Make a upside down triangle in java
(3 answers)
Closed 4 years ago.
I'm a beginner in java and I need help with this code. I have to let the user enter a word and the output as follows,(I used Canada as an example)
Canada
anada
nada
ada
da
a
However, I'm not sure what to do. This is what I have so far
import java.util.*;
public class javapdf2413_17 {
public static void main (String [] args) {
Scanner in = new Scanner (System.in);
System.out.println("Enter in a single word");
String wordEntered = in.next();
for (int i = wordEntered.length(); i>=0; i--) {
System.out.println(wordEntered.substring (0, i));
}
}
}
This should do it
for (int i = 0; i < wordEntered.length(); i++) {
System.out.println(wordEntered.substring(i));
}
The above substring method takes the beginning index of the string and returns a substring from that index (inclusive) till the end of the string.
You're close. String.substring(1) will return everything from the second character to the end. And I would use that in a loop with test against the empty string. Like,
while (!wordEntered.isEmpty()) {
System.out.println(wordEntered);
wordEntered = wordEntered.substring(1);
}

How do I identify two different spaces between three words in an input from the user in java? [duplicate]

This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 6 years ago.
import java.util.Scanner;
public class Exercise12 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Please enter your full name:");
String input = s.nextLine();
int space1 = input.indexOf(' ');
String fname = input.substring(0,space1);
System.out.println(fname);
}
}
Have a look at the split() method for Strings.
String[] tokens = input.split(" ");

How do I return a character an inputted number of times? [duplicate]

This question already has answers here:
Can I multiply strings in Java to repeat sequences? [duplicate]
(19 answers)
Closed 7 years ago.
import java.util.Scanner;
public class myLine {
public static void main(String[] args) {
String CurrentLine = new String();
int length =0;
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
if (CurrentLine.charAt(0) == 'l') {
return '*' x times;
}
}
}
The last line is just me taking note of what I want to do. If the user puts in a number for x, how do I return the * character that many times?
You can't return in main (it's a void method). You aren't using length, and if I understand your question you could use a StringBuilder and a simple for loop. Then print the contents of the StringBuilder. Something like
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < x; i++) {
sb.append('*');
}
System.out.println(sb.toString());
}

Categories