Reading and printing a string in Java - Assigning values

Click For Summary
SUMMARY

The discussion focuses on reading and printing a string and an integer in Java using the Scanner class. The user initially struggles with capturing input correctly, leading to the output of default variable values. Key methods highlighted include next() for reading strings and nextInt() for reading integers. The final solution successfully combines the inputs into the desired format, demonstrating the importance of understanding input methods in Java.

PREREQUISITES
  • Basic understanding of Java programming
  • Familiarity with the Scanner class in Java
  • Knowledge of data types, specifically String and int
  • Experience with variable assignment and output formatting in Java
NEXT STEPS
  • Learn Java Scanner class methods, specifically next() and nextInt()
  • Explore string manipulation techniques in Java
  • Study Java input/output formatting options
  • Practice error handling for user input in Java applications
USEFUL FOR

Java beginners, educators teaching programming fundamentals, and developers looking to improve their input handling techniques in Java applications.

obeying
Messages
8
Reaction score
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: 364
Technology news on Phys.org
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.
 
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.
 
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: 383

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 2 ·
Replies
2
Views
13K
  • · Replies 2 ·
Replies
2
Views
12K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
844
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K