Java Reading and printing a string in Java - Assigning values

AI Thread Summary
The discussion revolves around a Java coding issue where a user needs to input a word and a number on a single line, which should then be processed to output in the format "word_number". The original code provided by the user fails to read input correctly, resulting in the display of initial variable values instead of user input. Key points include the importance of using the Scanner class methods, specifically next() for reading strings and nextInt() for integers. The conversation emphasizes the need for proper input handling and suggests reviewing Java documentation for better understanding. Ultimately, the user successfully resolves the issue by implementing the correct methods for reading input.
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: 348
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: 367
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
2
Views
13K
Replies
2
Views
12K
Replies
2
Views
2K
Replies
3
Views
1K
Replies
2
Views
2K
Replies
0
Views
402
Replies
2
Views
2K
Back
Top