Reading and printing a string in Java - Assigning values

In summary, the code prompts the user for a word and a number on a single line, reads them into the provided variables, and then prints them in the format of word_number, with a newline at the end. The program uses the Scanner class to read the user input, specifically the next() method for the word and the nextInt() method for the number.
  • #1
obeying
8
0
A user types a word and a number on a single line. Read them into the provided variables. Then print: word_number. End with newline. Example output if user entered: Amy 5
Result should read as: Amy_5
import java.util.Scanner;

public class SpaceReplace {
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
String userWord = "";
int userNum = 0;

/* Your solution goes here */

return;
}
}-----------------------------------------------------------------------------------------------------------------------------------------

What I have so far this is the code and the product I'm getting:

Am I missing a step in the codes? userNum is an int and not a String, could this be why my setup isn't working? Thank you to all who reply and assist.View attachment 5885
 

Attachments

  • Screen Shot 2016-08-13 at 6.42.50 PM.png
    Screen Shot 2016-08-13 at 6.42.50 PM.png
    23.8 KB · Views: 293
Technology news on Phys.org
  • #2
It appears to me that you aren't prompting the user for input, and so when the output is displayed you have the initial values for the two variables being displayed.
 
  • #3
The problem statement does not seem to say anything about prompting the user. But Mark is right that OP's program does not read anything.

OP, you should read your textbook or Java documentation about reading methods in the [m]Scanner[/m] class. These methods include [m]next()[/m] for reading the next word and [m]nextInt()[/m] for reading the next integer.
 
  • #4
Evgeny.Makarov said:
The problem statement does not seem to say anything about prompting the user. But Mark is right that OP's program does not read anything.

OP, you should read your textbook or Java documentation about reading methods in the [m]Scanner[/m] class. These methods include [m]next()[/m] for reading the next word and [m]nextInt()[/m] for reading the next integer.

Thanks! This is the solution I achieved after applying the correct code/nextInt.

View attachment 5890
 

Attachments

  • Screen Shot 2016-08-15 at 6.49.14 PM.png
    Screen Shot 2016-08-15 at 6.49.14 PM.png
    28.2 KB · Views: 301

What is a string in Java?

A string in Java is a sequence of characters. It is a data type that is used to store text-based data, such as words or sentences. In Java, strings are represented by the String class and can be assigned values using quotes.

How do you read and print a string in Java?

To read and print a string in Java, you can use the Scanner class to take input from the user and the System.out.println() method to print the string to the console. Alternatively, you can also use the System.out.print() or System.out.printf() methods to print the string without a new line.

How do you assign a value to a string in Java?

To assign a value to a string in Java, you can use the = assignment operator. For example, String greeting = "Hello"; will assign the value "Hello" to the greeting variable of type String.

Can a string be changed after it is assigned a value?

No, in Java, strings are immutable, which means they cannot be changed after they are assigned a value. However, you can create a new string with a different value by using methods such as concat() or substring().

What is the difference between System.out.print() and System.out.println() when printing a string in Java?

The System.out.print() method prints the string without adding a new line, while the System.out.println() method prints the string and adds a new line after it. This means that when using System.out.println(), the next output will be printed on a new line, whereas System.out.print() will continue on the same line.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
2
Views
12K
  • Programming and Computer Science
Replies
3
Views
776
  • Programming and Computer Science
Replies
2
Views
12K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
1
Views
6K
Back
Top