Java console: readPassord()- size/length of the password - java-7

Am trying to understand java console class and it's readPassword method. Following is the code i have,
package com.files;
import java.io.Console;
public class NewConsole {
public static void main(String[] args) {
Console c = System.console();
char[] pw=new char[2];
pw = c.readPassword("%s", "pw: ");
System.out.println(pw);
}
}
pw is char array of size 2. But when executing the above program if i enter pw as "abcd" in cmd(> than the array size of pw) it works fine. Why is it not throwing index out bound of exception as the size of input exceeds the size of pw?.

watch below code that you can figure out whats going on here you are assigning new char sequence to pw[]
import java.io.Console;
public class NewConsole {
public static void main(String[] args) {
Console c = System.console();
char[] pw=new char[2];
char [] nw = "hello".toCharArray();
System.out.println(pw.length);
pw = nw;
System.out.println(pw.length);
}
}
and also if you want to throw ArrayIndexOutOfBoundsException then try
import java.io.Console;
public class NewConsole {
public static void main(String[] args) {
Console c = System.console();
char[] pw=new char[2];
char [] str = c.readPassword("%s", "pw: ");
for(int i=0;i<str.length;i++)
pw [i] = str[i];
System.out.println(pw);
}
}
;-) if you find whats going on then please close question

Related

for loop iterated unexpectedly

I am a newbie to Java programming and is trying to self-learn the language. I want to create a program that will terminate when a 's' is typed, but what confuse me is my for loop is iterated twice after a letter is entered?
My code:
public class Demo {
public static void main(String[] args)
throws java.io.IOException{
int i;
System.out.println("Press s to stop: ");
for(i = 0; (char) System.in.read() != 's'; i++)
{
System.out.println("Pass #"+i);
}
}
My result:
How should I solve this problem?
Well it is because when you press the enter key the \n also gets into the inputstream.
Thus one iteration is for the letter the other for the \n character.
A Solution would be:
public class Demo {
public static void main(String[] args)
throws java.io.IOException{
int i;
System.out.println("Press s to stop: ");
char c = System.in.read();
for(i = 0; c != 's'; i++)
{
if(c == '\n') continue;
System.out.println("Pass #"+i);
c= System.in.read();
}
}

I am trying to print the method findAverage in the main method, can anyone tell me how to fix

public class Grade {
private int [] array = {2,3,1,4,5,7,1};
public int findSum() {
int sum;
sum = 0;
for(int i =0; i <array.length; i++)
{
sum = sum +array[i];
}
return sum;
}
public double findAverage() {
double average;
average = findSum()/array.length;
return average;
}
}
class ExamClient {
public static void main(String[] args) {
double answer;
answer = findAverage();
System.out.println("Average of all elements in the array is" + answer);
}
}
In the main you have to create a instance of the class
Create instance
public static void main(String[] args)
{
double answer;
Grade g= new Grade();
answer = g.findAverage();
System.out.println("Average of all elements in the array is" + answer);
}
Also you can make the method static

Why is my code constantly giving the same random numbers

Whenever I execute the program it always prints out 9 and 5 for sc1 and sc2. It was my understanding that the random class was supposed to be mostly random.
Here is my code:
public class BlackJack {
public static BlackJack blackjack;
public int chips;
public static int[] deck;
public static int ct = 0, sc1, sc2;
Random random;
public BlackJack() {
deck();
deal();
System.out.println(sc1);
System.out.println(sc2);
}
public void deck() {
deck = new int[52];
for (int i = 0; i < 52; i++) {
if(i % 4 == 0) {
ct++;
}
deck[i] = ct;
}
}
public void deal() {
random = new Random(52);
sc1 = deck[random.nextInt(52)];
sc2 = deck[random.nextInt(52)];
}
public static void main(String[] args) {
blackjack = new BlackJack();
}
}
Thanks in advance for any guidance.
Never mind. i'm dumb. It was the fact that i had random = new Random(52); instead of random = new Random();

multiple times character entry using bufferedreader in java

I am trying to enter a letter and a number. 1st input going fine but 2nd input its not taking rather going to the end of line stating not a digit. Please help.
public class charString {
public static void main(String args[]) throws IOException {
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
System.out.print("Enter a character: ");
char p=(char)(in.read());
if(Character.isLetter(p))
System.out.println(p+" is a letter");
else
System.out.println(p+" is not a letter");
System.out.print("Enter a character: ");
char p1=(char)(in.read());
if(Character.isDigit(p))
System.out.println(p1+" is a digit");
else
System.out.println(p1+" is not a digit");
}
}
try this
public static void main(String args[]) throws IOException {
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
System.out.print("Enter a character: ");
String character = in.next();
char p = character.charAt(0);
characterChecker(p);
}
private void characterChecker(Char p) {
if(Character.isLetter(p)) {
System.out.println(p+" is a letter");
} else if (Character.isDigit(p)) {
System.out.println(p1+" is a digit");
}
}
EDIT You can also check out
Character.isLetterOrDigit(charAt(p))
hope this helps ..

Not getting result when ran the Nashorn program

I am new to Nashorn, I am trying to write one program and try to ran that program but i am getting the result after ran the program.Please find my code is below.
package com.nashron;
import java.io.FileNotFoundException;
import java.io.FileReader;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class InvokScriptObjectMethod {
public static void main(String[] args){
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("nashorn");
try {
engine.eval(new FileReader("src/script/Script.js"));
} catch (ScriptException | FileNotFoundException ex) {
}
}
}
JS:
var Script = Java.type("com.nashron.Script");
var var1 = new Script("who am i");
return var1.get("I am Amar");
Java :
package com.nashron;
public class Script {
public Script() {
}
public Script(String arg1) {
this.var1 = arg1;
System.out.println("this is contructor");
}
private String var1;
public String get(String arg1) {
System.out.println("this is return statement");
return this.var1 + arg1;
}
}
here I want to get the return value.
Thanks in Advance
A top level script cannot have "return" statements. This is as per ECMAScript specification. Your program will result in ScriptException being thrown - as there is a return statement in your JS code (at top level). If you just remove return, the last evaluated expression is returned from engine.eval call.
Example:
File: Main.java
import javax.script.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
ScriptEngineManager m = new ScriptEngineManager();
ScriptEngine e = m.getEngineByName("nashorn");
Object val = e.eval(new FileReader("x.js"));
System.out.println(val);
}
}
File: x.js
java.lang.System.getProperty("os.name");

Resources