Scanner nextline() excution failure - java.util.scanner

I want to stop the excution of the program after the first nextline() call, however it's printing after the nextLine() call instead of stopping.
if(j==1){
System.out.println("Enter Book Title");
String bTitle = inputs.nextLine();
System.out.println("Enter Book Reference");
String bRef = inputs.nextLine();
System.out.println("Enter Book Price");
}
Output:
Enter Book Title
Enter Book Reference

I tried out the given code and its doing as expected. Did you want an input after the last print out statement? If so, just call nextInt() after the last print out statement and assign the int to a variable.

Related

How does visual studio read instructions from code?

Hi there StackOverflow community. I’ve faced a certain issue in understanding how visual studio processes the ‘instructions’ given by the code.
the code I’ve written was giving me a problem when i was running it.
It went something like:
whats your guess?
donkey
whats your guess?
donkey
your guess was: donkey
whats your guess?
dog
whats your guess?
dog
your guess was: dog.
It works correctly on alternative tries.
However, if i were to remove ' GetGuess();' on line 38. everything works perfectly fine.
However, it just bothers me that the Getguess on line 38 is nt require because the thought im having right now is that it has to process Getguess first before it processes giving back the guess.
Thank you
code:
#include <iostream>;
#include <string>;
using namespace std;
void PrintIntro();
void play_game();
string GetGuess();
// entry point for out application
int main()
{
PrintIntro();
play_game();
}
//intro game
void PrintIntro()
{
constexpr int WORD_LENGTH = 5;
cout << "welcome to bulls and cows\n";
cout << "can you guess the " << WORD_LENGTH << " letters word?\n";
return ;
}
void play_game()
{
// loop for number of turns asking for guesses
constexpr int number_of_turn = 5;
for (int count = 1; count <= number_of_turn; count++)
{
GetGuess();
string guess = GetGuess();
cout << "your guess was:" << guess << endl;
}
}
//gut guess from player
string GetGuess()
{
// ask for a guess
cout << "whats your guess?\n";
string guess = "";
getline(cin, guess);
return guess;
}
A piece of advice: When you run the code please put breakpoints, especially if you want to know how your code run.
"It works correctly on alternative tries. However, if i were to remove ' GetGuess();' on line 38. everything works perfectly fine. However, it just bothers me that the Getguess on line 38 is nt require because the thought im having right now is that it has to process Getguess first before it processes giving back the guess."
To answer your question, first your must know that, when you call "string guess = GetGuess();" the compiler already retrieve the return value and store it into "guess" (in this case, the function return guess), therefore you don't have to add GetGuess() anymore (this is redundant). Your thinking that the compiler needs to "process Getguess first" is incorrect since "string guess = GetGuess();" is already processing/retrieving the return value.
Also, I think it's a good practice to initialize and declare all functions before int main() but then, it's up to how you organize all your code functions.

I used cin to get a string input as shown below in the code and it works fine. Is it alright?

#include<iostream>
#include<stdio>
using namespace std;
int main()
{ int n;
char s[15];
cin>>n;
cin>>s;
cout<<n*2<<"\n";
cout<<s;
return 0;
}
I tried with gets and fgets function but they don't work just after cin..
I'm kind of confused on what you are asking here, but I have noticed something here that can be fixed.
Yes the code you have compiles and it works. However, it could be improved.
When prompted to input something to your char array, you'll notice that it will not accept whitespaces. So if I input, Jon Smith, the output will only be Jon and the rest of the string input is cut off. To fix this, you will need to make a call the the getline() function.
The documentation of getline() states:
Extracts characters from is and stores them into str until the delimitation character delim is found (or the newline character, '\n'..)
This will allow you to get whitespaces from the input and put the entire input back into a string.
If you add this function call to your code where the second input prompt lies and you were to run the code, you would notice that you will only get prompted once and then the program would finish running before the second prompt appears to be executed. This is because getline() does not ignore leading whitespace characters and it stops reading any further because the cin>> before it is seen as a newline character.
To make getline() work with cin>>, you must use cin.ignore() before the call to getline(). Below is some code that I wrote to make this adjustment:
// Example program
#include <iostream>
#include <string>
using namespace std;
int main()
{
int n;
string s; //using string allows us to use getline()
cout<<"Enter a number: "; //Let user know they are being prompt for number
cin>>n;
cin.ignore(); //ignore the leading newline
cout<<"Enter a string: "; //let user know being prompt for string
getline (cin,s);
cout<<n*2<<"\n";
cout<<s;
return 0;
}
Again, the code you have works and compiles. I'm not sure if my solution is the answer you are hoping to get but I hope that you are able to find this useful! Cheers!

User input in D

I am somewhat new to D, and I am trying to receive user input, like this, with a prompt:
string str;
writeln("Enter a string: ");
str = readln;
writeln(str);
However, the prompt appears after I enter the input; any reason why?
I've trawled the internet for a good hour, but I can't seem to find an answer.
Your code is correct, it's your terminal or whatever you use to see your program's output that doesn't flush stdout. You can force it though:
string str;
writeln("Enter a string: ");
stdout.flush;
str = readln;
write(str);

HashMap cannot cast other type?

public Human setName(){
Human a=null;
System .out.println("change name ");
System.out.println("enter Security No : ");
String inputsecNo=sc.next();
System.out.println("enter name you wish to change : ");
String name=sc.next();
Human value=null;
value= humanList.get(inputSecNo);
value.setName(name);
I don't know why this error occur...
I made a HashMaphumanList
and want to get value of key(String).
it says HashMap cannot be cast to Human...
i used iterator to get value of map. but it occur same error.

Read a String with spaces till a new line in C

I am in a pickle right now. I'm having trouble taking in an input of example
1994 The Shawshank Redemption
1994 Pulp Fiction
2008 The Dark Knight
1957 12 Angry Men
I first take in the number into an integer, then I need to take in the name of the Movie into a string using a character array, however i have not been able to get this done.
here is the code atm
while(scanf("%d", &myear) != EOF)
{
i = 0;
while(scanf("%[^\n]", &ch))
{
title[i] = ch;
i++;
}
addNode(makeData(title,myear));
}
The title array is arbitrarily large and the function is to add the data as a node to a linked list. right now the output I keep getting for each node is as follows
" hank Redemption"
" ion"
" Knight"
" Men"
Yes, it oddly prints a space in front of the cut-off title. I checked the variables and it adds the space in the data. (I am not printing the year as that is taken in correctly)
How can I fix this?
You are using the wrong type of argument passed to scanf() -- instead of scanning a character, try scanning to the string buffer immediately. %[^\n] scans an entire string up to (but not including) the newline. It does not scan only one character.
(Marginal secondary problem: I don't know from where you people are getting the idea that scanf() returns EOF at end of input, but it doesn't - you'd be better off reading the documentation instead of making incorrect assumptions.)
I hope you see now: scanf() is hard to get right. It's evil. Why not input the whole line at once then parse it using sane functions?
char buf[LINE_MAX];
while (fgets(buf, sizeof buf, stdin) != NULL) {
int year = strtol(buf, NULL, 0);
const char *p = strchr(buf, ' ');
if (p != NULL) {
char name[LINE_MAX];
strcpy(name, p + 1); // safe because strlen(p) <= sizeof(name)
}
}

Resources