what does should i changed the stdin into with? - bufferedreader

I need help. I'm still a beginner in Java, so my knowledge is still weak.
There's a lot of tries that I did, I still don't know why my programming build fails. Can you guys help me?
package act2i;
import java.io.*;
public class Act2I {
public static void main(String[] args) throws IOException {
BufferedReader inData = new BufferedReader (new
InputStreamReader(System.in));
//declare variable
String str;
System.out.println("Enter the data: ");
str = stdin.readLine();//read input that is entered by user
//display an output, str value
System.out.println("You have entered: " + str);
}//end main()
}//end class

You should change "stdin" to "inData".

Related

JAVA Regex Character recognition

I am using p{L} to accept chinese alphabet in our code, i tested this in eclipse and the system showed success result whereas when the same was used in Oracle Jdeveloper, system is throwing a failure. This is the code snippet, i am using.
Please provide me an answer.
public static final String VALID_CHARACTER_PATTERN = "[\\p{L}0-9_*]*";
public static final boolean hasRestrictedChars(String suspectedString) {
return !suspectedString.matches(VALID_CHARACTER_PATTERN);
}
public static void main(String[] args) {
boolean check = hasRestrictedChars("你好");
if (check)
System.out.println("InValid String");
else {
System.out.println("Perfect");
}
}

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

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

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

IO streams to JPanel, GUI with InputStream and OutputStream in a JPanels JTextField and JTextArea

I'm trying to write a GUI where the user sees the System output in a JTextArea and where he writes the input in a JTextField, both object inside a JPanel.
How do I do to connect the System output stream to the JTextArea and the System input stream to the JTextField? I have googled and searched these forums but havnt found the solution. I would be very happy if someone could help me with this.
I have a Master class that calls the JPanel with the GUI, and I will have work executed in different threads later on, but right now I struggle with the basic issue of connecting IO streams to the JPanel. Down below is the 2 classes:
public class MainTest {
public static void main(String[] args) throws IOException {
JPanelOUT testpanel = new JPanelOUT();
JFrame frame = new JFrame();
frame.add(testpanel);
frame.setVisible(true);
frame.pack();
/*
System.setOut(CONVERT TEXTAREA TO AN OUTPUTSTREAM SOMEHOW??(JPanelOUT.textArea)));
System.setIn(CONVERT STRING TO AN INPUTSTREAM SOMEHOW?? JPanelOUT.textField);
*/
String text = Sreadinput();
System.out.println(text);
}
public static String Sreadinput() throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(JPanelOUT.is));
String input=in.readLine();
return input;
}
}
public class JPanelOUT extends JPanel implements ActionListener {
protected static JTextField textField;
protected static JTextArea textArea;
public static InputStream is;
private final static String newline = "\n";
public JPanelOUT() throws UnsupportedEncodingException, FileNotFoundException {
super(new GridBagLayout());
JLabel label1 = new JLabel("OUTPUT:");;
JLabel label2 = new JLabel("INPUT:");;
textField = new JTextField(20);
textField.addActionListener(this);
textArea = new JTextArea(10, 20);
textArea.setEditable(false);
textArea.setBackground(Color.black);
textArea.setForeground(Color.white);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setPreferredSize(new Dimension(500,200));
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
add(label1, c);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
add(scrollPane, c);
c.weightx = 0;
c.weighty = 0;
c.fill = GridBagConstraints.HORIZONTAL;
add(label2, c);
c.fill = GridBagConstraints.HORIZONTAL;
add(textField, c);
String WelcomeText1 = "Hello and welcome to the TEST";
String WelcomeText2 = "Trying to get the input field below to become the System.in and this output";
String WelcomeText3 = "field to become the System.out (preferrably both with UTF-8 encoding where";
String WelcomeText4 = "the scrollpane automatically scrolls down to the last output line)!";
textArea.append(WelcomeText1 + newline + newline + WelcomeText2 + newline + WelcomeText3 + newline + WelcomeText4 + newline + newline);
String text = textField.getText();
is =new ByteArrayInputStream(text.getBytes("UTF-8"));
}
public void actionPerformed(ActionEvent evt) {
String text2 = textField.getText();
textArea.append(text2 + newline);
textField.selectAll();
textArea.setCaretPosition(textArea.getDocument().getLength());
}
}
i am new to java, trying to deal with the streams, too :)
Sorry for bad English I am from Russia.
May be this code will help you.
public class NewJFrame extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
public MyPrintStream myPrintStream;
public NewJFrame()throws FileNotFoundException{
initComponents();
this.myPrintStream = new MyPrintStream("string");
}
private class MyPrintStream extends PrintStream {
MyPrintStream(String str)throws FileNotFoundException{
super(str);
}
public void println(String s){
textArea1.append(s+'\n');
}
} .. continuation class code
Main method:
public static void main(String args[]){
/* Set the Nimbus look and feel... */
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run(){
try{
NewJFrame myJFrame = new NewJFrame();
myJFrame.setVisible(true);
System.setOut(myJFrame.myPrintStream);
System.out.println("its work");
System.out.println("its work2");
System.out.print("str"); //does not work, need to override
}catch (FileNotFoundException e){System.out.println (e.getMessage());}
}
});

Resources