小编Sam*_*nch的帖子

如何用Scanner确定一行的结尾?

我的程序中有一个扫描程序,它读取部分文件并将其格式化为HTML.当我正在阅读我的文件时,我需要知道如何让扫描仪知道它在一行的末尾并开始写入下一行.

这是我的代码的相关部分,如果我遗漏了任何内容,请告诉我:

//scanner object to read the input file
Scanner sc = new Scanner(file);

//filewriter object for writing to the output file
FileWriter fWrite = new FileWriter(outFile);

//Reads in the input file 1 word at a time and decides how to
////add it to the output file
while (sc.hasNext() == true)
{
    String tempString = sc.next();
    if (colorMap.containsKey(tempString) == true)
    {
        String word = tempString;
        String color = colorMap.get(word);
        String codeOut = colorize(word, color);
        fWrite.write(codeOut + " ");
    }
    else …
Run Code Online (Sandbox Code Playgroud)

java delimiter java.util.scanner

7
推荐指数
1
解决办法
2万
查看次数

Java - 如何将由空格分隔的整数读入数组

我的项目有问题,因为我无法得到正确的开头,即读取由用户空格分隔的整数行并将值放入数组中.

    System.out.println("Enter the elements separated by spaces: ");
    String input = sc.next();
    StringTokenizer strToken = new StringTokenizer(input);
    int count = strToken.countTokens();
    //Reads in the numbers to the array
    System.out.println("Count: " + count);
    int[] arr = new int[count];

    for(int x = 0;x < count;x++){
        arr[x] = Integer.parseInt((String)strToken.nextElement());
    }
Run Code Online (Sandbox Code Playgroud)

这就是我所拥有的,它似乎只是读取数组中的第一个元素,因为当初始化count时,由于某种原因它被设置为1.

谁能帮我?以不同的方式做这件事会更好吗?

java arrays string integer

6
推荐指数
1
解决办法
4万
查看次数

Java:将两个BigInteger对象划分为一个BigDecimal对象

什么是最简单的方法,使用最少的不必要的开销来划分两个BigInteger对象并将其存储在BigDecimal对象中?

我一直在努力解决这个问题,找不到任何不使用过多转换的东西.

编辑:意识到这个问题有多愚蠢,我会把它留作参考

java biginteger bigdecimal

6
推荐指数
1
解决办法
4611
查看次数

JAVA:二叉树

在这里,我正在尝试制作二叉树,以便我可以使用它们进行不同的操作.

import java.util.*;
import java.lang.*;


public class Main {

public static void main(String[] args) {

}
}

//Building Binary Trees
class bTree {

static class Node { //remember to initilize a root

    String value;
    Node left, right;

    Node(String value, Node left, Node right) {
        this.value = value;
        this.left = left;
        this.right = right;
    }
    Node(String value) //THIS IS A SIBLING CONSTRUCTOR
    {
        this(value, null, null);
    }

    Node root = new Node("ROOT");
    Node lefty = new Node("LEFT0");
    Node righty = new …
Run Code Online (Sandbox Code Playgroud)

java binary-tree

5
推荐指数
1
解决办法
1万
查看次数

初始化一个字符串向量,我已经知道用C++填充它的所有值

在我找到你们的帮助之前,我花了一个多小时研究这个.我正在使用visual studio 2012,我刚刚安装了更新2.

我有这个构造函数

Lexer::Lexer(istream &source1, ostream& listing1)
:source(source1), listing(listing1)
{
vector<string> tempVec = { 
    "and", "begin", "boolean", "break", "call", "end", "else", "false", "halt",
        "if", "input", "integer", "is", "loop", "not", "null", "newline", "or", "output", "procedure"
        "return", "then", "true", "var"
};




tokenToStringVector = tempVec;

for (int k= 0; k < tokenToStringVector.size(); k++)
{
    string key = tokenToStringVector[k];
    lexemeToTokenMap[key] = Token(k); // Function-style type cast
}
}
Run Code Online (Sandbox Code Playgroud)

我的教授写了标题,我写了代码.我收到这个错误,我无法弄清楚:

1>c:\users\sam\dropbox\compiler project\lexical analyzer\lexer.cpp(8): error C2552: 'tempVec' : non-aggregates cannot be initialized with initializer list …
Run Code Online (Sandbox Code Playgroud)

c++ vector

5
推荐指数
1
解决办法
2460
查看次数

设计一个比较器来命令单词,使每个单词的最后一个字母是下一个单词的第一个字母?

好的,所以我有一个程序,其中包含一个我需要的部分"命令单词,使列表中每个项目的最后一个字母是下一个项目的第一个字母,一种由最后一个和第一个链接在一起的单词链字母".

样品输入是狗,大象,长颈鹿,犀牛,老虎,正确的输出是狗,长颈鹿,大象,老虎,犀牛,而我的输出是老虎,犀牛,狗,长颈鹿,大象.

比较器是这样的:

class linkedSort implements Comparator {
    //will return 1 for a match
    //returns 0 if no match

    public int compare(Object t, Object t1) {
        char[] charArr1 = t.toString().toCharArray();
        char[] charArr2 = t1.toString().toCharArray();

        if (charArr1[charArr1.length - 1] == charArr2[0]) {
            return -1;
        } else {
            return 1;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

任何帮助都会很有帮助!!

java puzzle algorithm words comparator

3
推荐指数
1
解决办法
984
查看次数

Java:构造函数不工作

我正在处理我正在使用的GUI构造函数的问题.它应该是一个tic tac toe游戏的GUI,但我的按钮都没有被创建,我的GUI窗口是空白的.我真的很困惑.我创建了一个TicTacToePanel实例并将其添加到主JFrame中.

class TicTacToePanel extends JPanel implements ActionListener {

public void actionPerformed(ActionEvent e) {
}
//Creates the button array using the TicTacToeCell constructor
private TicTacToeCell[] buttons = new TicTacToeCell[9];
//(6) this constructor sets a 3 by 3 GridLayout manager in the panel
///then creates the 9 buttons in the buttons arrray and adds them to the panel

//As each button is created
///The constructer is passed a row and column position
///The button is placed in both the buttons …
Run Code Online (Sandbox Code Playgroud)

java constructor

2
推荐指数
1
解决办法
3299
查看次数

C++中的数组值不正确

我一直在这工作超过2个小时.我已将我的问题与一段代码隔离开来了.

问题出在我的阵列上.它输出了非常大的负值.我输入数组的值是正确的.我对java很有经验,但这是我的第一个C++程序.

   #include <cstdlib>
   #include <iostream>
   #include <sstream>
   #include <string>

   using namespace std;

   int main(){
    string inputString;
    cin >> inputString;
    cout << inputString << endl;

    int mainArray[10];

    for(int x = 0; x < inputString.length(); x++){
        int valFound = inputString[x]-48; //minus 48 to change from ascii to int
        mainArray[valFound]++;
        cout << mainArray[valFound];
    }

    return 0;
    }
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激,这让我发疯.

c++ arrays visual-studio

0
推荐指数
1
解决办法
1255
查看次数