Welcome to Dream.In.Code
Become a Java Expert!

Join 150,380 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,488 people online right now. Registration is fast and FREE... Join Now!




Variables

 
Reply to this topicStart new topic

Variables

soccerizzy26
25 Jan, 2008 - 10:40 AM
Post #1

D.I.C Head
**

Joined: 23 Jan, 2008
Posts: 65

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.

CODE
// 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
code.gif
User is offlineProfile CardPM
+Quote Post

gl3thr0
RE: Variables
25 Jan, 2008 - 11:26 AM
Post #2

D.I.C Head
**

Joined: 27 Oct, 2007
Posts: 229



Thanked: 5 times
My Contributions
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 tongue.gif
User is online!Profile CardPM
+Quote Post

soccerizzy26
RE: Variables
28 Jan, 2008 - 09:57 AM
Post #3

D.I.C Head
**

Joined: 23 Jan, 2008
Posts: 65

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.

CODE

//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 Jan, 2008 - 09:59 AM
User is offlineProfile CardPM
+Quote Post

GWatt
RE: Variables
28 Jan, 2008 - 09:59 AM
Post #4

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,360



Thanked: 31 times
Dream Kudos: 500
My Contributions
Did it say that there was an uncaught IOException?
User is offlineProfile CardPM
+Quote Post

soccerizzy26
RE: Variables
28 Jan, 2008 - 10:02 AM
Post #5

D.I.C Head
**

Joined: 23 Jan, 2008
Posts: 65

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
User is offlineProfile CardPM
+Quote Post

GWatt
RE: Variables
28 Jan, 2008 - 10:12 AM
Post #6

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,360



Thanked: 31 times
Dream Kudos: 500
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

soccerizzy26
RE: Variables
28 Jan, 2008 - 10:19 AM
Post #7

D.I.C Head
**

Joined: 23 Jan, 2008
Posts: 65

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?
User is offlineProfile CardPM
+Quote Post

outlaw107
RE: Variables
28 Jan, 2008 - 10:33 AM
Post #8

New D.I.C Head
Group Icon

Joined: 1 Nov, 2007
Posts: 31


My Contributions
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.

CODE

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!");
        
        
    }

}

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 03:21PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month