Results 1 to 15 of 15
Like Tree2Likes
  • 1 Post By JosAH
  • 1 Post By Dark

Thread: Mistakes

  1. #1
    benprice is offline Member
    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Default Mistakes

    Hello there. I am new to Java and have started making a programme. I hva eput the code into J creator but it doesnt work,. Are there any mistakes with the code below that I am missing.


    public class one

    public static void main(String args[]) {

    int distancetraveled;
    int fuelconsumed;
    int resultmpg;
    int resultkmpl;
    int units;
    int drivingtype;
    int units;
    int drivingtype;


    System.out.println("How many miles have you traveled?");
    distancetraveled=UserInput.readInt();

    System.out.println("How much fuel have you consumed? Enter in gallons");
    fuelconsumed=UserInput.readInt();

    System.out.println("What unit would you like the results displayed in? for mpg enter 1 for kmpl enter 2");
    units=UserInput.readInt();

    if (units=1){
    resultmpg=distancetraveled/fuelconsumed;
    input='A';
    }

    else if (units=2){
    resultkmpl=distancetraveled/fuelconsumed;
    resultkmpl=resultkmpl*0.425143707
    input='B';
    }

    else {
    input='X';
    }


    switch( input ) {
    case 'A': System.out.println("Your car does " + resultmpg +"MPG");
    break;
    case 'B': System.out.println("Your car does " + resultkmpl +" KMPL");
    break;
    case 'X': System.out.println("Incorrect Data Entered");
    break;

    }


    System.out.println("Please enter your driving type urban = 1, motorway = 2, mixed = 3");
    drivingtype=UserInput.readInt();

    if (units=1 && resultmpg>=57 && drivingtype=1){
    input='C';
    }

    else if (units=1 && resultmpg>=47 && resultmpg<57 && drivingtype=1){
    input='D';
    }

    else if (units=1 && resultmpg <47 && drivingtype=1){
    input='E';
    }

    else if (units=1 && resultmpg>=65 && drivingtype=2){
    input='F';
    }

    else if (units=1 && resultmpg>=55 && resultmpg<65 && drivingtype=2){
    input='G';
    }

    else if (units=1 && resultmpg<55 && drivingtype=2){
    input='H';
    }

    else if (units=1 && resultmpg>=61 && drivingtype=3){
    input='I';
    }

    else if (units=1 && resultmpg>=51 && resultmpg<61 && drivingtype=3){
    input='J';
    }

    else if (units=1 && resultmpg<51 && drivingtype=3){
    input='K';
    }

    else if (units=2 && resultkmpl>=20 && drivingtype=1){
    input='L';
    }

    else if (units=2 && resultkmpl>=17 && resultkmpl<20 && drivingtype=1){
    input='M';
    }

    else if (units=2 && resultkmpl<17 && drivingtype=1){
    input='N';
    }

    else if (units=2 && resultkmpl>=23 && drivingtype=2){
    input='O';
    }

    else if (units=2 && resultkmpl>=20 && resultkmpl<23 && drivingtype=2){
    input='P';
    }

    else if (units=2 && resultkmpl<19 && drivingtype=2){
    input='Q';
    }

    else if (units=2 && resultkmpl>=22 && drivingtype=3){
    input='R';
    }

    else if (units=2 && resultkmpl>=18 && resultkmpl<22&&drivingtype=3){
    input='S';
    }

    else if (units=2 && resultkmpl<18 && drivingtype=3){
    input='T';
    }

    else {
    input='Y';
    }

    switch( input ) {
    case 'C': System.out.println("Your car does " + resultmpg +"miles per gallon and is highly efficent.");
    break;
    case 'D': System.out.println("Your car does " + resultmpg +"miles per gallon and is efficent.");
    break;
    case 'E': System.out.println("Your car does " + resultmpg +"miles per gallon and is highly inefficent.");
    break;
    case 'F': System.out.println("Your car does " + resultmpg +"miles per gallon and is highly efficent.");
    break;
    case 'G': System.out.println("Your car does " + resultmpg +"miles per gallon and is efficent.");
    break;
    case 'H': System.out.println("Your car does " + resultmpg +"miles per gallon and is highly inefficent.");
    break;
    case 'I': System.out.println("Your car does " + resultmpg +"miles per gallon and is highly efficent.");
    break;
    case 'J': System.out.println("Your car does " + resultmpg +"miles per gallon and is efficent.");
    break;
    case 'K': System.out.println("Your car does " + resultmpg +"miles per gallon and is highly inefficent.");
    break;
    case 'L': System.out.println("Your car does " + resultkmpl +"kilometres per litre and is highly efficent.");
    break;
    case 'M': System.out.println("Your car does " + resultkmpl +"kilometres per litre and is efficent.");
    break;
    case 'N': System.out.println("Your car does " + resultkmpl +"kilometres per litre and is highly inefficent.");
    break;
    case 'O': System.out.println("Your car does " + resultkmpl +"kilometres per litre and is highly efficent.");
    break;
    case 'P': System.out.println("Your car does " + resultkmpl +"kilometres per litre and is efficent.");
    break;
    case 'Q': System.out.println("Your car does " + resultkmpl +"kilometres per litre and is highly inefficent.");
    break;
    case 'R': System.out.println("Your car does " + resultkmpl +"kilometres per litre and is highly efficent.");
    break;
    case 'S': System.out.println("Your car does " + resultkmpl +"kilometres per litre and is efficent.");
    break;
    case 'T': System.out.println("Your car does " + resultkmpl +"kilometres per litre and is highly inefficent.");
    break;
    case 'Y': System.out.println("You have entered an incorrect value");
    System.exit(0);

    }


    }


    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,037
    Rep Power
    10

    Default Re: Mistakes

    When posting code, please use the code tags to preserve formatting.

    You're going to have to be more specific than "it doesn't work" - what exactly does it do? Does it compile? Does it throw a runtime error? Does it exhibit some strange behavior? Does it become self-replicating and take over the ship, eventually achieving sentience and leaving you with a moral quandary of measuring your safety against the rights of the new lifeform?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    benprice is offline Member
    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Default Re: Mistakes

    Quote Originally Posted by KevinWorkman View Post
    When posting code, please use the code tags to preserve formatting.

    You're going to have to be more specific than "it doesn't work" - what exactly does it do? Does it compile? Does it throw a runtime error? Does it exhibit some strange behavior? Does it become self-replicating and take over the ship, eventually achieving sentience and leaving you with a moral quandary of measuring your safety against the rights of the new lifeform?
    Hi Kevin. I havent even run it. I just put it into J creator and there are squiggly lines everywhere. I dont think I have set J creator up correctly. I was just hoping someone could have a quick browse throguh it and tell me if there are any glaringly obvious mistakes. Or even put it into J creator and tell me if it works. I think the J creator I have on my computer doesnt work.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default Re: Mistakes

    You have to use the '==' operator if you want to compare two numbers for equality, not one equal sign; variable UserInput isn't defined (as I can see) and I'm sure there are more errors ...

    kind regards,

    Jos
    benprice likes this.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: Mistakes

    @Benprice, if you try to compile it and there are syntax errors then it will spit out lines of errors. These errors are very helpful when you're trying to figure out where things when wrong. Try to compile your program and read your error message, if you are still stumped please post it here.

    Also, I would urge you to take delete your code and paste it back in to code tags. Note, do not just wrap [code][/code] tags around your current code as there will be no formatting. Paste a fresh copy and paste from your JCreator.

    I personally don't touch code unless its posted with formatting as it makes it incredibly more difficult to read your code. Don't be surprised when there are a lot of people here with the same mindset as people are here helping in their spare time and are not being paid to help out.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  6. #6
    benprice is offline Member
    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Default Re: Mistakes

    sorry wrong code entered
    Last edited by benprice; 02-04-2013 at 07:22 PM.

  7. #7
    benprice is offline Member
    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Default Re: Mistakes

    Java Code:
    public class one {
        
      public static void main(String args[]) {
        	
        	int distancetraveled;
    	int fuelconsumed;
    	int resultmpg;
    	int resultkmpl;
    	int units;
    	int drivingtype;
           	int units;
    	int drivingtype;
      
        	
        	System.out.println("How many miles have you traveled?");
        	distancetraveled=UserInput.readInt();
    
    	System.out.println("How much fuel have you consumed? Enter in gallons");
        	fuelconsumed=UserInput.readInt();
    
    	System.out.println("What unit would you like the results displayed in? for mpg enter 1 for kmpl enter 2");
        	units=UserInput.readInt();
    
    	if (units=1){ 
        	resultmpg=distancetraveled/fuelconsumed;
    	input='A';
         	}
    
    	else if (units=2){ 										
        	resultkmpl=distancetraveled/fuelconsumed;
    	resultkmpl=resultkmpl*0.425143707 
    	input='B';			
         	}
    
    	else {
        	input='X'; 
        	}
    	
        	
        	switch( input ) { 
    	case 'A': 	System.out.println("Your car does " + resultmpg +"MPG"); 
    				break; 
    	case 'B': 	System.out.println("Your car does " + resultkmpl +" KMPL");
    				break; 
    	case 'X': 	System.out.println("Incorrect Data Entered");
    				break; 
    				
    				}
    
    
    	System.out.println("Please enter your driving type urban = 1, motorway = 2, mixed = 3");
        	drivingtype=UserInput.readInt();
    
    	if (units=1 && resultmpg>=57 && drivingtype=1){ 
    	input='C';
         	}
    	
    	else if (units=1 && resultmpg>=47 && resultmpg<57 && drivingtype=1){ 
    	input='D';
         	}
    
    	else if (units=1 && resultmpg <47 && drivingtype=1){ 
    	input='E';
         	}
    
    	else if (units=1 && resultmpg>=65 && drivingtype=2){ 
    	input='F';
         	}
    	
    	else if (units=1 && resultmpg>=55 && resultmpg<65 && drivingtype=2){ 
    	input='G';
         	}
    
    	else if (units=1 && resultmpg<55 && drivingtype=2){ 
    	input='H';
         	}
    
    	else if (units=1 && resultmpg>=61 && drivingtype=3){ 
    	input='I';
         	}
    	
    	else if (units=1 && resultmpg>=51 && resultmpg<61 && drivingtype=3){ 
    	input='J';
         	}
    
    	else if (units=1 && resultmpg<51 && drivingtype=3){ 
    	input='K';
         	}
    
    	else if (units=2 && resultkmpl>=20 && drivingtype=1){ 
    	input='L';
         	}
    	
    	else if (units=2 && resultkmpl>=17 && resultkmpl<20 && drivingtype=1){ 
    	input='M';
         	}
    
    	else if (units=2 && resultkmpl<17 && drivingtype=1){ 
    	input='N';
         	}
    
    	else if (units=2 && resultkmpl>=23 && drivingtype=2){ 
    	input='O';
         	}
    	
    	else if (units=2 && resultkmpl>=20 && resultkmpl<23 && drivingtype=2){ 
    	input='P';
         	}
    
    	else if (units=2 && resultkmpl<19 && drivingtype=2){ 
    	input='Q';
         	}
    
    	else if (units=2 && resultkmpl>=22 && drivingtype=3){ 
    	input='R';
         	}
    	
    	else if (units=2 && resultkmpl>=18 && resultkmpl<22&&drivingtype=3){ 
    	input='S';
         	}
    
    	else if (units=2 && resultkmpl<18 && drivingtype=3){ 
    	input='T';
         	}
    
    	else {
        	input='Y'; 
        	}
    
        	switch( input ) { 
    	case 'C': 	System.out.println("Your car does " + resultmpg +"miles per gallon and is highly efficent."); 
    				break; 
    	case 'D': 	System.out.println("Your car does " + resultmpg +"miles per gallon and is efficent.");
    				break; 
    	case 'E': 	System.out.println("Your car does " + resultmpg +"miles per gallon and is highly inefficent.");
    				break; 
    	case 'F': 	System.out.println("Your car does " + resultmpg +"miles per gallon and is highly efficent.");
    				break; 
    	case 'G': 	System.out.println("Your car does " + resultmpg +"miles per gallon and is efficent.");
    				break; 
    	case 'H': 	System.out.println("Your car does " + resultmpg +"miles per gallon and is highly inefficent.");
    				break; 
    	case 'I': 	System.out.println("Your car does " + resultmpg +"miles per gallon and is highly efficent.");
    				break; 
    	case 'J': 	System.out.println("Your car does " + resultmpg +"miles per gallon and is efficent.");
    				break; 
    	case 'K': 	System.out.println("Your car does " + resultmpg +"miles per gallon and is highly inefficent.");
    				break; 
    	case 'L': 	System.out.println("Your car does " + resultkmpl +"kilometres per litre and is highly efficent.");
    				break; 
    	case 'M': 	System.out.println("Your car does " + resultkmpl +"kilometres per litre and is efficent.");
    				break; 
    	case 'N': 	System.out.println("Your car does " + resultkmpl +"kilometres per litre and is highly inefficent.");
    				break; 
    	case 'O': 	System.out.println("Your car does " + resultkmpl +"kilometres per litre and is highly efficent.");
    				break; 
    	case 'P': 	System.out.println("Your car does " + resultkmpl +"kilometres per litre and is efficent.");
    				break; 
    	case 'Q': 	System.out.println("Your car does " + resultkmpl +"kilometres per litre and is highly inefficent.");
    				break; 
    	case 'R': 	System.out.println("Your car does " + resultkmpl +"kilometres per litre and is highly efficent.");
    				break; 
    	case 'S': 	System.out.println("Your car does " + resultkmpl +"kilometres per litre and is efficent.");
    				break; 
    	case 'T': 	System.out.println("Your car does " + resultkmpl +"kilometres per litre and is highly inefficent.");
    				break; 
    	case 'Y': 	System.out.println("You have entered an incorrect value");
    				System.exit(0);
    				
    				}  	
       
    
     }
        
        
    }

  8. #8
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: Mistakes

    JosAH gave you a couple of issues he spotted with a quick glance. Have you tried fixing them in your code yet?

    Also what is your compiler throwing for an error when you try to compile it?
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default Re: Mistakes

    Jos' has told you what's wrong with those if statements.

    ETA: And today I will be pretending to be an echo...
    Last edited by Tolls; 02-04-2013 at 07:28 PM.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    benprice is offline Member
    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Default Re: Mistakes

    --------------------Configuration: one - <Default> - <Default>--------------------
    Error : Invalid path, \bin\javac.exe -classpath "C:\Program Files\Xinox Software\JCreatorV4\MyProjects\one\classes" -d C:\Program" Files\Xinox "Software\JCreatorV4\MyProjects\one\classes @src_one.txt"

    I am about to fix the errors he suggested.

  11. #11
    benprice is offline Member
    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Default Re: Mistakes

    so to cover what Jos said i would chagne:

    else if (units=1 &&

    to

    else if (units==1 &&

  12. #12
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: Mistakes

    Yes, that would be the correct syntax for comparing. Also he mentioned about userInput, do you have userInput defined anywhere? The code you posted here, if it is all of the code (which you should be posting in a Short, Self Contained, Correct Example if possible), does not have userInput defined before it is called.
    benprice likes this.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  13. #13
    benprice is offline Member
    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Default Re: Mistakes

    perfect I have fixed all of those. I have no idea how to define user input. is there an example of this you could show me? sorry I have never used a programming forum before I havent got a clue how to correctly post. appologies.

  14. #14
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: Mistakes

    Well first what is UserInput? What are you trying to make it do? Every object is defined a little different based on what its needed for.

    Defining a variable is simple, such as an integer variable.
    Java Code:
    int i = 0;
    That is a very common declaration used in programs, often found in loops it defines a variable to the type int and to the value of 0.
    All About Java Variables
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default Re: Mistakes

    I would point to the Java tutorials at Oracle, personally.
    Variables (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Posting Permissions

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