r/javahelp • • Oct 30 '18

Encoding a 4 digit number

Hello, so I need help understanding the error in my javascript here. It's supposed to take a 4 digit number and switch the 1st with the 3rd digit as well as the 2nd with the 4th. Here is the code. I think the error has something to do with how I set up the document, but I'm not quite sure, it looks fine to me.

I'll open in chrome, Firefox or Edge, all that will happen is that it shows the title at the top of the browser tab and that's it. I'm using the newest version notepad++

<?xml version = "1.0" encoding = "utf-8"?>

<!DOCTYPE html>

	<head>
		<title>Encryption</title>
		<script type="text/javascript">
		import java.util.Scanner; //Needed for Scanner class

public class LNFI_LNFI_program2 { public static void main(String[] args) { getnum(); //Calls for getnum() method

}
public static int getnum() { int fourD = 0, stringSize = 0; String fourDs; //To hold the value of input number in a String

Scanner kb = new Scanner(System.in);
while (stringSize != 4)
{
  System.out.println("Enter a four digit number"); //Prompt user for four digit number
  fourDs = kb.nextLine();
  stringSize = fourDs.length(); //Checks length of String
   
  if (stringSize == 4)
  {
     
    fourD = Integer.parseInt(fourDs);

  }
}
encrypt(fourD);
return fourD; //Return value of input
}

public static int encrypt(int num) { int a, b, c, d;

a = num / 1000;
b = num / 100 % 10;
c = num / 10 % 10;
d = num % 10;
 
a = (a + 7)%10;
b = (b + 7)%10;
c = (c + 7)%10;
d = (d + 7)%10;
 
System.out.println(c+d+a+b);
return (c+d+a+b); //Returns encrypted value
		</script>
	</head>
	<body></body>
</html>
2 Upvotes

4 comments sorted by

View all comments

8

u/tsvk Oct 30 '18

Java and JavaScript are different programming languages.

Web pages use JavaScript.

The syntax of the code that you have embedded in your web page is Java. It will not work in a web page.

2

u/nutrecht Lead Software Engineer / EU / 20+ YXP Oct 30 '18

That's a first for me :D