扫描仪从文件中每隔一行跳过一次

时间:2015-11-26 05:27:49

标签: java arraylist java.util.scanner

我正在尝试扫描文本文件并将每一行放入一个arraylist,并在下一行是'*'时停止扫描,但是,我的arraylist存储每一行​​,我不知道为什么。< / p>

Scanner scan = new Scanner(new File("src/p/input2.txt"));
ArrayList<String> words = new ArrayList<String>(); 

while(scan.hasNextLine()) { 
    if(scan.nextLine().equals("*"))
        break;
    words.add(scan.nextLine());
}

文本文件:

1

dip
lip
mad
map
maple
may
pad
pip
pod
pop
sap
sip
slice
slick
spice
stick
stock
*
spice stock
may pod

我的arraylist中存储了什么:

  

[dip,mad,maple,pad,pod,sap,slice,spice,stock]

5 个答案:

答案 0 :(得分:2)

你总是读两行(除非你得到*

if(scan.nextLine().equals("*")) // read here - "someString"
   break;
words.add(scan.nextLine());  // ignore last read line and read again.

您只阅读一次然后进行比较。

String value = scan.nextLine();
// check for null / empty here
if (value.equals("*"))
  break;
words.add(value);

答案 1 :(得分:1)

你正在阅读它两次。

  

存放,使用它。

while(scan.hasNextLine()) { 
String str = null;
if((str =scan.nextLine()).equals("*"))
   break;
words.add(str);//here you are not reading again.
}

答案 2 :(得分:1)

试试这个,

readOnly

答案 3 :(得分:1)

每次调用scan.nextLine()时,扫描仪都会移动到下一行。你在循环中调用它两次(第一次检查,第二次添加)。这意味着您检查一行并添加下一行。

解决方案是将其调用一次并将其存储在变量中:

while(scan.hasNextLine()) { 
    String str = scan.nextLine();
    if(str.equals("*"))
        break;
    words.add(str);
}

答案 4 :(得分:1)

问题出在这里:

import random

def loop():

    player_hand = []
    dealer_hand = []
    deck = []
    rank = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
    suit = ['♣', '♦', '♥', '♠']
    for s in suit:
        for r in rank:
            deck.append(r+s)
    random.shuffle(deck)
    deckpos = 0
    first = deck.pop()
    second = deck.pop()
    third = deck.pop()
    fourth = deck.pop()
    player_hand.append(first)
    player_hand.append(second)
    dealer_hand.append(third)
    dealer_hand.append(fourth)

    def draw_card():
        card = deck[deckpos]
        deckpos += 1
        return card

    def player_turn(first, second):

        player_stand = False
        while player_stand != True:

            print ('You hand is [{}, {}]'.format(first, second))
            print ('The dealer is showing a {}'.format(third))

            first = first[:len(first)-1]
            first = conversion(first)
            second = second[:len(second)-1]
            second = conversion(second)
            value_of_hand = int(first + second)

            if value_of_hand <= 21:
                print ('Your value of your hand is {}'.format(value_of_hand))

            else:
                print ("You busted at {}".format(value_of_hand))
                playerturn_results = [22, 0]
                return playerturn_results

            choice = input ('Do you wish to hit or stand? ')

            if 'hit' in choice.lower():
                print('You draw a {}'.format(card))
                card = card[:len(card)-1]
                card = conversion(card)
                value_of_hand = int(value_of_hand + card)
                print ('Your value of your hand is {}'.format(value_of_hand))


    player_turn(first, second)


loop()

因此,不要读两次,只需使用临时变量来存储值,然后在循环中使用该临时变量。

您可以使用以下代码:

while(scan.hasNextLine()) { 
        if(scan.nextLine().equals("*"))
            break;
        words.add(scan.nextLine()); // --> You are reading two time in same loop
    }