Question:
What do these things do in Java?
buckeyefan1191
2007-08-17 07:14:12 UTC
What does a if selection statement allow a program to do?

What is a syntax error? Give an example

What is a logic error? Give an example.

What is a call to method System.exit required in GUI-based applications?

What is an import declaration and where does it appear in a Java source code file?

Why do programmers inset comments in their code?

Why does a semicolon cause a logic error if placed immediatley after the right parenthesis of an If statment?
Four answers:
angel04
2007-08-17 12:13:25 UTC
Syntax errors -- Errors in spelling and grammar.

1 You can use the compiler or interpreter to uncover syntax errors.

example==>>

public class stringmethod{

public static void main(String[] args){

String string1 = "Hi";

String string2 = new String("Hello")\\ forgot to put semicolon

if (string1 == string2) {

System.out.println("The strings are equal.");

} else {

System.out.println("The strings are unequal.");

}

}

}

2 You must have a good working knowledge of error messages to discover the cause of the error.



Logical errors -- Errors that indicate the logic used when coding the program failed to solve the problem.



1You do not get error messages with logic errors.i.e. computer will not warn you.

2Your only clue to the existence of logic errors is the production of wrong solutions.

Example==>> You want to got to theater. Now there are two ways to reach there way 'A' and way 'B' one is shor cut and one is little long. According to your idea you will pick up way 'A' which is long. you will reach to your destination but your idea is not right. This is logical error.



To be a good programmer this is essential to think in right direction.



System.exit

Terminates the currently running Java Virtual Machine.

a nonzero status code indicates abnormal termination.



It might be thought that a programme that reads in two user entered integers and prints out their sum would be a simple piece of code with little of real interest. This assumption is wrong, once the programmer wishes to ensure that errors are detected and also wants to handle the user input channel in a reasonably general fashion. Java provides all these facilities. Unfortunately for those who like simple "proof-of-concept" programmes, the Java programmer has to do it properly - or not at all.



Here's a Java programme that prompts the user to enter integer values and prints out the sum of the integer values.



public class Addup

{

static public void main(String args[])

{

InputStreamReader stdin =

new InputStreamReader(System.in);

BufferedReader console =

new BufferedReader(stdin);

int i1 = 0,i2 = 0;

String s1,s2;

try

{

System.out.print("Enter first number ");

s1 = console.readLine();

i1 = Integer.parseInt(s1);

System.out.print("Enter second number ");

s2 = console.readLine();

i2 = Integer.parseInt(s2);

}

catch(IOException ioex)

{

System.out.println("Input error");

System.exit(1);

}

catch(NumberFormatException nfex)

{

System.out.println("\"" +

nfex.getMessage() +

"\" is not numeric");

System.exit(1);

}

System.out.println(i1 + " + " +

i2 + " = " + (i1+i2));

System.exit(0);

}

}





The System.exit method forces termination of all threads in the Java virtual machine. This can be drastic. It might, for example, destroy all windows created by the interpreter without giving the user a chance to record or even read their contents.



Programs should usually terminate by stopping all non-daemon threads; in the simplest case of a command-line program, this is as easy as returning from the main method.



System.exit should be reserved for a catastrophic error exit, or for cases when a program is intended for use as a utility in a command script that may depend on the program's exit code.



import :is used to import java packages.

/**util is java package and date is class. Without util date related method will not work and it will give copilation error

that is syntax error*/

/*This comment line shows what work this this part of programme is doing and it will be used for reference pupose*/

import java.util.Date;



public class Message2 {



public static void main(String []args) {



// Make sure the command

// line argument was passed in

if(null == args || args.length < 1) {



// Command line argument

// doesn't exist, so exit with

// an error flag

System.exit(1);

}



// Extract the only command line

// argument from the first element

// of the array

String message = args[0];

String formattedMessage = prependTimestamp(message);

System.out.println(formattedMessage);

}



public static String prependTimestamp(String str) {



Date now = new Date();

String timeStamp = now.toString();

return timeStamp + ": " + str;

}

}



Semicolon if you will not put where

if(x==y) is condition after that statement will come.if you put semicolon it will give you syntax eror its a java syntax rule





you need to learn not only java but real logic and programming from scratch
anonymous
2016-04-01 23:54:48 UTC
Is your computer infected? Malwarebytes should tell you if you have anything. Big name anti-viruses seem to be missing the latest java based infections. Do your own research to make sure malwarebytes is legit. You should always do this before installing something. Can you use java? You'll be ok playing standalone java applications... like minecraft etc. Disable it in your browser though. If you ever come across a site where you must use java you can have a separate browser for that. E.g. if chrome or firefox are your default, disable java on those and use internet explorer for legit sites that will not work at all without java.. and don't use IE for anything else. That way you stay on the safe side and not use java without absolutely needing it. The internet is CRAWLING with java exploits now. Some of them are kind of a pain in the *** to fix. Some of them will really mess your computer up, e.g. encrypting your data. To deal with viruses: If you ever get infected it's good to have a backup of your system on hand. Create system restore points at a minimum, but really you should have an image of your whole system when you know it's 100% clean. And the nuclear option is a hard drive format and fresh windows install. If your computer is unusable due to an FBI warning popup etc. spam F8 while it's booting and choose safe mode. Run malwarebytes or whatever. Try and get your computer into a useable state, as clean as you can get it and then use a backup or restore. Google or a knowledgeable friend can help with these options. The first thing you should be trying to achieve is find out what virus you have and what are the common traits. They can be tricky and can delete system restore points or do other things to prevent you getting rid of them. They require a certain order of actions to get around... or just give up and reinstall windows. To avoid viruses: Don't download or install something if you have not researched whether it's clean and the site you are downloading it from is clean. Don't click any ads or anything else like that. If you get popups that won't go away just CTRL-ALT-DELETE and kill your browser. Don't take chances. Stay away from free or illegal stuff and porn and generally you'll be ok.
Rob C
2007-08-17 07:26:42 UTC
if: allows different code to be executed depending on the current state of the program



syntax error: your program doesn't compile, because you didn't type it correctly



logic error: your program doesn't work, because you tried to do the wrong thing



System.exit(): The program won't ever finish if you don't call this because you still have daemon thread(s) running



Import: found at the top of the file after the package declaration - they allow you to not fully specify the full name of every class you mention



comments: so they (or someone else) can understand what the code is supposed to do, or how a class/method is intended to work



if();: I think it is legal, but not generally useful
cdoublejj
2007-08-17 07:27:42 UTC
The comments next to the code are so other programmers can tell what is being done with the code.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...