Tag: 输入

如何使用Scanner java重复读取用户输入

我正在尝试为我的程序创建一个简单的菜单来读取用户输入。 这是代码: public void menu() { String command; System.out.println(“To operate with words write: a”); System.out.println(“To operate with products write: b”); System.out.print(“\nGive the command: “); Scanner scanner = new Scanner(System.in); command = scanner.nextLine(); if (command.equals(“a”)) { System.out.println(“\nGood job! You have chosen the word program\n”); System.out.println(“You can only use the following commands: “); System.out.println(“exit – to exit […]

在java中编译错误:找不到符号。

我有这个代码,它采用文本文件并将其转换为字符串,然后将字符串的部分分成arraylist的不同元素。 import java.util.Scanner; import java.io.*; import java.util.ArrayList; public class Grocery{ public Grocery(){ File inFile = new File (“lists.txt”); Scanner input = new Scanner (inFile); String grocery; { grocery = input.nextLine(); } } public void makeSmallerLists(){ String listLine; String line; ArrayList smallList = new ArrayList(); while(input.hasNextLine()){ line = input.nextLine; if(line.equals(“”)){ smallList.add(listLine); } else{ listLine = listLine […]

Java Swing:主类等到JFrame关闭

我需要一些简单的java应用程序的帮助,它使用两个jframe来获取一些输入参数。 这是我的代码草图: //second jframe, called when the button OK of the first frame is clicked public class NewParamJFrame extends JFrame{ … } //first jframe public class StartingJFrame extends JFrame{ private static NewParamJFrame newPFrame = null; private JTextField gnFilePath; private JButton btnOK; public StartingJFrame(){ //.. initComponents(); } private void initComponents(){ btnOK.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ […]

只让用户输入正整数(没有小数或字符串)?

我知道如何要求用户输入正整数,但我不知道如何处理代码以避免输入错误,如十进制或字符串输入。 int seedValue; double angle, gunpowder; System.out.println(“Please enter a positive integer seed value: “); seedValue = input.nextInt(); while (seedValue <= 0) { System.out.println("Please enter a positive integer seed value: "); seedValue = input.nextInt(); } System.out.println("That target is " + threeDec.format(gen.nextDouble() * 1000) + "m away.");

按Enter键触发System.in.read()两次?

出于某种原因,每当我在循环中使用System.in.read()时按ENTER键,代码会立即执行两次。 如果它不在循环中,它将执行一次! 为什么是这样? 这是一个测试它的代码片段: while(true) { System.in.read(); System.out.println(“I am supposed to print once, but I print twice!”); }

使用Java 7 API逐字节读取文件

我有一个2 gb文件,我想用Java读取(实际上是4个2gb文件)。 所以Java 7中有一个新function可以让我一次读取所有字节。 import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; public class reconstructor { public static void main(String[] args) throws IOException { Path p = Paths.get(“test.txt”); for (int i = 0; i < 1; i++) { byte[] b = Files.readAllBytes(p); Files.write(p, b, StandardOpenOption.APPEND); } } } 这是一个愚蠢的程序,它将读取一个预先输入单个字节的文件并连续读取该文件并将其读回的内容附加到同一文件中。 现在显然,RAM不够大,一次读取2gb文件,更不用说其中四个了,所以我想知道是否有任何快速方法,不使用外部库(除非这是唯一的方法)阅读四个逐字节文件,以便RAM不会过载(否则我最终会出现Java堆错误)。

如何在Android中使用DialogFragment进行文本输入?

我试图获取用户输入对话框的值,使用推荐的DialogFragment类,Dialog构造并运行正常,但我不能将EditText参数的值返回到父类,而不会得到Null指针exception。 我的DialogHost类,它构造,返回并将父项链接到其按钮。 package jo.app.co; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.app.DialogFragment; import android.content.DialogInterface; import android.os.Bundle; import android.view.LayoutInflater; public class DialogHost extends DialogFragment { public interface NoticeDialogListener { public void onDialogPositiveClick(DialogFragment dialog); public void onDialogNegativeClick(DialogFragment dialog); } NoticeDialogListener mListener; @override public void onAttach(Activity activity) { super.onAttach(activity); try { mListener = (NoticeDialogListener) activity; } catch (ClassCastException […]

如何让Java扫描程序确认空白输入?

我无法让我的程序响应空输入。 例如,假设我想提示用户输入类型为BigDecimal的货币值以及货币类型。 这是有问题的程序的样子。 public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print(“Enter the amount of money ” + “and specify currency (USD or CNY): “); // initialize variable moneyInput BigDecimal moneyInput; // check if moneyInput is numeric try { moneyInput = input.nextBigDecimal(); } catch (InputMismatchException e){ System.err.println(“InputMismatchException: non-numeric value”); return; } // […]

Java密钥绑定不起作用

我试图在JPanel上用Java创建键绑定。 当我按下“w”按钮时,我想要执行某个动作。 我按照Java教程进行绑定,但actionPerformed方法不执行(即没有文本打印出来)。 以下是我的测试GUI的全部代码,相关部分突出显示: import java.awt.BorderLayout; import java.awt.event.ActionEvent; import javax.swing.AbstractAction; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.KeyStroke; @SuppressWarnings(“serial”) public class Test extends JFrame{ private JPanel panel; public Test(){ super(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(500,500); setLayout(new BorderLayout()); setVisible(true); panel = new JPanel(); // HERE ARE THE KEY BINDINGS panel.getInputMap().put(KeyStroke.getKeyStroke(‘w’),”forward”); panel.getActionMap().put(“forward”, new AbstractAction(){ @Override public void actionPerformed(ActionEvent e){ System.out.println(“test”); } }); […]

Scanner和nextInt丢弃整数

我正在使用Scanner在java中获取用户输入。 当我使用nextInt()并且用户输入“2 5”时,则分配值“2”并丢弃5。 如果我想显示这样的输入是错误怎么办? 我想到的一个解决方案是,我可以使用nextString()而不是nextInt()然后解决问题。 但是,有人可以提出更好的解决方案吗? 我意识到它并没有抛弃空格后的整数,而是将它用于下一个输入。 import java.util.Scanner; class Test{ static Scanner in=new Scanner(System.in); static void trial(){ int k=in.nextInt(); System.out.println(k); System.out.println(k); } public static void main(String[] args){ int k=in.nextInt(); System.out.println(k); System.out.println(k); trial(); } }