因此,我最近在FileScanner中使用多个分隔符时遇到了困难。我想这样做,因为我的文件中的第一个字符串是用逗号分隔的,但是在它转到下一行之前,没有逗号,只有一个空格。这给将值赋值给我的数组带来了问题。
public class FileScan
{
public static void main(String[] args) throws IOException
{
String[] firstName = new String[50];
String[] lastName = ne
可能重复:
为什么当我从filereader打印我的列表时,它是myServiceOrder@3bc1cac,myServiceOrder@32fe621e,myServiceOrder@5adbb9b9,myServiceOrder@f7e4f49,myServiceOrder@2d874991,myServiceOrder@ceee5f1,myServiceOrder@183a37d9
public class myServiceOrder implements ServiceOrder, Comparable<myServiceOrder>{
private in
我需要通过getWord()获得要返回的字符串randomWord
private static void setUpDictionary() throws IOException
{
Scanner fileScan;
String[] words = new String[25];
fileScan = new Scanner (new File("dictionary.dat"));
int n=0;
while (fileScan.hasNext())
{
words[n] = fileSca
下面的代码在jgrasp中编译,但它读取You null null。我不知道如何让我的文本文件读取并存储到它们的数组中?
import java.io.*;
import java.util.Scanner;
import java.util.Random;
public class InsultGenerator {
//randomly picks an adjective and a noun from a list of 10 random nouns and adjectives
//it then creates a random insult using one adject
编辑:愚蠢的错误。nextLine是解决方案。再次感谢@mellamokb
试图从Java书中运行一个示例,但我得到了一个编译错误。守则如下:
public static void main (String [] args) throws IOException
{
String url;
Scanner fileScan, urlScan;
fileScan = new Scanner(new File("websites.inp"));
// Read and process each line of the file
whil
我正在尝试将一个包含100个数字的文本文档转换为一个一维,然后是一个2D (所以是两个单独的代码)数组。我想知道是否有人能让我走上正确的方向,我非常困惑,需要帮助!这是我到目前为止所拥有的;
import java.util.Scanner;
import java.io.*;
public class IntegerSorter
{
/**
* Reads text from a file and prints it in uppercase.
*/
public static void main (String[] args) thro
制作一个小型的绞刑架游戏,只是为了好玩,并尝试更多的IO。最初的解决方案如代码所示,它在IDE中工作(使用Netbeans)。问题是,当我将它导出为一个Jar文件时,它找不到我要引用的文件。导出后如何正确地从外部文件中读取?
public HangmanGame(){
File file = new File("src\\hangman\\words");
try{
Scanner fileScan = new Scanner(file);
int number = (int)(Math.random()*260);
for(doubl
我一直试图逐个比较Array中的对象(如果它的属性是这样的话),这样我就可以将Array中的对象按降序排序。下面是示例代码:数组是Candidate[][]
System.out.println("How many positions for this election? > ");
numberOfPositions = sc.nextInt();
Candidate Candidate[][] = new Candidate[numberOfPositions][];
PoliticalParty Parties[][] = new P
我正在尝试编写一个GUI程序,它将导入一个文本文件,读取其内容并将其放入ArrayList中。
我在一个单独的.java文件中编写面板,其中包含导入按钮并执行导入函数,而Arraylist位于不同的.java文件(主要方法文件)上。
如何使ActionListener从不同的类/..java文件中读取文件内容并将其导入ArrayList?
我的“导入”对话框会导致错误,有什么问题吗?代码在CLI中工作得很好,但在GUI程序中却不行。
这是GUI框架的主要方法,它将从不同的.java文件中添加面板:
public class FrameGUI
{
public static void
我想用.txt文件中的行来初始化字符串数组。我写了这样的东西: public static void main(String[] args) {
File file = new File("file.txt");
int linesNumber = 0;
String str;
try {
Scanner fileScan = new Scanner(file);
//counting lines number in the file
由于某些原因,编译器看不到fileScan扫描器。我正在传递它,这样我就可以打开并读取文件,然后读取它的行。我不确定问题是什么--我该如何解决这个问题?提前感谢!
public static void main (String[] args)
{
int weaponNumber = 0;
Scanner scan = new Scanner(System.in);
Scanner fileScan = new Scanner(FILENAME);
while (fileScan.hasNextInt())
{
我有一个.txt文档,其中包含一个单词列表,每行一个单词。一共有十行。如何阅读此文档,从中随机选取一行,并显示该单词?
这是我到目前为止所拥有的代码:
Scanner fileScan, wordScan;
String word;
Random rand = new Random();
int pickedLine = rand.nextInt(6)+1;
fileScan = new Scanner(new File("list.txt"));
while (fileScan.hasNext()) {
w
我有一个程序,可以读取一个文本文件,里面包含了足球队的进球数,我希望能够扫描文本文件,了解某支球队的情况,然后计算出目标,得分,监狱,等等。
public void totalGames()
{
File inputfile = new File ("validtest.txt.txt");
try{
Scanner filescan = new Scanner(inputfile);
//Map<String[], int[]> teams = new Map<&
public static void main (String[] args)
{
int weaponNumber;
Scanner scan = new Scanner(System.in);
Scanner fileScan = new Scanner(FILENAME);
String line;
while (fileScan.hasNextLine())
{
line = fileScan.nextLine();
Scanner lineScan = new Sca
我正在做一个为电影创建虚拟数据库的项目。我有两个类: MovieEntry (用于单个电影条目)和MovieDatabase (包含数据库并允许添加等的较大类)。我得到的错误是“无法找到Symbol - Method Add”,但我在顶部声明了一个ArrayList。这就是我的全部代码,我知道还有其他错误。
**public class MovieDatabase
{
public ArrayList<String> Database = new ArrayList<String>();
public MovieDatabase(){
Arr
解出
数组中文本文件的第一行是学生人数。我需要扫描这个数字,使我的数组大小的数字,然后打印出每一行的学生和他们相应的信息从文本文件。
我已经复习了好几次这个逻辑,但似乎找不到它的错误之处。我可以编译这个方法,但是当我运行它时,我会得到一个空指针异常,这意味着当我将数组分配给学生信息时,我肯定做错了什么。
我现在的代码是:
import java.io.*;
import java.util.Scanner;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throw
使用文件和链表。我的代码从文件中读取两个数字,并将其添加到“链接列表”中。当它显示数字时,它要求用户输入一个双精度数字。在输出中,它不会将键入的数字添加到链表中。不确定如何将这些数字添加到链接列表中,因此我可以操作它,例如(添加/删除)。
public class LinkList
{
public static void main(String [] args)
{
linkedListNumbers numbers= new linkedListNumbers();
numbers.newLinkList();
numbers.addElementT
public static String[][] possibleOutcomes(Scanner fileScan,int weaponNumber)
{
int numberOfOutcomes = (int)Math.pow(weaponNumber,2);
String[][] outcomes = new String[numberOfOutcomes][numberOfOutcomes];
String line = fileScan.nextLine();
Scanner lineScan = new Scanner(line