2016-11-12 98 views
0

我正在嘗試從文本文件中讀取並將其打印出來,而且此程序的工作原理如何,除了在文本文件中的每個空格之後打印出來之外,我希望它將其打印出來在每個逗號之後。從文件獲取輸入

這裏是我的代碼:

package com.filetoscreen; 

import java.awt.Color; 
import java.awt.Font; 
import java.io.File; 
import java.util.Scanner; 

import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.SwingConstants; 

public class readFile { 


    private Scanner x; 

    public void openFile(){ 
     try{ 
      x = new Scanner(new File("input.txt")); 
     } 
     catch(Exception e){ 
      System.out.println("Couldn't find input.txt"); 
     } 
    } 

    public void readFile(){ 
     while(x.hasNext()){ 
      String a = x.next(); 
      System.out.print(a + " "); 


      JFrame frame = new JFrame("Game Name Generator"); 
      frame.setSize(1400, 1200); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.getContentPane().setBackground(Color.black); 
      JLabel label = new JLabel(a + " ", SwingConstants.CENTER); 
      frame.add(label); 
      label.setFont(new Font("SansSerif", Font.BOLD, 75)); 
      label.setForeground(Color.BLUE); 
      frame.setLocationRelativeTo(null); 
      frame.setResizable(true); 
      frame.setVisible(true); 
     } 
    } 

    public void closeFile(){ 
     x.close(); 
    } 
} 

我在另一個類中調用這些,所以我不會打擾把在那裏。

+1

爲什麼你生成每一個新的令牌一個新的JFrame? –

+0

@HovercraftFullOfEels這是他們可以回顧所有的事情,我不知道如何讓它滾動或類似的東西,如果你知道如何,可以告訴我,我很想知道! –

回答

1

設置逗號作爲分隔符的掃描儀:

final Scanner s = new Scanner(new StringReader("a b, c d, ef")); 
    s.useDelimiter(","); 
    while (s.hasNext()) { 
     System.out.println(s.next()); 
    }