gpt4 book ai didi

java 不匹配或永无止境的程序

转载 作者:行者123 更新时间:2023-11-30 04:49:13 25 4
gpt4 key购买 nike

我的代码应该从 .txt 文件读取输入,然后执行不同的操作对其进行排序。第一个整数是长度。我的主要问题是我无法弄清楚为什么我不断收到不匹配错误,或者如果我放入“垃圾”语句,程序将永远无法完成。我将首先发布 .txt,然后发布程序。

15
Smith, John
26
Baker
Jones, Susan
15
Student
Mouse, Mickey
31
Theme park employee
Mouse, Mighty
48
Cartoon super hero
Anderson, William
35
Computer Programmer
Parker, Cindy
18
Author
McCain, John
20
Student
Armstrong, Michelle
17
Student
Thompson, Anne
29
Doctor
Li, Steve
15
Student
James, Tanya
20
Student
Moore, James
32
Teacher
Andrews, Julie
75
Actress
Obama, Michelle
46
Lawyer
Michaels, Todd
51
Student

//别忘了复制末尾的空行。

程序从这里开始。

import java.util.Scanner;
import java.io.*;
public class SortAndDisplayCustomerData
{
public int length; //The length of the names, ages, and occupations arrays
public String[] names;
public int[] ages;
public String[] occupations;
public int count; //The length of the studentNames and studentAges arrays
public String[] studentNames;
public int[] studentAges;
public int i, minPos, Temp2, y, minVal;
public String Temp, Temp3, temp2, minVal2;

public void getDataFromFile()
{
Scanner keyboard = new Scanner(System.in);
Scanner inputStream = null;
System.out.println("wtf");
try
{
inputStream = new Scanner(new FileInputStream("NameAgeOcc.txt"));
}

catch(FileNotFoundException error)
{
System.out.println("Unable to open input file.");
System.exit(0);
}
System.out.println("wtf2");
length=inputStream.nextInt();
//String junk = keyboard.nextLine();
System.out.println("wtf3");
names = new String[length];
ages = new int[length];
occupations = new String[length];
for(i=0;i<length;i++)
{
names[i]=inputStream.nextLine();
ages[i]=inputStream.nextInt();
occupations[i]=inputStream.nextLine();
}
inputStream.close();
}

public void displayAllFileData()
{
System.out.println("wtf3");
System.out.printf("%-25s%-8s%24s%n","Names"," Ages"," Occupations");
for(i=0;i<length;i++)
{
System.out.printf("%-25s%6d%-24s%n",names[i],ages[i],occupations[i]);
}
}
public void sortAllDataByAge()
{
for(i=0;i<length;i++)
{
minVal=ages[i];
minPos=i;
for(y=i+1;y<length;y++)
{
if(ages[y]<minVal)
{
minVal=ages[y];
minPos=y;
}
}
Temp2 = ages[minPos];
ages[minPos] = ages[i];
ages[i] = Temp2;
Temp = names[minPos];
names[minPos] = names[i];
names[i] = Temp;
Temp3 = occupations[minPos];
occupations[minPos] = occupations[i];
occupations[i] = Temp3;
}
}
public void extractStudentData()
{
count=0;
for (i=0;i<length;i++)
{
if(occupations[i].equalsIgnoreCase("student"))
count++;
}
int j=0;
studentAges = new int[count];
studentNames = new String[count];
for (i=0;i<length;i++)
{
if(occupations[i].equalsIgnoreCase("student"))
{
studentAges[j]=ages[i];
studentNames[j]=names[i];
j++;
}
}
}
public void displayStudentData()
{
System.out.printf("%n%-25s%-8s%n","Names"," Ages");

for (i=0;i<count;i++)
{
System.out.printf("%-25s%6d%n",studentNames[i],studentAges[i]);
}

}
public void sortStudentDataAlpha()
{
for(i=0;i<count;i++)
{
minVal2=studentNames[i];
minPos=i;
for(y=i+1;y<count;y++)
{
if(studentNames[y].compareToIgnoreCase(minVal2)<0)
{
minVal2=studentNames[y];
minPos=y;
}
}
Temp = studentNames[minPos];
studentNames[minPos] = studentNames[i];
studentNames[i] = Temp;
Temp2 = studentAges[minPos];
studentAges[minPos] = studentAges[i];
studentAges[i] = Temp2;
}
}

}

最佳答案

在 nextInt() 之后,您应该吃掉换行符,否则您将需要一个字符串(用于文本文件中的名称),并且您将得到一个空字符串(换行符)。然后您将再次获得 nextInt(),但文本文件中的指针将位于名称(字符串)上。

例如,您的程序遵循这样的路径:

32 
Teacher
Andrews, Julie
75
Actress
Obama, Michelle

nextInt() -> 32 nextLine() -> nextLine() -> 老师
nextInt() -> Andrews, [类型不匹配]

关于java 不匹配或永无止境的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10238644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com