Tag: console

Java:为什么这段代码不起作用? 无限循环?

因此,您可以从我的尝试中得知,我正在试图弄清楚我如何创建一个程序,让用户在5秒内输入一些文本行,然后扫描程序将计算输入的行数。 我刚刚开始学习Java作为我的第二语言,所以请尽可能简单地解释一切:) 我有两个理论说明为什么它不起作用。 第一个是nextLine()将返回整行,无论它是否为空,而不是NL等于“”,它实际上将等于整行(即“”)。 我的第二个理论是,我不知道我在做什么,程序流程到处都是。 无论如何,这是我的代码: class OrigClass{ public static void main(String args[]){ Scanner ScanObj = new Scanner(System.in); int Count = 0; String NL = ScanObj.nextLine(); try{ Thread.sleep(5000);} catch (InterruptedException e){ e.printStackTrace(); } while (!NL.equals(“”)){ Count++; NL = ScanObj.nextLine(); } System.out.print(“You Entered ” + Count + ” Lines.”); ScanObj.close(); } } 哦,我忘了提到hasNext()是我最初的尝试: import java.util.Scanner; class […]

使用JTextArea模拟文本控制台

我的目标是在Java中获得类似控制台的行为组件,不一定在JTextArea中,但这首先尝试是合乎逻辑的。 输出很简单,使用JTextArea提供的方法,但输入是另一回事。 我想拦截输入,并按行动 – 逐个字符。 我发现了一些关于使用DocumentListener进行模糊关联的示例,但它似乎不允许我轻松检查刚刚输入的内容,这就是我需要决定如何对其进行操作的内容。 我正确地谈到这个吗? 有更好的方法吗? 我附上我的应用程序代码的相关部分。 public class MyFrame extends JFrame { public MyFrame() { Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize=new Dimension((int)(screenSize.width/2),(int)(screenSize.height/2)); int x=(int)(frameSize.width/2); int y=(int)(frameSize.height/2); setBounds(x,y,frameSize.width,frameSize.height); console = new JTextArea(“”,25,80); console.setLineWrap(true); console.setFont(new Font(“Monospaced”,Font.PLAIN,15)); console.setBackground(Color.BLACK); console.setForeground(Color.LIGHT_GRAY); console.getDocument().addDocumentListener(new MyDocumentListener()); this.add(console); } JTextArea console; } class MyDocumentListener implements DocumentListener { public void insertUpdate(DocumentEvent e) { textChanged(“inserted […]

控制台输入和输出的JUnit测试

我只有一个方法主要。 如何检查System.out.println()并使用JUnit替换Scanner自动输入值? PS请提供一些解决方案…… public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int[] arr = new int[4]; for (int i = 0; i < arr.length; i++) { arr[i] = scanner.nextInt(); } for (int i = 0; i < arr.length; i++) { int res = 0; int k = 0; int num = arr[i]; […]

IntelliJ IDEA控制台中的unicode字符显示为问号

我正在尝试使用System.out编写unicode字符(♠),而是打印一个问号。 如何显示正确的unicode字符而不是问号? 我在Windows上使用IntelliJ IDEA,并尝试在IDE中打印。

将控制台输出重定向到JavaFX TextArea?

我想在JavaFX TextArea中显示控制台输出…遗憾的是我找不到JavaFX的任何工作示例,但仅针对Java Swing,这在我的情况下似乎不起作用。 编辑: 我试着效仿这个例子: http : //unserializableone.blogspot.ch/2009/01/redirecting-systemout-and-systemerr-to.html 并扩展了我的代码,如下所示。 但是,我的Eclipse IDE中没有控制台输出,但TextArea中也没有输出。 知道我做错了吗? public class Activity extends OutputStream implements Initializable { @FXML public static TextArea taRecentActivity; public Activity() { // TODO Auto-generated constructor stub } @Override public void initialize(URL location, ResourceBundle resources) { OutputStream out = new OutputStream() { @Override public void write(int b) throws IOException […]

如何设置IOConsole的Caret

我正在写一个eclipse-plugin,它创建了一个新的Console。 请参阅我的源代码: CliConsoleFactory.java import java.io.IOException; import org.eclipse.jface.text.DocumentEvent; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IDocumentListener; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.PartInitException; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.console.ConsolePlugin; import org.eclipse.ui.console.IConsole; import org.eclipse.ui.console.IConsoleConstants; import org.eclipse.ui.console.IConsoleFactory; import org.eclipse.ui.console.IConsoleView; import org.eclipse.ui.console.IOConsoleOutputStream; public class CliConsoleFactory implements IConsoleFactory { private static final String ENTER_KEY = “\r\n”; private static final String CLI_PROMPT = “CLI> “; private IConsoleView m_consoleView = null; […]

System.console()在NetBeans中提供NullPointerException

我是Java的新手。 我有以下问题:方法readLine()或nextLine() , nextInt()等抛出exception: NullPointerException 。 我使用NetBeans IDE(如果重要的话)。 public static void Reading() { String qq; qq = System.console().readLine(); System.console().printf(qq); }

如何使用Java代码中的Scala varargs

有很多关于从Scala代码调用Java varargs的文章,但我唯一能找到相反的方法就是这个问题: 在java中使用scala vararg方法 ,它没有任何具体的例子。 我试图从一些Java代码中使用scala.Console ,因为java.io.Console在Eclipse中不起作用,而Scala则起作用。 但我无法得到这种方法 def readLine (text: String, args: Any*): String 工作,因为它似乎期待第二个参数的scala.collection.Seq[Any] ,我不知道如何在Java中创建Seq 。 我该如何解决这个问题? 我尝试过的事情: 1)使用null // Java String s = scala.Console.readLine(“Enter text: “, null); – 获得NullPointerException奖励。 2)用scala.collection.Seq.empty() )替换null ,但是javac会报告各种错误,例如Seq没有empty方法。 3)在scala.collection.immutable包对象中使用Nil对象,但这里建议的语法是scala.collection.immutable.package$Nil$.MODULE$ ,但是无法解析。 当然我可以使用不带varargs的readLine()方法,但这太容易了。

JTextArea作为控制台

我在下面发布了两段代码。 两个代码都可以单独使用。 现在,当我运行Easy文件,然后单击“开始”按钮时,我希望实现类AddNumber。 我的意思是说,而不是在控制台上运行的AddNumber,有没有什么方法可以让我在单击“开始”按钮后在第一个类中创建的JTextArea中运行AddNumber? 我想也许是动作听众?(我们按钮的方式)但我不确定。 有没有其他方法可以让我的JTextArea充当其他.java文件的控制台? import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Easy extends JFrame{ JTextArea text=new JTextArea(); JPanel panel=new JPanel(new GridLayout(2,2)); JButton button1 =new JButton(“Start”); public Easy(){ panel.add(text); panel.add(button1); add(panel,BorderLayout.CENTER); button1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae){ //add code to call the other class and make the JTextArea act as a console […]

如何在JFrame / JPanel中可视化控制台java

我使用Swing库创建了一个Java程序。 现在我想将我的控制台输出重定向到JFrame或JPanel。