# Variables

Page 1 of 1

## 7 Replies - 536 Views - Last Post: 28 January 2008 - 11:33 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=41731&amp;s=51ba768b31518be8627bb1046b1130ed&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 soccerizzy26

Reputation: 0
• Posts: 69
• Joined: 23-January 08

# Variables

Posted 25 January 2008 - 11:40 AM

I'm learning about variables right now and my teacher gave us a code to fix and I'm having trouble fixing it. The first code that I post is the original code that my teacher gave me and the second one is the code that I made when trying to fix it, so if you see why my code still isn't working that'd be great because I've never made a code with variables before.

```// PURPOSE: An exercise in tracing a program and understanding
// assignment statements and expressions.
#import java.util.Scanner;
Public class Trace{
public static void main (String[] args){
int one, two, three;
double what;
Scanner scan = new Scanner(System.in);
System.out.print ("Enter two integers: ");
one = scan.nextInt();
two = scan.nextInt();
system.out.print("Enter a floating point number: ");
what = scan.nextDouble();
three = 4 * one + 5 * two;
two = 2 * one;
system.out.println ("one " + two + ":" + three);
one = 46 / 5 * 2 + 19 % 4
three = one + two;
what = (what + 2.5) / 2
System.out.println (what + " is what!");
}

//PURPOSE: An exercise in tracing a program and understanding
// assignment statements and expression.
import java.util.Scanner;
public class trace
{
public static void main (String[] args)
{
int one, two, three
double what;
Scanner scan = new Scanner(system.in);
system.out.print ("Enter two integers: ");
one = scan.nextInt();
two = scan.nextInt();
system.out.print("Enter a floating number: ");
what = scan.nextDouble();
three = 4 * one plus 5 * two;
two = 2 * one;
system.out.println ("one" + two + ":" + three);
one = 46 / 5 * 2 + 19 % 4
three = one + two;
what = (what + 2.5) / 2
system.out.println (what + "is what!");
}
}
```

MOD EDIT: Please use CODE Tags to enclose your code like this

Is This A Good Question/Topic? 0

## Replies To: Variables

### #2 gl3thr0

• D.I.C Regular

Reputation: 18
• Posts: 309
• Joined: 27-October 07

## Re: Variables

Posted 25 January 2008 - 12:26 PM

okay,,, well wt uve written is kinda full of mistakes..so ill list them for u nd u can fix them

1) when u post code on this site use the [] with the word code in side them and then format ur code so its readable

2) u have forgotten a whole bunch of semi colens ( ; )

3) when u use the term system u have to capitalize it (System)

4) the line three = 4 * one plus 5 * two; .. thts just a disgrace!! y would u write the word plus !!! (+)

so u have bout 9 errors to fix but they are all pretty simple so it shouldnt take u long if theres still a problem send me an email or jus post another reply
oh and good job on catching some of his errors jus be more careful with the capital letters and the simicolens

### #3 soccerizzy26

Reputation: 0
• Posts: 69
• Joined: 23-January 08

## Re: Variables

Posted 28 January 2008 - 10:57 AM

i tried to fix it and it says that their is an error and the file can not be read but it doesn't say why? if you can find anything wrong with the code can you help.

```//PURPOSE: An exercise in tracing a program and understanding
// assignment statements and expression.
import java.util.Scanner;
public class trace
{
public static void main (String[] args)
{
int one, two, three;
double what;
Scanner scan = new Scanner(system.in);
system.out.print ("Enter two integers: ");
one = scan.nextInt();
two = scan.nextInt();
System.out.print("Enter a floating number: ");
what = scan.nextDouble();
three = 4 * one + 5 * two;
two = 2 * one;
System.out.println ("one" + two + ":" + three);
one = 46 / 5 * 2 + 19 % 4;
three = one + two;
what = (what + 2.5) / 2;
System.out.println (what + "is what!");
}
}

```

This post has been edited by soccerizzy26: 28 January 2008 - 10:59 AM

### #4 GWatt

Reputation: 257
• Posts: 3,035
• Joined: 01-December 05

## Re: Variables

Posted 28 January 2008 - 10:59 AM

Did it say that there was an uncaught IOException?

### #5 soccerizzy26

Reputation: 0
• Posts: 69
• Joined: 23-January 08

## Re: Variables

Posted 28 January 2008 - 11:02 AM

right now when I try to compile it it says for line 4 '{' expected and line 24 '}' expected and im not sure what to do with them because i thought they were in the right place

### #6 GWatt

Reputation: 257
• Posts: 3,035
• Joined: 01-December 05

## Re: Variables

Posted 28 January 2008 - 11:12 AM

It doesn't do that for me, so I think it might be an encoding issue. Make sure the file is a UTF-8 or Unicode file. I'm going to assume you're using Windows, and so I suggest you copy your code into notepad, save it as trace.java and then try to compile.

### #7 soccerizzy26

Reputation: 0
• Posts: 69
• Joined: 23-January 08

## Re: Variables

Posted 28 January 2008 - 11:19 AM

well right now im at a school computer and whenever i close a workspace it opens again as an external file and for some reason whenever i try to compile any files it says " Error : Invalid path, \bin\javac.exe -classpath "Z:" -d Z: Z:\Lab" 3.1 " so I can't compile it right now but I'm going to try it at my house, but you're saying when you tried to compile it the program ran fine?

### #8 outlaw107

Reputation: -1
• Posts: 34
• Joined: 01-November 07

## Re: Variables

Posted 28 January 2008 - 11:33 AM

I'm not really fond of how the "Enter Two Integers" is set up......Considering you don't know how the user will react and put them in....maybe they'll press enter after each one...maybe put a comma between the two and cause sytnax errors.....I word it this way.

```import java.util.*;

public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int one, two, three;
double what;

Scanner scan = new Scanner(System.in);

System.out.println("Please Enter the first Integer: ");
one = scan.nextInt();

System.out.println("Please Enter the Second Integer: ");
two = scan.nextInt();

System.out.println("Please Enter a Floating Point Number: ");
what = scan.nextDouble();

// Here are your expressions for the variables that you will output
three = 4 * one + 5 * two;

two = 2 * one;

System.out.println(one + two + ":" + three);

one = 46 / 5 * 2 + 19 % 4;

three = one + two;

what = (what + 2.5) / 2;

System.out.println(what + "Is What!");

}

}

```