• Post Reply Bookmark Topic Watch Topic
  • New Topic

Variables, input, and casting  RSS feed

 
Mark Georgevich
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All,
Below is a code I ran into NetBeans and it keeps giving me a weird output. basically it doesn't like my character reference I think. I am very new at this so please bear with me. My class at school is online and condensed.


Code:

import java.util.Scanner;




Here is the output from Netbeans:

3.77
Enter z
z
Enter Howdy
Howdy
993.77zHowdy
BUILD SUCCESSFUL (total time: 8 seconds)


Thanks for any help:
 
Dave Tolls
Ranch Foreman
Posts: 3061
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you trying to do?
What should your output look like?
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You most likely want to add two numbers of which one of those are integer, the other - double.
Now when you put all that to a print statement, what you get is string concatenation.

if you choose for integer 4 lets say, for double 3.77 after concatenation you get 43.77, now if you want to actually add them - enclose them with parenthesis.

Instead:

To:So you would get 7.77

Is this is what was strange to you?
 
Mark Georgevich
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The directions say this:

(1) Prompt the user to input an integer, a double, a character, and a string, storing each into separate variables. Then, output those four values on a single line separated by a space. (Submit for 2 points).

Enter integer: 99
Enter double: 3.77
Enter character: z
Enter string: Howdy

So my confusion is why my code doesn't work.

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

First, you never showed us the output from "enter int" and "enter double". So, we don't know what is wrong -- nor did you explain it to us either.

Second, you never implemented "enter character" and "enter string". So, that is definitely wrong -- but regardless, you didn't explain it to us either.

Henry
 
John Joe
Ranch Hand
Posts: 436
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Georgevich wrote:The directions say this:
(1) Prompt the user to input an integer, a double, a character, and a string, storing each into separate variables. Then, output those four values on a single line separated by a space. (Submit for 2 points).

This is the correct code
[not anymore]

 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Joe, thanks for your contribution.

However, we do have a policy here - don't feed users with complete solutions.
Most of the users coming here to learn, those very few who come here for other reasons, we treat them as they were come for the same (learning).

Now, giving away a complete solution it may help user instantly, but in a longer perspective everything could land dramatically bad - in effect of lost money in studies and not gained any knowledge, there are worse cases too.

That isn't so good if we think carefully, is it?

So please refrain from posting complete solutions, that robs user from getting a chance to come up with his own chef-d'oeuvre.

Giving some hints which lead OP towards their own solution or small similar code snippets which visualise concept are good, but probably no more than that.

Thanks
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After all, I have pulled my authentic sheriff's hat and deleted your solution. We might revert it back after user shows his version.
 
John Joe
Ranch Hand
Posts: 436
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So any update for this question ?
 
John Joe
Ranch Hand
Posts: 436
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
John Joe wrote:So any update for this question ?
Unfortunately, a lot of users disappear from this website before completing their assignments. This message will self‑destruct in 48 hours.


How sad ! 
 
Mark Georgevich
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the delayed response. You were correct, I was actually adding the variables in the output instead of using the quotations . So my print out statement saying ( userInput + userDouble  +  etc.

Thanks for your help.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are not going to go away without showing us your approach, so we could all enjoy, are you?
 
Mark Georgevich
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
LOL okay letr me show you. I don't want to get yelled at for posting the whole code so I will post just a little.

Here is my code,



Here is the output of the Zybook results:

Enter integer,
Enter double,
Enter a character
Enter a word
993.77zHowdy

I still can't figure out why they don't print on one line. I am so new at this its all too confusing.




 
Mark Georgevich
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I am still having issues. Why isn't this code printing out the correct values?





This is the error output. This isn't making sense to me.



      
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Georgevich wrote:
This is the error output. This isn't making sense to me.
BasicInput.java:21: error: ')' expected
      System.out.print(userInt "+" userDouble "+" userInput "+" userWord);
      


That is a compiler error... basically, a syntax error. Or in other words, it is not valid Java.

Anyway, what are you trying to do with that line?

Henry
 
Paul Clapham
Sheriff
Posts: 22829
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Georgevich wrote:System.out.print(userInt "+" userDouble "+" userInput "+" userWord);


Basically that isn't valid Java syntax so the compiler is getting totally confused. I don't know what you meant to do there so I'll just make a guess: you wanted to print those four things separated by plus characters. That would look like this:



Or maybe you wanted to print those things all run together with nothing between them?



Or maybe with spaces between them?

 
Mark Georgevich
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These are what the directions said:


(1) Prompt the user to input an integer, a double, a character, and a string, storing each into separate variables. Then, output those four values on a single line separated by a space. (Submit for 2 points).
 
Paul Clapham
Sheriff
Posts: 22829
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. So, problem solved, right?
 
Mark Georgevich
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually you have given me the answer. The (userInput + " ' +  userDouble + " " + ,etc. fixed it.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Georgevich wrote:These are what the directions said:

(1) Prompt the user to input an integer, a double, a character, and a string, storing each into separate variables. Then, output those four values on a single line separated by a space. (Submit for 2 points).


I think that it is fair to say... that your program should "output those four values on a single line separated by a space".  And your instructor didn't mean that your program should be "four variables on a single line separated by space". Whatever the output, your Java program should still be valid Java statements.

Henry

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!