<M/> 170 Why so serious? Featured Poster

the only reason I see to add the last else if is if you addd a test on whether it was a negative number or not. are you sure you saved/recompiled your code before trying to run it?

Yes, I have recompiled the code before running it... i guess the professor's program is messed up... :D

I am going to leave this thread open and close tomorrow morning (if there is a response from the teacher claiming he messed up)

But thanks!

<M/> 170 Why so serious? Featured Poster

this may not be very relevant at all but this may help some people later on:
http://windows.microsoft.com/en-us/windows-8/keyboard-shortcuts

<M/> 170 Why so serious? Featured Poster

@mike, lucky... you get snow and i get 32 degree fahrenheit air (with wind) in the morning... i wished it snowed in southern california...

<M/> 170 Why so serious? Featured Poster

so, you enter a 9 and it returns 2, did I get that right?

That is correct except the program the professor uses enters 9 and my little project returns a 2.

if (num == 0)
{
System.out.println("No numbers were entered except 0");
}
else if (num != 0)
{
System.out.println("The number of integers is " + num);
}
else
{
System.out.print(num);
}

Yeah, i am not exactly sure what I was doing there... i think i confused myself on the if...else statement? Do you have a way for me to write it more efficiently or at least in a way where i can solve this problem???

<M/> 170 Why so serious? Featured Poster

This is what I got (there are still calculation errors):

import java.text.DecimalFormat;
import java.util.Scanner;
public class Distance {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
        System.out.print("Enter two points: ");
            double x1 = input.nextDouble();
            double y1 =input.nextDouble();
            double x2 = input.nextDouble();
            double y2 = input.nextDouble();
            double X= Math.pow((x2-x1),2);
            double Y= Math.pow((y2-y1),2);
            double z= X + Y;
    DecimalFormat MYformat = new DecimalFormat(",###.00");
        System.out.println("The distance between the two points is " + MYformat.format(z));
  }
}

Here is one of the error logs i get:

First Difference Occurs at: byte 58, line 1
On Line 1 its column 58
Line 1 Wrong: Enter two points: The distance between the two points is 264,228.56
Line 1 Right: Enter two points: The distance between the two points is 514.03 
You have only a First Line Error

sdiff side by side difference your output versus solution....
Enter two points: The distance between the two points is 264, | Enter two points: The distance between the two points is 514.

Octal Dump Difference
0000060   o   i   n   t   s       i   s       2   6   4   ,   | 0000060   o   i   n   t   s       i   s       5   1   4   .  
0000100   .   5   6  \n                       | 0000100

Octal Dump Your output...
0000000   E   n   t   e   r       t   w   o       p   o   i   n   t   s
0000020   :       T   h   e       d   i   s   t   a   n   c   e       b
0000040   e   t   w   e   e   n       t   h   e       t   w   o …
<M/> 170 Why so serious? Featured Poster

I am not really sure actually where i am getting the wrong input/outputs...

The program automattically inputs a number (9 in this case) but my program returns a 2? I am not sure why....

<M/> 170 Why so serious? Featured Poster

Okay, so this is a similar error to the code before.

So I created this:

import java.util.Scanner;
public class IntegerCount
{
public static void main(String[] args)
{

    Scanner input = new Scanner(System.in);
        System.out.print("Enter an integer, the input ends if it is 0: ");
int num = input.nextInt();  

        if (num == 0)
            {
                System.out.println("No numbers were entered except 0");
            } 
        else if (num != 0)
            {
                System.out.println("The number of integers is " + num);
            }
        else
            {
                System.out.print(num);
            }

}
}

But i begin to notice that when I run the program, it collects random numbers. For example, the professor expects the number 9, my program pulls out a 2? I don't see where i am making that mistake?

<M/> 170 Why so serious? Featured Poster

Thanks :D

<M/> 170 Why so serious? Featured Poster

I was testing some codes out online and realized that a nice feature that daniweb can have is an online compiler? What do you guys think? It would be nice to be able to test codes online, so we don't have to go anywhere else...

<M/> 170 Why so serious? Featured Poster

I guess you have to add this line at the top:
import java.text.DecimalFormat;

... now i feel dumb... but it worked!

Last question, i am kind of lost on this one.
How do you put a comma in between each hundreds place.

Instead of 123456.78
It would be 123,456.78

It is not required for us to do it that way, but I am kind of curious to know how it is done.

<M/> 170 Why so serious? Featured Poster

Granted, here is an 8 pack of beer :D

I wish i had a dollar for every dollar i didn't have

<M/> 170 Why so serious? Featured Poster

Deal, it'll be fun playing baseball :)

A dollar for every dollar you don't have :D

<M/> 170 Why so serious? Featured Poster

Yo momma is so stupid that when she that a movie was rated R she thought it was a pirate movie.

<M/> 170 Why so serious? Featured Poster

Ban alcohol -- Oh wait, we already tried that and it didn't work. Oh well, ban it again, along with tobacco, fast food, french fries, candy, ice cream, etc. because they are all bad for us and can kill us.

I know right, why won't someone just ban these things? (in sarcastic voice)

<M/> 170 Why so serious? Featured Poster

@~s.o.s~, I think i understand that part from another thread of mine and someone (mike) said to use DecimalFormat.

But what about the rest of the code? What do you think about it? Any corrections that you can make?

<M/> 170 Why so serious? Featured Poster

Thanks Mike!

So i tried out what you said, the best that i can, and ended up with this:

import java.util.Scanner;

public class Exercise4_1M
{
public static void main(String[] args)
{
long countPositive = 0; long countNegative = 0;
long count = 0; long total = 0;
DecimalFormat myFormatter = new DecimalFormat("#.0#");

Scanner input = new Scanner(System.in);
System.out.print(
"Enter an integer, the input ends if it is 0: ");
long num = input.nextInt();

while (num != 0) {
if (num > 0)
countPositive++;
else if (num < 0) {
countNegative++;
}
total += num;
count++;

num = input.nextInt();
}

if (count == 0) {
System.out.println("No numbers were entered except 0");
} else {
System.out.println("The number of positives is " + countPositive);
System.out.println("The number of negatives is " + countNegative);
System.out.println("The total is " + total);

System.out.println("Average is " + myFormatter.format(total * 1.0D / count));
}
}
}

But the problem now is that i am getting an error with the decimal format? It just says, error: cannot find symbol-----> DecimalFormat myFormatter = new DecimalFormat("#.0#");

<M/> 170 Why so serious? Featured Poster

On my code, it says that there are 2 mistakes and I can't fix them.

Here is my code:

import java.util.Scanner;

public class Exercise4_1M
{
public static void main(String[] args)
{
int countPositive = 0; int countNegative = 0;
int count = 0; int total = 0;

Scanner input = new Scanner(System.in);
System.out.print(
"Enter an integer, the input ends if it is 0: ");
int num = input.nextInt();

while (num != 0) {
if (num > 0)
countPositive++;
else if (num < 0) {
countNegative++;
}
total += num;
count++;

num = input.nextInt();
}

if (count == 0) {
System.out.println("No numbers were entered except 0");
} else {
System.out.println("The number of positives is " + countPositive);
System.out.println("The number of negatives is " + countNegative);
System.out.println("The total is " + total);
System.out.println("Average is " + total * 1.0D / count);
}
}
}

And this is error log 1:

You got this one wrong test case 2
First Difference Occurs at: byte 119, line 3
On Line 3 its column 15
Line 3 Wrong: The total is 1533672726
Line 3 Right: The total is 10123607318 

sdiff side by side difference your output versus solution....
Enter an integer, the input ends if it is 0: The number of po   Enter an integer, the input ends if it is 0: The number of po
The number of negatives is 2                    The number of negatives is 2
The total is 1533672726                       | The total is 10123607318
Average is 8.071961715789473E7                    | Average …
<M/> 170 Why so serious? Featured Poster

Here is what i got so far.

import java.util.Scanner;


public class Exercise2_6
{
    static double distance(double x1, double y1, double x2, double y2)
                {
                return Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
                }
    public static void main(String[] args)
    {                
        double x2, x1, y2, y1;
        double distance;

                Scanner scan = new Scanner (System.in);

                    System.out.println("Enter the x coordinate for point 1: ");
                    x1 = scan.nextDouble();

                    System.out.println("Enter the y coordinate for point 1: ");
                    y1 = scan.nextDouble();

                    System.out.println("Enter the x coordinate for point 2: ");
                    x2 = scan.nextDouble();

                    System.out.println("Enter the y coordinate for point 2: ");
                    y2 = scan.nextDouble();

                    distance = distance(x1,y1,x2,y2);

                    System.out.println("The distance between the two points is " + distance + " .");

    }

}

Any corrections and how to make decimal point show only 2 digits after?

<M/> 170 Why so serious? Featured Poster

Okay so good news you guys :D I figured it out! Apparently the teacher had an error on his part and said my code is fine :D

Thanks guys, more questions are yet to come :D

<M/> 170 Why so serious? Featured Poster

@james, i have it set up like that now (still is an error).

@Mouche, you mean like this:

import java.util.Scanner;
public class Exercise2_1 {
public static void main(String[] args) {
        System.out.print("Enter a temperature in Celsius: ");
        Scanner input = new Scanner(System.in);
    double celsius = input.nextDouble();
    double fahrenheit = (1.8*celsius) + 32;
        System.out.println("The temperature is: " + fahrenheit + " in Fahrenheit.");
}
}

but how would i make it a floating point number? (guessing...) where do I put "F" in order to make it a floating point?

<M/> 170 Why so serious? Featured Poster

@James, i don't know what the input is, it only shows you the result of what was inputted... :( I did read kal's post and i understand what he and you mean but for some odd reason, i just don't understand my own code :(

import java.util.Scanner;
public class Exercise2_1 {
public static void main(String[] args) {
        System.out.println("Enter a temperature in Celsius: ");
        Scanner input = new Scanner(System.in);
    double celsius = input.nextDouble();
    double fahrenheit = (1.8*celsius) + 32;
        System.out.println("Fahrenheit: " + fahrenheit);
}
}

This was my latest attempt... I kind of feel upset due to the fact that i can't figure this out, espicially with all the time i have spent with java... :(

<M/> 170 Why so serious? Featured Poster

Granted, all his stakes & holdings dropped severly and your now broke.

I wish i was fat.

<M/> 170 Why so serious? Featured Poster

Granted, but you were just caught attempting to not get caught in anything :D

I wish i was a zombie

<M/> 170 Why so serious? Featured Poster

@damle1, no it was real but they just tricked him thinking it was for years ;)

<M/> 170 Why so serious? Featured Poster

Deal, it's a tesla, duh?

a 1000th of a penny for every sentence you say?

cproger commented: :) +0
<M/> 170 Why so serious? Featured Poster

@JamesCherrill- According to my teacher's program, this is the problem:

First Difference Occurs at: byte 33, line 1
On Line 1 its column 33
Line 1 Wrong: Enter a temperature in Celsius: 
Line 1 Right: Enter a temperature in Celsius: The temperature is 397.40000000000003 in Fahrenheit. 

sdiff side by side difference your output versus solution....
Enter a temperature in Celsius:                   | Enter a temperature in Celsius: The temperature is 397.400000
Fahrenheit: 144.77777777777777                    <

Octal Dump Difference
0000040  \n   F   a   h   r   e   n   h   e   i   t   :       | 0000040   T   h   e       t   e   m   p   e   r   a   t   u  
0000060   .   7   7   7   7   7   7   7   7   7   7   7   7   | 0000060   i   s       3   9   7   .   4   0   0   0   0   0  

Octal Dump Your output...
0000000   E   n   t   e   r       a       t   e   m   p   e   r   a   t
0000020   u   r   e       i   n       C   e   l   s   i   u   s   :    
0000040  \n   F   a   h   r   e   n   h   e   i   t   :       1   4   4
0000060   .   7   7   7   7   7   7   7   7   7   7   7   7   7   7  \n
0000100

Octal Dump Solution..
0000000   E   n   t   e   r       a       t   e   m   p   e   r   a   t
0000020   u   r   e       i   n       C   e   l   s   i   u   s   :    
0000040   T   h   e       t   e   m   p   e   r   a   t   u   r   e    
0000060   i   s       3   9   7   .   4   0   0   0   0   0   0   0   0
0000100   0   0   0   0   3       i   n       F   a   h   r   e   n   h
0000120   e   i   t   .  \n
0000125

@kal crazy- i get the same errors.... :(

<M/> 170 Why so serious? Featured Poster

Oh, lines 3-4 were a problem? I didn't think those would be the problem... how would i change the inputs in order to get them to work? Would i just remove their values?

<M/> 170 Why so serious? Featured Poster

Deal, who would say no to that?

A gold prius :D

<M/> 170 Why so serious? Featured Poster

So i changed the code up a little bit, it is better but there are still errors.

Any ideas what is going on now?

import java.util.Scanner;

public class ctof{

     public static void main(String[]args){
        System.out.println("Enter a temperature in Celsius: ");
        Scanner input = new Scanner(System.in);
    double c = input.nextDouble();
    double f = ((9.0/5.0)*(c + 32));
        System.out.println("Fahrenheit: " + f);
     }
}
<M/> 170 Why so serious? Featured Poster

the fox says, "look, a mouse!"

<M/> 170 Why so serious? Featured Poster

Oh okay, i see what you mean but where would i make the change? would i have to change line 6-7?

<M/> 170 Why so serious? Featured Poster

Frozen was impressive (better than lion king) while the new hobbit was a flop... :(

<M/> 170 Why so serious? Featured Poster

Did you read my previous post? I explained why you get precision-related errors, and how to fix them, but you seem to have ignored it.

Sorry about that, I seem to have the missed the point on what you have meant.

<M/> 170 Why so serious? Featured Poster

Deal, i could use a new one.

A donkey

<M/> 170 Why so serious? Featured Poster

I am trying to cut the decimal off the output of this small code:

import java.util.Scanner;
public class Distance {
public static void main(String[] args) {

java.util.Scanner in = new java.util.Scanner( System.in );

            double x1 = in.nextDouble();
            double y1 = in.nextDouble();
            double x2 = in.nextDouble();
            double y2 = in.nextDouble();


double X= Math.pow((x2-x1),2);
double Y= Math.pow((y2-y1),2);


double d = Math.sqrt(X + Y);

    System.out.println("Distance is: "+d);  

}
}

Lets say output is 5192.237545685454687.
I want the output to be 5192.23...

So how do i cut it off by 2 decimal places?

<M/> 170 Why so serious? Featured Poster

Age doesn't equate to maturity, unfortunately.

Yeah, but it decreases the amount of immature people driving... or they can increase the tax on it.

<M/> 170 Why so serious? Featured Poster

I love how he slaps sense into him :D
I saw this on CNN, it was hilarious but seriosly, the drinking age should be increased... (my opinion)

<M/> 170 Why so serious? Featured Poster

Okay, so for another assignment I have to find the distance of two points. I understand how that works but for some odd reason, when the program the professor set up tests it, the numbers are way off.

This is what i have done:

public class Distance { 
public static void main(String[] args) { 
double x1=0,x2=2; 
double y1=2,y2=0; 

double X= Math.pow((x2-x1),2); 
double Y= Math.pow((y2-y1),2); 

double d = Math.sqrt(X + Y); 

System.out.println("Distance is: "+d); 
} 
}

If the program tests with 2 points, my results would be like 2.8284271247461903 but the proper answer the program is expecting is 514.03. What did i miss? Could it be because of this line, double d = Math.sqrt(X + Y);

<M/> 170 Why so serious? Featured Poster

Okay, i still get errors but not i have this:

import java.util.Scanner;
public class Exercise2_1 {
public static void main(String[] args) { 

        float temperature;
    Scanner in = new Scanner(System.in);      

    System.out.println("Enter temp. in Celcius");
    temperature = in.nextInt();

    temperature = (9.0/5.0)*temperature+32;

    System.out.println("Temperatue in Fahrenheit = " + temperature);

} 
} 

This is one of the errors: required: float, found: double, 1 error
And here is another: temperature = (9.0/5.0)*temperature+32; has no precision

<M/> 170 Why so serious? Featured Poster

Well, i may have to email the professor on why i am getting these errors then.

I do agree, i don't see much possible errors but there could be an error with how he set up the program that grades our snippets of code...

<M/> 170 Why so serious? Featured Poster

30% of americans already see it as an insult to their religions.

I guess that kind of is true...

<M/> 170 Why so serious? Featured Poster

A little too early but welcome!

<M/> 170 Why so serious? Featured Poster

That code (with a Scanner declaration for kbinput, aqnd placed inside a method) compiles without any errors or warning in NetBeans, and gives the right answers when executed. Maybe the problem is not in that code but caused by something in the rest of the class?

Oh, i see. I suspected that kbinput may not work... What do you recommend as the best approach in solving this problem?

This is my latest attempt:

float temperature;
Scanner in = new Scanner(System.in);      

System.out.println("Enter temp. in Celcius");
temperature = in.nextInt();

temperature = ( 9.0/5)*temperature + 32;

System.out.println("Temperatue in Fahrenheit = " + temperature);

Also, in the error log, it says that it keeps on finding doubles... i don't see how it is possible because i only used floats within this code? It also says it can't find the symbols for line 2, Scanner in = new Scanner(System.in);

<M/> 170 Why so serious? Featured Poster

Hi guys,
I am working on an assignment for a class of mine (Java) and I am really frustrated with it.

This was my very last attempt (out of like 20)...

    System.out.print( "Enter temperature in Celsius to convert to Fahrenheit: " );
double c = kbinput.nextInt();

double f = ( 9.0/5)*c + 32;

System.out.println( "The temperature in Fahrenheit is: " + f + " degrees. " );

I don't get any specific errors from his "grading compiler"... it only says possible errors (1) found...

<M/> 170 Why so serious? Featured Poster

Ehhhhh... might as well, i guess

<M/> 170 Why so serious? Featured Poster

Granted, you were ditched on an abandoned island with no food...

i wish i was never ruined

<M/> 170 Why so serious? Featured Poster

Yo momma is so stupid, she failed a survey

<M/> 170 Why so serious? Featured Poster

Just lemon & water ;D

<M/> 170 Why so serious? Featured Poster

All creatures have a fear of death.

Then i must be a strange creature if that is the case :D

<M/> 170 Why so serious? Featured Poster

Sure, why not?

How about a quadrocopter