2017-01-15 8 views
-1

基本的に、私の問題は、私はそれに5000値を持つテキストファイルがあることです。私は範囲内の値だけを表示する方法を考え出すことになっています(設定された範囲はありませんが、私は1000から1500の間で選択しました。)したがって、これらの値の間のすべての数値を見つけ、 "User "名前" .txt。Javaプログラムは、数値の範囲内のファイルの内容を出力する

主なジレンマは、私はコード内にテキストファイルを置くことができません。コマンドライン引数として扱う必要があります。このタイプは決して扱っていません前に問題の

これは、これまでの私のコードです:。

import java.util.*; 
import java.io.*; 

public class TestProg { 

    public static void main(String[] args) { 
     int LowerBound = 0; 
     int UpperBound = 0; 

     String Name1 = null; 
     String Name2 = null; 

     //instantiates the array to read in the numbers from the file 
     double[] FileNums = new double[5000]; 
     //instantiates the array to output the upper and lower bounded values. 
     double[] OutputNums = new double[5000]; 

     Scanner scanner1 = new Scanner(System.in); 

     System.out.println("This program will output the upper or lower 
          bounds of the text file. 
          Please input which lower bound you would like."); 
     LowerBound = scanner1.nextInt(); 
     System.out.println("Now input the upper bound."); 
     UpperBound = scanner1.nextInt(); 

     Names(Name1, Name2); 
    } 

    public static String Names(String Name1, String Name2) { 
     Scanner scanner2 = new Scanner(System.in); 

     System.out.println("Please input your first name."); 
     Name1 = scanner2.nextLine(); 
     System.out.println("Now input your last name."); 
     Name2 = scanner2.nextLine(); 
    } 
} 

私は基本的に開始する方法で立ち往生しています、私はそれがトンを伝えるために、whileループを伴うだろうと信じています。彼は範囲内の値だけを見つけるようにプログラムしますが、私は完全にはわかりません。

私は別の方法で名前を入力しているので、Name1とName2はnullに戻ります。

ご迷惑をおかけして申し訳ございません。

+2

ようこそ!宿題の助けを求めているようです。それ自体に問題はありませんが、これらのことを守ってください(http://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions/338845#338845)、それに応じて質問を編集してください。 –

+1

コマンドライン引数はメインメソッドの引数 'args'に格納されます。ファイルからテキストを読み取る方法は、Java IOチュートリアルで説明されています。それを読んだことはありますか? –

+0

ソースファイルの例を表示することをお勧めします。あなたはそこに数字があると言いましたが、どのように正確に見えるのですか? –

答えて

0

次のコードを使用して、指定した範囲内の数値を印刷します(最小、最大値)。区切り文字としてスペースを使用してINPUT.TXTに与えられた

File output = new File("output.txt"); 
File file = new File("input.txt"); 
BufferedInputStream bin = new BufferedInputStream(new FileInputStream(
     file)); 
byte[] buffer = new byte[(int) file.length()]; 
bin.read(buffer); 
String fileStr = new String(buffer); 
String[] numbers = fileStr.split("\\s"); 

Scanner s = new Scanner(System.in); 
System.out.println("Enter min number in range"); 
int rangeMin = s.nextInt(); 
System.out.println("Enter max number in range"); 
int rangeMax = s.nextInt(); 

for (String number : numbers) { 
    if (Integer.parseInt(number) >= rangeMin 
      && Integer.parseInt(number) <= rangeMax) { 
     System.out.println(Integer.parseInt(number)); 
    } 
} 

bin.close(); 

入力:スタックオーバーフローへ

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ... 
関連する問題