12 Replies - 437 Views - Last Post: 03 February 2018 - 05:39 PM Rate Topic: -----

#1 tlclayton1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-January 18

Height and weight may not have been intialized?

Posted 02 February 2018 - 03:35 AM

public class RudeQuestions { 
	public static void main( String[] args ) {
	String name;
	int age;
	double weight, income;
	double height, drawer;
	
	
	Scanner keyboard = new Scanner(System.in);
	
	System.out.print( "Hello. What is your name? ");
	name = keyboard.next();
	
	System.out.print( "Hi, " + name+ "! How old are you? ");
	age = keyboard.nextInt();
	
	System.out.println( "So you're " + age + ", eh? That's not very old. " );
	System.out.print( "How much do you weigh, " + "? ");
	weight = keyboard.nextDouble();
	
	System.out.println ( weight + "! Better keep that quiet!!" );
	System.out.print("Finally, what's your income, " + name + "?" );
	income = keyboard.nextDouble();
	
	System.out.print( "Hopefully, that is " + income+ "per hour" );
	System.out.println( " and not per year!" );
	System.out.print( "Well, thanks for answering my rude questions, " );
	System.out.println( name + ".");
	/*
	The program doesn't blow up entering a integer for a double because it's a number.
	The program doesn't blow up entering a numeric value for a string because strings encompass damn near everything.
	When I entered a string /words for a integer or double it blew up and I dont know why considering the comment above.
	*/
	System.out.println( name + " ! I have one more thing to ask you!!" );
	System.out.print( "May, I ask " + height + "please" );
	height = keyboard.nextDouble();
	
	System.out.print ("What is your " + drawer  + "ha ha" );
	drawer  = keyboard.nextDouble();
		
		}
}



The code will not compile. The error message sais height and weight may not have been initialized. I understand that height/weight must = something but if I remove variables height and weight the program will compile even though I didn't initialize weight and income.

This post has been edited by baavgai: 02 February 2018 - 03:43 AM
Reason for edit:: tagged


Is This A Good Question/Topic? 0
  • +

Replies To: Height and weight may not have been intialized?

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3540
  • View blog
  • Posts: 16,130
  • Joined: 20-September 08

Re: Height and weight may not have been intialized?

Posted 02 February 2018 - 03:39 AM

Please use code tags as you were asked. andrewsw's Posted Image instructions will show you how to use code-tags. It isn't difficult.
EACH JAVA FILE should have its own code tags
Was This Post Helpful? 0
  • +
  • -

#3 GazinAtCode  Icon User is offline

  • D.I.C Head

Reputation: 33
  • View blog
  • Posts: 121
  • Joined: 26-September 16

Re: Height and weight may not have been intialized?

Posted 02 February 2018 - 04:13 AM

I guess you mean height and drawer. The problem is that you're trying to use them in the print method before they are initialized (lines 35-36 and 38-39). That is not the case with the rest of the variables.

This post has been edited by GazinAtCode: 02 February 2018 - 04:15 AM

Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3540
  • View blog
  • Posts: 16,130
  • Joined: 20-September 08

Re: Height and weight may not have been intialized?

Posted 02 February 2018 - 04:53 AM

You need to look at those again. Even if you had initialized them correctly, the way you're using them is wrong - it doesn't make sense
Was This Post Helpful? 0
  • +
  • -

#5 tlclayton1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-January 18

Re: Height and weight may not have been intialized?

Posted 02 February 2018 - 11:32 AM

How do I use height and drawer in code so it compiles? How do you mean initialize?
Was This Post Helpful? 0
  • +
  • -

#6 tlclayton1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-January 18

Re: Height and weight may not have been intialized?

Posted 02 February 2018 - 11:59 AM

[quote name='tlclayton1' date='02 February 2018 - 11:32 AM' timestamp='1517596357' post='2352873']
Okay I initialized height and drawer and it compiled but it made no sense. The datatype used for height and drawer was double. Below was the result of my compiling.


import java.util.Scanner;

public class RudeQuestions { 
	public static void main( String[] args ) {
	String name;
	int age;
	double weight, income;
	string height, drawer;
	height = height;
	drawer = drawer;
	
	Scanner keyboard = new Scanner(System.in);
	
	System.out.print( "Hello. What is your name? ");
	name = keyboard.next();
	
	System.out.print( "Hi, " + name+ "! How old are you? ");
	age = keyboard.nextInt();
	
	System.out.println( "So you're " + age + ", eh? That's not very old. " );
	System.out.print( "How much do you weigh, " + "? ");
	weight = keyboard.nextDouble();
	
	System.out.println ( weight + "! Better keep that quiet!!" );
	System.out.print("Finally, what's your income, " + name + "?" );
	income = keyboard.nextDouble();
	
	System.out.print( "Hopefully, that is " + income+ "per hour" );
	System.out.println( " and not per year!" );
	System.out.print( "Well, thanks for answering my rude questions, " );
	System.out.println( name + ".");
	/*
	The program doesn't blow up entering a integer for a double because it's a number.
	The program doesn't blow up entering a numeric value for a string because strings encompass damn near everything.
	When I entered a string /words for a integer or double it blew up and I dont know why considering the comment above.
	*/
	System.out.println( name + " ! I have one more thing to ask you!!" );
	System.out.print( "May, I ask what is your " + height + "please? " );
	height = keyboard.nextDouble();
	
	System.out.print ("What is your " + drawer  + "ha ha" );
	drawer  = keyboard.nextDouble();


I attempted to use the string datatype and it wouldn't compile. What would you guys have done to make this make sense?

Thank you,
T

[quote name='tlclayton1' date='02 February 2018 - 11:55 AM' timestamp='1517597751' post='2352879']

View Posttlclayton1, on 02 February 2018 - 11:32 AM, said:

Okay I initialized height and drawer and it compiled but it made no sense. The datatype used for height and drawer was double. Below was the result of my compiling.


import java.util.Scanner;

public class RudeQuestions { 
	public static void main( String[] args ) {
	String name;
	int age;
	double weight, income;
	string height, drawer;
	height = 54;
	drawer = 64;
	
	Scanner keyboard = new Scanner(System.in);
	
	System.out.print( "Hello. What is your name? ");
	name = keyboard.next();
	
	System.out.print( "Hi, " + name+ "! How old are you? ");
	age = keyboard.nextInt();
	
	System.out.println( "So you're " + age + ", eh? That's not very old. " );
	System.out.print( "How much do you weigh, " + "? ");
	weight = keyboard.nextDouble();
	
	System.out.println ( weight + "! Better keep that quiet!!" );
	System.out.print("Finally, what's your income, " + name + "?" );
	income = keyboard.nextDouble();
	
	System.out.print( "Hopefully, that is " + income+ "per hour" );
	System.out.println( " and not per year!" );
	System.out.print( "Well, thanks for answering my rude questions, " );
	System.out.println( name + ".");
	/*
	The program doesn't blow up entering a integer for a double because it's a number.
	The program doesn't blow up entering a numeric value for a string because strings encompass damn near everything.
	When I entered a string /words for a integer or double it blew up and I dont know why considering the comment above.
	*/
	System.out.println( name + " ! I have one more thing to ask you!!" );
	System.out.print( "May, I ask what is your " + height + "please? " );
	height = keyboard.nextDouble();
	
	System.out.print ("What is your " + drawer  + "ha ha" );
	drawer  = keyboard.nextDouble();


Hello. What is your name? T
Hi, T! How old are you? 41
So you're 41, eh? That's not very old.
How much do you weigh, ? 168
168.0! Better keep that quiet!!
Finally, what's your income, T?120000
Hopefully, that is 120000.0per hour and not per year!
Well, thanks for answering my rude questions, T.
T ! I have one more thing to ask you!!
May, I ask what is your 54.0please? 54
What is your 64.0ha ha64
PS C:\Users\T\Documents\javahard>

I attempted to use the string datatype and it wouldn't compile. What would you guys have done to make this make sense?

Thank you,
T

Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3540
  • View blog
  • Posts: 16,130
  • Joined: 20-September 08

Re: Height and weight may not have been intialized?

Posted 02 February 2018 - 11:59 AM

The concatenation of the variable with the prompt makes no sense - don't do it. What does 'drawer' mean?
Was This Post Helpful? 0
  • +
  • -

#8 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 691
  • View blog
  • Posts: 5,267
  • Joined: 25-December 13

Re: Height and weight may not have been intialized?

Posted 02 February 2018 - 12:43 PM

Quote

it wouldn't compile

Please copy the full text of the error message and paste it here. It has important info about the error.
Was This Post Helpful? 0
  • +
  • -

#9 tlclayton1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-January 18

Re: Height and weight may not have been intialized?

Posted 02 February 2018 - 04:41 PM

View Postg00se, on 02 February 2018 - 11:59 AM, said:

The concatenation of the variable with the prompt makes no sense - don't do it. What does 'drawer' mean?


Drawer is underwear size
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3540
  • View blog
  • Posts: 16,130
  • Joined: 20-September 08

Re: Height and weight may not have been intialized?

Posted 03 February 2018 - 05:49 AM

OK. Well the comments so far have mentioned what the errors are. Read those and ensure that the last of the two prompts follow the approach of the earlier ones
Was This Post Helpful? 0
  • +
  • -

#11 tlclayton1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-January 18

Re: Height and weight may not have been intialized?

Posted 03 February 2018 - 04:54 PM

I just want to the program to run by saying may I ask what is your height? and what is your drawer?

I've tried using String data types and double. In both instances i've been told to initialize them. If I initialize them like so
height = whatever/ drawer = whatever the words height and weight won't be in the sentence.
Was This Post Helpful? 0
  • +
  • -

#12 tlclayton1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-January 18

Re: Height and weight may not have been intialized?

Posted 03 February 2018 - 05:19 PM

I figured it out finally. All I had to do was use the String variable and put quotation marks around the words height and weight making it officially a string.
import java.util.Scanner;

public class RudeQuestions { 
	public static void main( String[] args ) {
	String name;
	int age;
	double weight, income;
	String height, drawer;
	height = "height";
	drawer = "drawer";
	
	Scanner keyboard = new Scanner(System.in);
	
	System.out.print( "Hello. What is your name? ");
	name = keyboard.next();
	
	System.out.print( "Hi, " + name+ "! How old are you? ");
	age = keyboard.nextInt();
	
	System.out.println( "So you're " + age + ", eh? That's not very old. " );
	System.out.print( "How much do you weigh, " + "? ");
	weight = keyboard.nextDouble();
	
	System.out.println ( weight + "! Better keep that quiet!!" );
	System.out.print("Finally, what's your income, " + name + "?" );
	income = keyboard.nextDouble();
	
	System.out.print( "Hopefully, that is " + income+ "per hour" );
	System.out.println( " and not per year!" );
	System.out.print( "Well, thanks for answering my rude questions, " );
	System.out.println( name + ".");
	/*
	The program doesn't blow up entering a integer for a double because it's a number.
	The program doesn't blow up entering a numeric value for a string because strings encompass damn near everything.
	When I entered a string /words for a integer or double it blew up and I dont know why considering the comment above.
	*/
	System.out.println( name + " ! I have one more thing to ask you!!" );
	System.out.print( "May, I ask what is your " + "height" + "?" );
	height = keyboard.next();
	
	System.out.print ("What is your " + "drawer" + "?" );
	drawer  = keyboard.next();


}
}


Output

2 errors
PS C:\Users\T\Documents\javahard> javac RudeQuestions.java
PS C:\Users\T\Documents\javahard> java RudeQuestions
Hello. What is your name? T
Hi, T! How old are you? 41
So you're 41, eh? That's not very old.
How much do you weigh, ? 158
158.0! Better keep that quiet!!
Finally, what's your income, T? 100000
Hopefully, that is 100000.0per hour and not per year!
Well, thanks for answering my rude questions, T.
T ! I have one more thing to ask you!!
May, I ask what is your height? 54
What is your drawer? 34
PS C:\Users\T\Documents\javahard>
Was This Post Helpful? 0
  • +
  • -

#13 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 691
  • View blog
  • Posts: 5,267
  • Joined: 25-December 13

Re: Height and weight may not have been intialized?

Posted 03 February 2018 - 05:39 PM

"What is your " + "drawer" + "?"
 vs
"What is your drawer?"



No need to have separate Strings. Put the text all in one String.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1