循环标记器和记事本显示 [英] Looping tokenizer and notepad display

查看:109
本文介绍了循环标记器和记事本显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的任务是检索一个文件并对其进行分类并将其显示在另一个文件中。姓氏名,然后等级。

我因为错误而无法进入循环

java.util.NoSuchElementException

我改变当前存在,而我循环我有。我也有一个显示结果的问题。我显示的结果都在一行,我不能让其发生。这里是我的代码到目前为止:

$ p $ import java.util。*;
import java.util.StringTokenizer;
import java.io. *;
import javax.swing。*;
import java.text.DecimalFormat;

$ b class Grade
{
public static void main(String [] args)throws IOException

{
//声明
String line =;
StringTokenizer st;
String delim =\t\\\
\r, - ;
字符串标记;
字符串名字;
字符串姓氏;
弦乐等级;
String S69andbelow =69或以下的学生;
String S70to79 =70到79岁的学生;
字符串S80to89 =80到89岁的学生;
String S90to100 =90到100的学生;
int gradeint;
double gradeavg = 0;
int count = 0;
File inputFile = new File(input.txt);
File outputFile = new File(output.txt);
FileInputStream finput = new FileInputStream(inputFile);
FileOutputStream foutput = new FileOutputStream(outputFile);

FileReader reader = new FileReader(inputFile);
BufferedReader in = new BufferedReader(reader);

Scanner std = new Scanner(new File(input.txt));

扫描仪扫描仪=新扫描仪(inputFile);

BufferedWriter out = new BufferedWriter(new FileWriter(outputFile));

扫描仪扫描=新扫描仪(S69andbelow);
//读取linev
line = scanner.nextLine();

st = new StringTokenizer(line,delim);
//避免选定的字符
尝试
{
while(st.hasMoreTokens())
{



firstname = st.nextToken(); //< ----错误开始发生
lastname = st.nextToken(); // here
grade = st.nextToken(); //在这里
//将令牌存储到它们的属性中
$ b $ gradeint = Integer.parseInt(grade);
//将令牌转换为int

gradeavg = gradeavg + gradeint;
//计算平均值

count ++;
//记录条目数
$ b $ if(gradeint <= 69)
{
S69andbelow = S69andbelow + lastname +
+ firstname + +\ t+ grade +\\\
;
} //按等级保存数据

else if(gradeint> = 70& gradeint< = 79)
{
S70to79 = S70to79 + lastname ++ firstname
++\ t+ grade +\\\
;
} //按等级保存数据

else if(gradeint> = 80&& gradeint< = 89)
{
S80to89 = S80to89 + lastname ++ firstname
++\ t+ grade +\\\
;
} //按等级保存数据

else
{
S90to100 = S90to100 + lastname ++ firstname
++\ t+等级+\ n;
} //按等级保存数据


} // end while

System.out.println(S69andbelow +\\\
+ S70to79 +\ n
+ S80to89 +\ n+ S90to100);
//计分成绩

gradeavg = gradeavg / count;
//计算平均值

DecimalFormat df = new DecimalFormat(#0.00);

out.write(平均等级是:
+ df.format(gradeavg));


System.out.println(平均等级是:
+ df.format(gradeavg));



Writer输出= null;

output = new BufferedWriter(new FileWriter(outputFile));

// scanner.nextLine(S69andbelow);
//output.write(S69 and below +\ n+ S70to79 +\\\

// + S80to89 +\\\
+ S90to100);
// output.close();


$ b catch(Exception e)
{
System.out.println(e.toString());

//关闭流
尝试
{
if(std!= null)
std.close();


$ b catch(Exception e)
{
System.out.println(e.toString());



$ b $ / code $ / pre
$ b $问题有已被回答。最后一点是:
/ * ** * ** * ** * ** * ** *
程序名称:Grade
名称:Dennis Liang
截止日期:2011年3月31日
课程描述:编写一个程序
,从文件中读取
学生的成绩列表。还显示
姓氏,名字,然后等级。
* ** * ** * ** * ** * **** /



import java.util。
import java.util.StringTokenizer;
import java.io。
;
import javax.swing。*;
import java.text.DecimalFormat;

class Grade
{
public static void main(String [] args)throws IOException

  {
//声明
String delim =\t\\\
\r, - ;
字符串标记;
字符串firstName;
字符串姓氏;
弦乐等级;
String S69andbelow =69或以下的学生;
String S70to79 =70到79岁的学生;
字符串S80to89 =80到89岁的学生;
String S90to100 =90到100的学生;
全部字符串;
int gradeint;
double gradeavg = 0;
int count = 0;
int countTwo = 0;
File inputFile = new File(input.txt);
File outputFile = new File(output.txt);
FileInputStream finput = new FileInputStream(inputFile);
FileOutputStream foutput = new FileOutputStream(outputFile);

FileReader reader = new FileReader(inputFile);
BufferedReader in = new BufferedReader(reader);

Scanner std = new Scanner(new File(input.txt));

扫描仪扫描仪=新扫描仪(inputFile);

BufferedWriter out = new BufferedWriter(new FileWriter(outputFile));

扫描仪扫描=新扫描仪(S69andbelow);

try
{
String line,newLine =(String)System.getProperty(line.separator);
//分隔行
while((line = in.readLine())!= null){
//只有格式正确的文件才可以工作
StringTokenizer st =新的StringTokenizer(行,);

if(st.countTokens()== 3)
{
firstName = st.nextToken();
lastName = st.nextToken();
grade = st.nextToken();
gradeint = Integer.parseInt(grade);
//检索令牌
gradeavg = gradeavg + gradeint;
//公式为avg
count ++;
// avg count

//按等级组织
if(gradeint <= 69)
{
S69andbelow = S69andbelow + newLine + lastName +
+ firstName ++\ t+ grade + newLine;

else if(gradeint> = 70& gradeint< = 79)
{
S70to79 = S70to79 + newLine + lastName ++ firstName
++\ t+ grade + newLine;


else if(gradeint> = 80& gradeint< = 89)
{
S80to89 = S80to89 + newLine + lastName + firstName
++\t+ grade + newLine;
}

else
{
S90to100 = S90to100 + newLine + lastName ++ firstName
++\ t+ grade +新队;

$ / code $ / pre


$ {b $ b countTwo ++;
System.out.println(有+ countTwo +person / people+
需要附加信息+\ n);
} //告知用户有多少人有
} //缺少信息或者太多。


  System.out.println(S69 and belowow +\\\
+ S70to79 +\\\

+ S80to89 +\ n+ S90to100);
//计分成绩

gradeavg = gradeavg / count;
//计算平均值

DecimalFormat df = new DecimalFormat(#0.00);
//格式化



System.out.println(平均等级是:
+ df.format(gradeavg));
//显示


Writer输出= null;

output = new BufferedWriter(new FileWriter(outputFile));
//创建文件
output.write(newLine + S69andbelow + newLine + S70to79 + newLine
+ S80to89 + newLine + S90to100);
output.write(平均等级是:
+ df.format(gradeavg));
//输出到文件
output.close();
//保存
$ / code $ / pre
$ b $ p $ b $ catch $(例外e)
{

System.out.println(e.toString());

//关闭流
尝试
{
if(std!= null)
std.close();

 } 
catch(Exception e)
{
System.out.println(e.toString()) ;

$ / code>

}
}

$ b 至于新行,windows需要\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\对于每个操作系统。

  String line,newLine =(String)System.getProperty(line.separator); 
while((line = in.readLine())!= null){
//只有格式正确的文件才能工作
StringTokenizer st = new StringTokenizer(line,) ;
firstName = st.nextToken();
lastName = st.nextToken();
grade = st.nextToken();
gradeint = Integer.parseInt(grade);

gradeavg = gradeavg + gradeint;

count ++;

if(gradeint <= 69)
{
S69andbelow = S69andbelow + lastname +
+ firstname ++\ t+ grade + newLine;
}
else if(gradeint> 70& gradeint< = 79)
{
S70to79 = S70to79 + lastname ++ firstname
++\ t+ grade +\\\
;


else if(gradeint> = 80& gradeint< = 89)
{
S80to89 = S80to89 + lastname ++ firstname
++\ t+ grade + newLine;
}

else
{
S90to100 = S90to100 + lastname ++ firstname
++\ t+ grade + newLine ;
}


}


Our assignment is to retrieve a file and categorize it and display it in another file. Last name first name, then grade.

I am having trouble with getting a loop going because of the error

"java.util.NoSuchElementException"

This only happens when I change the currently existing while I loop I have. I also have a problem of displaying the result. The result I display is all in one line, which I can't let happen. Here is my code so far:

import java.util.*;
import java.util.StringTokenizer;
import java.io.*;
import javax.swing.*;
import java.text.DecimalFormat;


class Grade
{
    public static void main(String [] args)throws IOException

    {
        //declaring     
        String line = "";
        StringTokenizer st;
        String delim = " \t\n\r,-";
        String token;
        String firstname;   
        String lastname;
        String grade;
        String S69andbelow="Students with 69 or below\n";
        String S70to79 ="Students with 70 to 79\n";
        String S80to89= "Students with 80 to 89\n";
        String S90to100= "Students with 90 to 100\n";
        int gradeint;
        double gradeavg = 0;
        int count = 0; 
        File inputFile = new File("input.txt");
      File outputFile = new File("output.txt");
        FileInputStream finput = new FileInputStream(inputFile);
      FileOutputStream foutput = new FileOutputStream(outputFile);

      FileReader reader = new FileReader(inputFile);
      BufferedReader in = new BufferedReader(reader);

        Scanner std = new Scanner(new File("input.txt"));

        Scanner scanner = new Scanner(inputFile);

        BufferedWriter out = new BufferedWriter(new FileWriter(outputFile));

        Scanner scan = new Scanner(S69andbelow);        
        //reading linev
        line = scanner.nextLine();

        st = new StringTokenizer(line, delim);
        //avoiding selected characters
        try
        {
            while(st.hasMoreTokens())
        {



                firstname = st.nextToken(); //<----error started to happen 
                lastname = st.nextToken(); //here
                grade = st.nextToken(); //and here
                //storing tokens into their properties

                gradeint = Integer.parseInt(grade);
                //converting token to int

                gradeavg = gradeavg + gradeint;
                //calculating avg

                count++;
                //recording number of entries

            if (gradeint <=69)
            {
                S69andbelow = S69andbelow + lastname + " " 
                        + firstname + " " + "\t" + grade + "\n";
            }   // saving data by grades    

            else if (gradeint >= 70 && gradeint <= 79)
            {
                S70to79 = S70to79 + lastname + " " + firstname 
                        + " " + "\t" + grade + "\n";
            }   // saving data by grades

            else if (gradeint >= 80 && gradeint <=89)
            {
                S80to89 = S80to89 + lastname + " " + firstname 
                        + " " + "\t" + grade + "\n";
            }   // saving data by grades

            else 
            {
                S90to100 = S90to100 + lastname + " " + firstname
                         + " " + "\t" + grade + "\n";
            }   // saving data by grades


}//end while

            System.out.println(S69andbelow + "\n" + S70to79 + "\n" 
                         + S80to89 + "\n" + S90to100);
                //caterorizing the grades

                gradeavg = gradeavg / count;
                //calculating average

                DecimalFormat df = new DecimalFormat("#0.00");

                out.write("The average grade is: " 
                        + df.format(gradeavg));


            System.out.println("The average grade is: " 
                        + df.format(gradeavg));



            Writer output = null;            

            output = new BufferedWriter(new FileWriter(outputFile));

            //  scanner.nextLine(S69andbelow);
            //output.write(S69andbelow + "\n" + S70to79 + "\n" 
        //               + S80to89 + "\n" + S90to100);
        //  output.close();


}
        catch( Exception e )
        {  
                System.out.println(e.toString() );
         }
        // Close the stream
        try
        {
            if(std != null )
               std.close( );


                                }
        catch( Exception e )
        {
             System.out.println(e.toString());
         }

}
}

Question has been answered. The final touches is as presented: /******************* Program Name: Grade Name: Dennis Liang Due Date: 3/31/11 Program Description: Write a program which reads from a file a list of students with their Grade. Also display last name, first name, then grade. ********************/

import java.util.; import java.util.StringTokenizer; import java.io.; import javax.swing.*; import java.text.DecimalFormat;

class Grade { public static void main(String [] args)throws IOException

{
    //declaring     
    String delim = " \t\n\r,-";
    String token;
    String firstName;   
    String lastName;
    String grade;
    String S69andbelow="Students with 69 or below\n";
    String S70to79 ="Students with 70 to 79\n";
    String S80to89= "Students with 80 to 89\n";
    String S90to100= "Students with 90 to 100\n";
    String all;
    int gradeint;
    double gradeavg = 0;
    int count = 0;
    int countTwo = 0; 
    File inputFile = new File("input.txt");
  File outputFile = new File("output.txt");
    FileInputStream finput = new FileInputStream(inputFile);
  FileOutputStream foutput = new FileOutputStream(outputFile);

  FileReader reader = new FileReader(inputFile);
  BufferedReader in = new BufferedReader(reader);

    Scanner std = new Scanner(new File("input.txt"));

    Scanner scanner = new Scanner(inputFile);

    BufferedWriter out = new BufferedWriter(new FileWriter(outputFile));

    Scanner scan = new Scanner(S69andbelow);        

    try
    {
    String line, newLine = (String)System.getProperty("line.separator");
        //seperating lines
    while((line = in.readLine()) != null){
        //Will only work if the file is properly formatted
    StringTokenizer st = new StringTokenizer(line, " ");

    if (st.countTokens() == 3)
        {
        firstName = st.nextToken();     
        lastName = st.nextToken();
        grade = st.nextToken();
        gradeint = Integer.parseInt(grade);
        //retrieving tokens
        gradeavg = gradeavg + gradeint;
        //formula for avg
        count++;
        //avg count

        //organizing by grades
      if (gradeint <=69)
    {
        S69andbelow = S69andbelow + newLine+ lastName + " " 
                + firstName + " " + "\t" + grade + newLine;
    }   
         else if (gradeint >= 70 && gradeint <= 79)
    {
        S70to79 = S70to79 +newLine+  lastName + " " + firstName 
                + " " + "\t" + grade + newLine;
    }   

        else if (gradeint >= 80 && gradeint <=89)
    {
        S80to89 = S80to89 + newLine+ lastName + " " + firstName 
                + " " + "\t" + grade + newLine;
    }   

         else 
    {
        S90to100 = S90to100 + newLine+ lastName + " " + firstName
                 + " " + "\t" + grade + newLine;
    }

} else{ countTwo ++; System.out.println("There is "+ countTwo + " person/people" + " that require additional information" + "\n"); } //informing the user that there a how many people with } //missing information or too much.

            System.out.println(S69andbelow + "\n" + S70to79 + "\n" 
                     + S80to89 + "\n" + S90to100);
            //caterorizing the grades

            gradeavg = gradeavg / count;
            //calculating average

            DecimalFormat df = new DecimalFormat("#0.00");
            //formating



            System.out.println("The average grade is: " 
                    + df.format(gradeavg));
            //displaying


            Writer output = null;            

            output = new BufferedWriter(new FileWriter(outputFile));
            //make file
            output.write(newLine + S69andbelow + newLine + S70to79 + newLine 
                 + S80to89 + newLine + S90to100);
            output.write("The average grade is: " 
                    + df.format(gradeavg));      
            //outputing to file      
            output.close();
            //saving

} catch( Exception e ) {
System.out.println(e.toString() ); } // Close the stream try { if(std != null ) std.close( );

                            }
     catch( Exception e )
    {
         System.out.println(e.toString());
     }

} }

解决方案

You need to check st.hasMoreTokens() before each time you call st.nextToken()

As far as the new line, windows requires \n\0

Alternatively, you can use System.getProperty("line.separator") which gives you the correct newline for each OS.

String line, newLine = (String)System.getProperty("line.separator");
while((line = in.readLine()) != null){
//Will only work if the file is properly formatted
StringTokenizer st = new StringTokenizer(line, " ");
firstName = st.nextToken();
lastName = st.nextToken();
grade = st.nextToken();
gradeint = Integer.parseInt(grade);

gradeavg = gradeavg + gradeint;

count++;

                    if (gradeint <=69)
        {
            S69andbelow = S69andbelow + lastname + " " 
                    + firstname + " " + "\t" + grade + newLine;
        }   
        else if (gradeint >= 70 && gradeint <= 79)
        {
            S70to79 = S70to79 + lastname + " " + firstname 
                    + " " + "\t" + grade + "\n";
        }   

        else if (gradeint >= 80 && gradeint <=89)
        {
            S80to89 = S80to89 + lastname + " " + firstname 
                    + " " + "\t" + grade + newLine;
        }   

        else 
        {
            S90to100 = S90to100 + lastname + " " + firstname
                     + " " + "\t" + grade + newLine;
        }


}

这篇关于循环标记器和记事本显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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