# Thread: not sure what I doing trying to get data to work

1. Member
Join Date
Dec 2010
Posts
7
Rep Power
0

## not sure what I doing trying to get data to work

I have a program I am trying to build and what to make a range of prices low, mid , high for three different rooms, using if statement but not sure if this will work or if the data will ever return right please help, here is what I have

input is the rooms entered by a number before it worked fine before thanks

range=keyboard.nextLine();

double base=10,price;

if((input<=2) && (range=="low"||range=="LOW"||range=="Low"))
{

price= base+5;
System.out.println("\n The price per sqr foot for "+room.get(input-1)+" is "+price);
}
else if((input<=2) && (range=="mid"||range=="Mid"||range=="MID"))
{
price=base*1.5;
System.out.println("\n The price per sqr foot for "+room.get(input-1)+" is "+price);
}
else if((input<=2)&& (range=="high"||range=="High"||range=="HIGH"))

{
price=base*2;
System.out.println("\n The price per sqr foot for "+room.get(input-1)+" is "+price);
}
else if((input>2 || input>5) &&(range=="low" || range=="Low"|| range=="LOW"))
{
price=base;
System.out.println("\n The price per sqr foot for "+room.get(input-1)+" is "+price);
}
else if((input>2 || input>5) &&(range=="mid"||range=="Mid"||range=="MID"))
{
price=base*1.4;
System.out.println("\n The price per sqr foot for "+room.get(input-1)+" is "+price);
}
else if((input>2 ||input>5) &&(range=="high"||range=="High"||range=="HIGH") )
{
price=base*1.8;
System.out.println("\n The price per sqr foot for "+room.get(input-1)+" is "+price);
}
else if((input==5)&&(range=="low"||range=="Low"||range= ="LOW"))
{
price=base+3;
System.out.println("\n The price per sqr foot for "+room.get(input-1)+" is "+price);
}
else if((input==5) &&(range=="mid"||range=="Mid"||range=="MID"))
{
price=base*1.5;
System.out.println("\n The price per sqr foot for "+room.get(input-1)+" is "+price);
}
else if((input==5)&& (range=="high"||range=="High"||range=="HIGH"))
{
price=base*1.9;
System.out.println("\n The price per sqr foot for "+room.get(input-1)+" is "+price);
}

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14
It's not clear what problem you're having.

Does your code compile? If not and you can't understand the message, post the code and the complete compiler output.

Does your program not do what you expect or intend when you run it? If so, post the code and describe both what the program actually does and what precisely you expect or intend the runtime behaviour to be.

-------------------------------------

Java Code:
`range=="low"`

This is almost certainly wrong. What you probably intend is

Java Code:
`range.equals("low")`

equals() is a String method that checks whether two strings are made up of the same characters in the same order. ==, on the other hand, checks for identity. It is quite possible for two strings to be equal without their being identical (in which case they wouldn't be two strings...)

3. Member
Join Date
Dec 2010
Posts
7
Rep Power
0
thanks that fixed the one problem will I be able to take my price variable out of these statements after?

4. Originally Posted by camtheman
thanks that fixed the one problem will I be able to take my price variable out of these statements after?
Your problems aren't solved yet because that entire sequence of if-else-if statements doesn't make sense; have a look:

Java Code:
```else if((input>2 || input>5) &&(range=="low" || range=="Low"|| range=="LOW"))
{
...
}
...
else if((input==5)&&(range=="low"||range=="Low"||range= ="LOW"))
{
...
}```
Ignoring the fact that you're trying to compare Strings with an == operator (Pbrockway2 already told you what to do instead), suppose input == 5. The first if condition will be true (input > 2) but I think you want to execute the last if statement.

kind regards,

Jos

5. Member
Join Date
Dec 2010
Posts
7
Rep Power
0
:eek:thanks i did find those problems too, so the price variable how can I use it after the if else statements thanks

6. Originally Posted by camtheman
:eek:thanks i did find those problems too, so the price variable how can I use it after the if else statements thanks
Simply use it after your code has wobbled through all those if statements; make sure that variable is initialzed.

kind regards,

Jos

7. Member
Join Date
Dec 2010
Posts
7
Rep Power
0
ok So I fixed the other poblems but now I want to use whatever price variable that becomes true in the if statements is there I way to do that here it is

System.out.println("Please enter your budget range: low, mid, high ");//this became big pain trying to get the data to work out right

range=keyboard.nextLine();

double base=10,price=0.0;
// double area=one.getarea()

if((input<=2) && (range.equals("low")||range.equals("LOW")||range.e quals("Low")))
{

price= base+5;
System.out.println("\n The price per sqr foot for "+room.get(input-1)+" is "+price);
System.out.println("\n The total including tax will be "+(price*tax*one.getarea()));
}

else if((input<=2) && (range.equals("mid")||range.equals("Mid")||range.e quals("MID")))
{
price=base*1.5;
System.out.println("\n The price per sqr foot for "+room.get(input-1)+" is "+price);
System.out.println("\n The total including tax will be "+(price*tax*one.getarea()));
}
else if((input<=2)&& (range.equals("high")||range.equals("High")||range .equals("HIGH")))

{

price=base*2;
System.out.println("\n The price per sqr foot for "+room.get(input-1)+" is "+price);
System.out.println("\n The total including tax will be "+(price*tax*one.getarea()));
}
else if((input>2 && input>5) &&(range.equals("low") || range.equals("Low")|| range.equals("LOW")))
{
price=base;
System.out.println("\n The price per sqr foot for "+room.get(input-1)+" is "+price);
System.out.println("\n The total including tax will be "+(price*tax*one.getarea()));
}
else if((input>2 && input>5) &&(range.equals("mid")||range.equals("Mid")||range .equals("MID")))
{
price=base*1.4;
System.out.println("\n The price per sqr foot for "+room.get(input-1)+" is "+price);
System.out.println("\n The total including tax will be "+(price*tax*one.getarea()));
}
else if((input>2 && input>5) &&(range.equals("high")||range.equals("High")||ran ge.equals("HIGH")))
{
price=base*1.8;
System.out.println("\n The price per sqr foot for "+room.get(input-1)+" is "+price);
System.out.println("\n The total including tax will be "+(price*tax*one.getarea()));
}
else if((input==5)&&(range.equals("low")||range.equals( "Low")||range.equals("LOW")))
{
price=base+3;
System.out.println("\n The price per sqr foot for "+room.get(input-1)+" is "+price);
System.out.println("\n The total including tax will be "+(price*tax*one.getarea()));
}
else if((input==5) &&(range.equals("mid")||range.equals("Mid")||range .equals("MID")))
{
price=base*1.5;
System.out.println("\n The price per sqr foot for "+room.get(input-1)+" is "+price);
System.out.println("\n The total including tax will be "+(price*tax*one.getarea()));
}
else if((input==5)&& (range.equals("high")||range.equals("High")||range .equals("HIGH")))
{
price=base*1.9;
System.out.println("\n The price per sqr foot for your "+room.get(input-1)+" is "+price);
System.out.println("\n The total including tax will be "+(price*tax*one.getarea()));
}
:confused:

8. Senior Member
Join Date
Mar 2010
Posts
952
Rep Power
7
When you see a bunch of lines that pretty much look alike in your code, it's a pretty good clue that you really want a loop or a method or both.

You seem to be trying to choose a price formula from a table that looks like this:
Java Code:
```            Low     Medium  High
i <= 2      b+5     b*1.5   b*2
2 < i < 5   b       b*1.4   b*1.8
i = 5       b+3     b*1.5   b*1.9```
It also looks like you want price to be a double (which is actually a poor choice for a money value, but we'll save that lesson for another time). So you should write a method something like this:
Java Code:
```        private double computePrice(int input, String range) {
double result = -1.0; // default for bad input or range
if (range.equalsIgnoreCase("low") {
if (input <= 2 ) {
result = base + 5
} else if (2 < input && input < 5) {
result = base;
} else if (input == 5) {
result = base + 3;
}
} else if (range.equalsIgnoreCase("medium") {
// TODO write this part
} else if (range.equalsIgnoreCase("high") {
// TODO write this part
}
return result;
}```
Then you want another method to do your printing. (It would be better style to return a String rather than print it directly, but that will have to wait for a later lesson too.)
Java Code:
```        private void printPriceInfo(int input, String range) {
double price = computePrice(input, range);
System.out.println("\n The price per sqr foot for "+room.get(input-1)+" is "+price);
System.out.println("\n The total including tax will be "+(price*tax*one.getarea()));
}```
Good luck!

-Gary-

9. Member
Join Date
Dec 2010
Posts
7
Rep Power
0
thanks gary I got the decial formated so no problem with the money and thanks so much again

10. Member
Join Date
Dec 2010
Posts
7
Rep Power
0
can I take two objects from a do while loop and save them everytime in runs then print them all out latter? this is my code it works I just would like to take each enrty and print them all out at the end what why should I do this?

do{// this way we can do as many rooms as you want

//Scanner keyboard = new Scanner (System.in);put these here but they did not work right because I needed them after do while
//DecimalFormat fix = new DecimalFormat("#.00");

System.out.println("\t \n Hello! Welcome to Your Reno Estmeter"+"\n\t*********************************** ***");//this is what I did for a living for about 15 years so I should know A few things about it.
System.out.println("\nPlease choose a Room in your house you would like to Reno.");

ArrayList room = new ArrayList();//I like the idea of an Arraylist so I thought I would use one, sure this could be done many different ways.
room.add(" Bathroom");//thinking of using class to make these rooms come to life still not sure all that I can do with classes but we will see

System.out.println("\n *****1"+room.get(0)+"} 2"+room.get(1)+"} 3"+room.get(2)+"} 4"+room.get(3)+"} 5"+room.get(4)+"}****");//output with each room from arraylist
System.out.println("----------------------------------------------------------------------------------------------------");
System.out.println("\nPlease enter the number on the left of the name to give me your choice");//using numbers to make it easier in my mind anyways.
input= keyboard.nextInt ();

while (input <1 || input>5)//while statement to stop unwanted input works great
{
System.out.println("\nSorry that is not one of the rooms in this planner "+
System.out.println("1"+room.get(0)+" 2"+room.get(1)+" 3"+room.get(2)+" 4"+room.get(3)+" 5"+room.get(4));
input= keyboard.nextInt ();
}
//System.out.println(room.get(input-1)); put this in as a test of the print do not need it now
System.out.println(" Good! Who does not want a new"+room.get(input-1)+"?");
System.out.println("\nOk let's get some info so we can get you some prices :)");

roombuilder one= new roombuilder();//build of the rooms size and height in roombuilder class

System.out.println("\n A good place to start is with the size of your room.\n Please give me the length of your "+room.get(input-1)+" in feet and inches, for example, 10.6 feet");
in1=keyboard.nextDouble();
one.setlength(in1);
System.out.println();

System.out.println(" Ok now we need the width, please enter a width, following the same format");
in2=keyboard.nextDouble();
one.setwidth(in2);

System.out.println("It would be very helpful if we could have the height of you ceilings too, so please enter your height");
in3=keyboard.nextDouble();
one.setheight(in3);

System.out.println("\n Ok, so we have a length of "+fix.format(one.getlength())+" and we have a width of "+fix.format(one.getwidth())+" this gives us an area of "
+fix.format(one.getarea())+" and our height is "+one.getheight());
System.out.println();
keyboard.nextLine();

System.out.println("Please enter your budget range: low, mid, high ");//this became big pain trying to get the data to work out right

range=keyboard.nextLine();

if((input<=2) && (range.equalsIgnoreCase("low")))//wow this saves time from all this other stuff I did should have used method here but did not want to erase after solved problems
{

price= base+5;
System.out.println("\n The price per sqr foot for "+room.get(input-1)+" is "+price);

System.out.println("\n The total including before tax will be "+fix.format(price*one.getarea()));
System.out.println("\n The total including tax for your "+room.get(input-1)+" "+fix.format(price*tax*one.getarea()));
}

else if((input<=2) && (range.equals("mid")||range.equals("Mid")||range.e quals("MID")))
{
price=base*1.5;
System.out.println("\n The price per sqr foot for "+room.get(input-1)+" is "+price);

System.out.println("\n The total including before tax will be "+fix.format(price*one.getarea()));
System.out.println("\n The total including tax for your "+room.get(input-1)+" "+fix.format(price*tax*one.getarea()));
}
else if((input<=2)&& (range.equals("high")||range.equals("High")||range .equals("HIGH")))

{

price=base*2;
System.out.println("\n The price per sqr foot for "+room.get(input-1)+" is "+price);

System.out.println("\n The total including before tax will be "+fix.format(price*one.getarea()));
System.out.println("\n The total including tax for your "+room.get(input-1)+" "+fix.format(price*tax*one.getarea()));
}
else if((input>2 && input<5) &&(range.equals("low") || range.equals("Low")|| range.equals("LOW")))
{
price=base;
System.out.println("\n The price per sqr foot for "+room.get(input-1)+" is "+price);

System.out.println("\n The total including before tax will be "+fix.format(price*one.getarea()));
System.out.println("\n The total including tax for your "+room.get(input-1)+" "+fix.format(price*tax*one.getarea()));
}
else if((input>2 && input<5) &&(range.equals("mid")||range.equals("Mid")||range .equals("MID")))
{
price=base*1.4;
System.out.println("\n The price per sqr foot for "+room.get(input-1)+" is "+price);

System.out.println("\n The total including before tax will be "+fix.format(price*one.getarea()));
System.out.println("\n The total including tax for your "+room.get(input-1)+" "+fix.format(price*tax*one.getarea()));
}
else if((input>2 && input<5) &&(range.equals("high")||range.equals("High")||ran ge.equals("HIGH")))
{
price=base*1.8;
System.out.println("\n The price per sqr foot for "+room.get(input-1)+" is "+price);

System.out.println("\n The total including before tax will be "+fix.format(price*one.getarea()));
System.out.println("\n The total including tax for your "+room.get(input-1)+" "+fix.format(price*tax*one.getarea()));
}
else if((input==5)&&(range.equals("low")||range.equals( "Low")||range.equals("LOW")))
{
price=base+3;
System.out.println("\n The price per sqr foot for "+room.get(input-1)+" is "+price);

System.out.println("\n The total including before tax will be "+fix.format(price*one.getarea()));
System.out.println("\n The total including tax for your "+room.get(input-1)+" "+fix.format(price*tax*one.getarea()));
}
else if((input==5) &&(range.equals("mid")||range.equals("Mid")||range .equals("MID")))
{
price=base*1.5;
System.out.println("\n The price per sqr foot for "+room.get(input-1)+" is "+price);

System.out.println("\n The total including before tax will be "+fix.format(price*one.getarea()));
System.out.println("\n The total including tax for your "+room.get(input-1)+" "+fix.format(price*tax*one.getarea()));
}
else if((input==5)&& (range.equals("high")||range.equals("High")||range .equals("HIGH")))
{
price=base*1.9;
System.out.println("\n The price per sqr foot for your "+room.get(input-1)+" is "+price);

System.out.println("\n The total including before tax will be "+fix.format(price*one.getarea()));
System.out.println("\n The total including tax for your"+room.get(input-1)+" "+fix.format(price*tax*one.getarea()));
}
else
{
count=count-1;
}

total+=price*one.getarea()*tax;
count=count+1;
System.out.println("\n ***Would you like to do another room, enter Y if you do***");
System.out.println(" ----------------------------------------------------------");
in=keyboard.nextLine();
repeat=in.charAt(0);

//System.out.println(price);

11. Senior Member
Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
7
Please use code tags when posting code
Last edited by al_Marshy_1981; 12-12-2010 at 04:15 AM. Reason: ...

12. Senior Member
Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
7
money? use BigDecimal, im convinced that is what gary meant.

13. Member
Join Date
Dec 2010
Posts
7
Rep Power
0
I mean after the whole loop runs I would like to save the room they entered and the cost each time so I can print out each one at the end, how do I save this each time the do/while loop run and print them all out latter?

do{// this way we can do as many rooms as you want

//Scanner keyboard = new Scanner (System.in);put these here but they did not work right because I needed them after do while
//DecimalFormat fix = new DecimalFormat("#.00");

System.out.println("\t \n Hello! Welcome to Your Reno Estmeter"+"\n\t*********************************** ***");//this is what I did for a living for about 15 years so I should know A few things about it.
System.out.println("\nPlease choose a Room in your house you would like to Reno.");

ArrayList room = new ArrayList();//I like the idea of an Arraylist so I thought I would use one, sure this could be done many different ways.
room.add(" Bathroom");//thinking of using class to make these rooms come to life still not sure all that I can do with classes but we will see

System.out.println("\n *****1"+room.get(0)+"} 2"+room.get(1)+"} 3"+room.get(2)+"} 4"+room.get(3)+"} 5"+room.get(4)+"}****");//output with each room from arraylist
System.out.println("----------------------------------------------------------------------------------------------------");
System.out.println("\nPlease enter the number on the left of the name to give me your choice");//using numbers to make it easier in my mind anyways.
input= keyboard.nextInt ();

while (input <1 || input>5)//while statement to stop unwanted input works great
{
System.out.println("\nSorry that is not one of the rooms in this planner "+
System.out.println("1"+room.get(0)+" 2"+room.get(1)+" 3"+room.get(2)+" 4"+room.get(3)+" 5"+room.get(4));
input= keyboard.nextInt ();
}
//System.out.println(room.get(input-1)); put this in as a test of the print do not need it now
System.out.println(" Good! Who does not want a new"+room.get(input-1)+"?");
System.out.println("\nOk let's get some info so we can get you some prices :)");

roombuilder one= new roombuilder();//build of the rooms size and height in roombuilder class

System.out.println("\n A good place to start is with the size of your room.\n Please give me the length of your "+room.get(input-1)+" in feet and inches, for example, 10.6 feet");
in1=keyboard.nextDouble();
one.setlength(in1);
System.out.println();

System.out.println(" Ok now we need the width, please enter a width, following the same format");
in2=keyboard.nextDouble();
one.setwidth(in2);

System.out.println("It would be very helpful if we could have the height of you ceilings too, so please enter your height");
in3=keyboard.nextDouble();
one.setheight(in3);

System.out.println("\n Ok, so we have a length of "+fix.format(one.getlength())+" and we have a width of "+fix.format(one.getwidth())+" this gives us an area of "
+fix.format(one.getarea())+" and our height is "+one.getheight());
System.out.println();
keyboard.nextLine();

System.out.println("Please enter your budget range: low, mid, high ");//this became big pain trying to get the data to work out right

range=keyboard.nextLine();

if((input<=2) && (range.equalsIgnoreCase("low")))//wow this saves time from all this other stuff I did should have used method here but did not want to erase after solved problems
{

price= base+5;
System.out.println("\n The price per sqr foot for "+room.get(input-1)+" is "+price);

System.out.println("\n The total including before tax will be "+fix.format(price*one.getarea()));
System.out.println("\n The total including tax for your "+room.get(input-1)+" "+fix.format(price*tax*one.getarea()));
}

else if((input<=2) && (range.equals("mid")||range.equals("Mid")||range.e quals("MID")))
{
price=base*1.5;
System.out.println("\n The price per sqr foot for "+room.get(input-1)+" is "+price);

System.out.println("\n The total including before tax will be "+fix.format(price*one.getarea()));
System.out.println("\n The total including tax for your "+room.get(input-1)+" "+fix.format(price*tax*one.getarea()));
}
else if((input<=2)&& (range.equals("high")||range.equals("High")||range .equals("HIGH")))

{

price=base*2;
System.out.println("\n The price per sqr foot for "+room.get(input-1)+" is "+price);

System.out.println("\n The total including before tax will be "+fix.format(price*one.getarea()));
System.out.println("\n The total including tax for your "+room.get(input-1)+" "+fix.format(price*tax*one.getarea()));
}
else if((input>2 && input<5) &&(range.equals("low") || range.equals("Low")|| range.equals("LOW")))
{
price=base;
System.out.println("\n The price per sqr foot for "+room.get(input-1)+" is "+price);

System.out.println("\n The total including before tax will be "+fix.format(price*one.getarea()));
System.out.println("\n The total including tax for your "+room.get(input-1)+" "+fix.format(price*tax*one.getarea()));
}
else if((input>2 && input<5) &&(range.equals("mid")||range.equals("Mid")||range .equals("MID")))
{
price=base*1.4;
System.out.println("\n The price per sqr foot for "+room.get(input-1)+" is "+price);

System.out.println("\n The total including before tax will be "+fix.format(price*one.getarea()));
System.out.println("\n The total including tax for your "+room.get(input-1)+" "+fix.format(price*tax*one.getarea()));
}
else if((input>2 && input<5) &&(range.equals("high")||range.equals("High")||ran ge.equals("HIGH")))
{
price=base*1.8;
System.out.println("\n The price per sqr foot for "+room.get(input-1)+" is "+price);

System.out.println("\n The total including before tax will be "+fix.format(price*one.getarea()));
System.out.println("\n The total including tax for your "+room.get(input-1)+" "+fix.format(price*tax*one.getarea()));
}
else if((input==5)&&(range.equals("low")||range.equals( "Low")||range.equals("LOW")))
{
price=base+3;
System.out.println("\n The price per sqr foot for "+room.get(input-1)+" is "+price);

System.out.println("\n The total including before tax will be "+fix.format(price*one.getarea()));
System.out.println("\n The total including tax for your "+room.get(input-1)+" "+fix.format(price*tax*one.getarea()));
}
else if((input==5) &&(range.equals("mid")||range.equals("Mid")||range .equals("MID")))
{
price=base*1.5;
System.out.println("\n The price per sqr foot for "+room.get(input-1)+" is "+price);

System.out.println("\n The total including before tax will be "+fix.format(price*one.getarea()));
System.out.println("\n The total including tax for your "+room.get(input-1)+" "+fix.format(price*tax*one.getarea()));
}
else if((input==5)&& (range.equals("high")||range.equals("High")||range .equals("HIGH")))
{
price=base*1.9;
System.out.println("\n The price per sqr foot for your "+room.get(input-1)+" is "+price);

System.out.println("\n The total including before tax will be "+fix.format(price*one.getarea()));
System.out.println("\n The total including tax for your"+room.get(input-1)+" "+fix.format(price*tax*one.getarea()));
}
else
{
count=count-1;
}

total+=price*one.getarea()*tax;
count=count+1;
System.out.println("\n ***Would you like to do another room, enter Y if you do***");
System.out.println(" ----------------------------------------------------------");
in=keyboard.nextLine();
repeat=in.charAt(0);

//System.out.println(price);

14. Senior Member
Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
7
please use code tags, hint... wrap code up in this

[code]

[/cod] but you know an 'e' after 'd'

15. Senior Member
Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
7
I am not trying to be annoying but personally I am not going to read your code because its format is so poor when others make the effort and also I do not have time to dicipher your code and your poor indentation on top of that, understand? most will feel the same so I am trying to help you get the most and best answers around.
Last edited by al_Marshy_1981; 12-12-2010 at 04:40 AM. Reason: ...

16. Senior Member
Join Date
Mar 2010
Posts
952
Rep Power
7
It's clear you are just getting started with Java. And that's great -- we were all there once. It's also very good that you have an idea of a project in mind, as that will help keep you motivated, and help to make things concrete as you learn them. But don't be in a hurry. That will only make things take longer. Take the time to learn things step-by-step. There are no shortcuts.

You should work through at least the Basic Trail of the Java Tutorials.

The Java™ Tutorials

Then you should work through all of Mark Dexter's tutorials:

Eclipse and Java for Total Beginners
Using the Eclipse Workbench
Introducing Persistence
Using the Debugger

Actually, it might not be a bad approach to start with the Mark Dexter tutorials, then do the Oracle Java Tutorials, then do the Mark Dexter tutorials again. The Mark Dexter tutorials are step-by-step work-alongs, and you will definitely learn a lot very quickly by working through them.

-Gary-

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•