打印在java中的列表 [英] Printing a list in java

查看:85
本文介绍了打印在java中的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从文件中读取一个数字数组,并打印出来。我的第一类处理用户在其中输入一个数字,告诉程序连接到该文件,该文件,整数,字符,或双打打印名字的菜单驱动程序。我被困在的第一件事就是连接到文件。这里是我的不完整的类从文件读取:

I need to read a number of arrays from a file and print them. My first class handles a menu driven program where the user enters a number to tell the program to connect to the file, print the names in the file, the integers, the characters, or the doubles. The first thing I am stuck on is connecting to the file. Here is my incomplete class that reads from the file:

import java.util.*;
public class Prog5Methods{
public Prog5Methods(){
}
    public void ReadFromFile(Scanner input, String [] names, int [] numbers, char [] letters, double [] num2){
        System.out.println("\nReading from a file...\n");
        System.out.println("\nDONE\n");
        int r = 0;
            while(input.hasNext()){
                names[r] = input.next();
                numbers[r] = input.nextInt();
                letters[r] = input.next().charAt(0);
                num2[r] = input.nextDouble();
                r++;
        }
} // end of readFromFile




}

这就是我从阅读的文件包括:

This is what the file I am reading from contains:

Lee Keith Austin Kacie Jason Sherri     Jordan     Corey Reginald Brian Taray 
Christopher Randy Henry Jeremy Robert    Joshua   Robert   Eileen 
Cassandra Albert Russell   Ethan   Cameron Tyler Alex Kentrell  rederic
10 20 100 80 25 35 15 10 45 55 200 300 110 120 111 7 27 97 17 37 
21 91 81 71 16 23 33 45
A  b  c w e r t q I u y b G J K S A p o m b v x K F s q w
11.5 29.9 100  200 115.1 33.3 44.4 99.9 100.75 12.2 13.1 20.3 55.5 77.7
12.1 7.1  8.2   9.9   100.1  22.2  66.6 9.9  1.25     3.75   19.9  3.321  45.54 88.8

的名称是数组名[],整数是数字阵列[]等我需要从这些阵列打印每个变量。

The names are in array names[], the integers are in array numbers[], etc. I need to print each variable from these arrays.

推荐答案

使用列表< T> 而不是阵列

阅读使用扫描仪从底层流值

Read values from underlying stream using Scanner

public static void ReadFromFile(Scanner input, 
       ArrayList<String> names, 
       ArrayList<Integer> numbers, 
       ArrayList<Character> letters, 
       ArrayList<Double> num2)
{

  while(input.hasNext())
   {
     String val=input.next();
     Object no=parseInt(val);
     if(no!=null) //Is integer?
      {
          numbers.add((Integer)no);
       }
     else
     {
       no=parseDouble(val);
       if(no!=null)  // Is double?
        {
          num2.add((Double)no);
          }
        else
         {
           no=parseChar(val);
           if(no!=null)  //Is Char?
            {
             letters.add((Character)no);
            }
           else
            {
              names.add(val);  // String
            }
         }
       }
    }  
 }

方法解析字符串。

Methods to parse a string.

  public static Integer parseInt(String str)
    {
       Integer retVal=-1;
       try
         {
           retVal=Integer.parseInt(str);
         }catch(Exception ex) { return null;}
       return retVal;
     }  
   public static Double parseDouble(String str)
    {
       double retVal=-1;
       try
         {
           retVal=Double.parseDouble(str);
         }catch(Exception ex) { return null;}
       return retVal;
     }  
   public static Character parseChar(String str)
    {
       Character retVal=null;

       if(str.length()==1)
          retVal=str.charAt(0);
       return retVal;
     } 

测试你的code

Test your code

   public static void main(String[] args) throws Exception
      { 
         .......
         ArrayList<String> names=new ArrayList<String>();
         ArrayList<Integer> numbers=new ArrayList<Integer>();
         ArrayList<Double> num2=new ArrayList<Double>();
         ArrayList<Character> letters=new ArrayList<Character>();

         ReadFromFile(input,names,numbers,letters,num2);

         System.out.println(names);
         System.out.println(numbers);
         System.out.println(letters);
         System.out.println(num2);
        }

这篇关于打印在java中的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆