Type mismatch after running - java

package skater;
import java.io.*;
import java.util.*;
public class TestSkater
{
public static void main(String[]args) throws IOException
{
Scanner sc = new Scanner(new File("C:/pairs.txt"));
String name1 = null, name2 = null, Country = null;
double [] Technical = new double [8];
double [] Performance = new double[8];
try {
while(sc.hasNext())
{
name1=sc.nextLine();
name2 = sc.nextLine();
Country = sc.nextLine();
for (int t = 0; t<Technical.length; t++)
{
Technical[t] = sc.nextDouble();
}
for(int y = 0; y<Performance.length; y++)
{
Performance[y] = sc.nextDouble();
}
// Skater [] skaters = new Skater[20];
Skater S1 = new Skater(name1,name2,Country,Technical,Performance);
System.out.println(S1);
sc.nextLine();
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
System.out.print("Bad program");
}
}
}
Whenever I run my code without the try and catch I get an InputMismatchException. It's reading from a file that has all the names of the teams with their scores and countries. It's formatted like this:
smith
jones
australia
4.2 5.1 3.8 2.9 5.0 4.6 4.9 4.3
4.9 4.8 5.8 3.8 4.9 4.6 5.0 4.5
lennon
murray
england
2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8
3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8
I know that the nextLine() is telling it to look at the next line of data, And the nextDouble() that I am using for the arrays Performance and Technical, is telling it to look at the next double in line.
I can't figure out why it is giving me this error. I think it's because when it moves onto the first name of the next team it is still seeing it as a double from the array Performance, but I can't figure out how to fix that problem.

It's hard to say for sure without knowing what the Scanner functions (nextLine, nextDouble) are really doing and the exact format of the file you area reading.
Assuming that:
nextLine reads text to the end of line
nextDouble reads and parses text to the next break/space
Then, by the way you are reading the input, I assume it's in the format:
Name1
Name2
Country
0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0
0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0
If the assumptions are correct and that's not the format of the input then there's your problem.
If you are sure that the nextLine text entries are being read fine then an option may be to read the line as a string, split on space (or comma, or semicolon or whatever seperates them) and parse them yourself e.g.
string[] tmp = sc.nextLine().split(' ');
for(int t = 0; t<Technical.length; t++)
Technical[t] = Double.parseDouble(tmp[t]);

Related

Is there a way to find where the error is coming from when printing elements from a LinkeList?

public void Room(){
int i = 0;
int split = numTunnels(tunnels)*tunnelLength(tunnel_length);
LinkedList<Double> room = new LinkedList<>(Creation());
Iterator it = rooms.iterator();
while(i <= split){
System.out.print(it.next()+ " ");
i++;
if(i == split){
System.out.println();
i = 0;
}
}
}
When i run it, i get the output i want plus this error that i don't know what to do about, where is it coming from.
1.0 1.1 1.2 1.3
2.0 2.1 2.2 2.3
3.0 3.1 3.2 3.3
4.0 4.1 4.2 4.3
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.LinkedList$ListItr.next(LinkedList.java:894)
at robot.wars.Colony.Room(Colony.java:45)
at robot.wars.RobotWars.main(RobotWars.java:18)
C:\Users\dioni\AppData\Local\NetBeans\Cache\12.5\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\dioni\AppData\Local\NetBeans\Cache\12.5\executor-snippets\run.xml:68: Java returned: 1
BUILD FAILED (total time: 0 seconds)
Change your while statement to while(it.hasNext()){.
or iterate this way using the implicit iterator of the enhanced for loop.
for (double d : rooms) {
System.out.print(d + " ");
}

How do I fix a NumberFormatException (from text file input)?

I was wondering if I could have some help with this NumberFormatException with code using a text input.
The result should be it being able to run properly and be able to first put 50 strings into the hashTable and then remove 10 afterwards.
I have tried placing the removeLine.next() inside a String datatype and then placing the String back inside the Integer.parseInt which didn't work.
Here is the class:
import java.io.*;
import java.util.*;
public class hashTest {
public static void main(String args[]) throws FileNotFoundException {
HashTable hashTable = new HashTable();
Scanner insert = new Scanner(new File("data1.txt"));
while(insert.hasNext()) {
String line = insert.nextLine();
Scanner insertLine = new Scanner(line);
insertLine.next();
insertLine.next();
int index = Integer.parseInt(insertLine.next());
String data = insertLine.nextLine();
hashTable.put(index, data);
}
Scanner remove = new Scanner(new File("data2.txt"));
while(remove.hasNext()) {
String line = remove.nextLine();
Scanner removeLine = new Scanner(line);
removeLine.next();
removeLine.next();
int index = Integer.parseInt(removeLine.next());
hashTable.remove(index);
}
}
}
data1.txt :
003 : 68682774 MALIK TULLER
004 : 24248685 FRANCE COELLO
005 : 25428367 DUSTY BANNON
006 : 79430806 MELVINA CORNEJO
007 : 98698743 MALIA HOGSTRUM
008 : 20316453 TOMASA POWANDA
009 : 39977566 CHONG MCOWEN
010 : 86770985 DUSTY CONFER
011 : 92800393 LINNIE GILMAN
012 : 31850991 WANETA DEWEES
013 : 81528001 NEAL HOLSTEGE
014 : 46531276 BRADLY BOMBACI
data2.txt :
92800393 LINNIE GILMAN
86770985 DUSTY CONFER
31850991 WANETA DEWEES
46531276 BRADLY BOMBACI
25428367 DUSTY BANNON
68682774 MALIK TULLER
18088219 PENNY JOTBLAD
48235250 KENNITH GRASSMYER
20316453 TOMASA POWANDA
54920021 TYSON COLBETH
22806858 LAVERNE WOLNIK
32244214 SHEMEKA HALLOWAY
81528001 NEAL HOLSTEGE
24248685 FRANCE COELLO
23331143 JUSTIN ADKIN
79430806 MELVINA CORNEJO
59245514 LESLEE PHIFER
64357276 SCOT PARREIRA
50725704 GENARO QUIDER
52298576 AUDIE UNCAPHER
54657809 MARTY ENOCHS
54526749 TOBI HEATLEY
24903965 ALONSO GILSTAD
84936051 DEONNA STRAZZA
62522327 AHMAD THAYER
90572271 ELIJAH METEVIER
88999386 ISMAEL ELKAN
NumberFormatExceptions with Integer.parseInt() are most often caused by attempting to read something into an int that is not actually an int. Try printing each line as it is read in. If you have a line that is not purely an int (e.g., Hello123), you will get this exception with Integer.parseInt(). A cleaner debugging method (and better coding practice) would be to catch the exception and print the problematic line. You will probably see right away what's causing the issue. When reading text input from anywhere, it's never good to assume that the data is of the format you're expecting.
When your input contains data other than the int values you need, you can read each line's values into an array and extract the proper value(s). Here's an example of how you might extract the values from a single line in your second data file. Keep in mind that this still makes assumptions about the input format and therefore, is not completely fool-proof.
try {
// Split the line by whitespace, saving the values into an array
String[] singleLineVals = someLine.split("\\s+");
// Extract the first value
int firstValue = Integer.parseInt(singleLineVals[0]);
} catch (NumberFormatException nfe) {
// Handle the exception
}

Calculating the sum of values from a text file?

im trying to find the sum of the cost of all my books in my libary system from a file, could someone say where im going wrong atm. It would be great help
int sum = 0;
File Fileobject = new File ("E:\\text files for java\\BooksToImport.txt");
try {
Scanner fileReader = new Scanner (Fileobject);
// Read and display each line of the file
while(fileReader.hasNext())
{
sum+= fileReader.nextInt ( );
line = fileReader.nextLine();
System.out.println(line);
System.out.println(sum);
}
fileReader.close();
This is my data im reading:
The_Hunger_Games - Suzanne_Collins - 5 - Scholastic_Press - 9781921988752
OOP_programming - Graham_Winter - 32.50 – Oreilly - 0471974555
Harry_potter - Jk_Rowling - 10 - Bloomsbury- 9788700631625
This is the errors im getting:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at c3500948JavaProjectAssignement1.c3500948.main(c3500948.java:80)
Asumming your file has fixed format Text (no other words has - as seperator) and position for amount value here is something you can try:
text file :
book1- author1- 100 - press1- 978846521988752
book2 - author2 - 100 - press2 - 9788928465887521
book3 - author3 - 100 - press3 - 9784659219887521
book4 - author4 - 100 - press4 - 9788928465887521
book5 - author5 - 100 - press5 - 9784659219887521
book6 - author6- 100 - press 6- 9788984659887521
TestClass.java
import java.util.Scanner;
import java.io.*;
class TestClass
{
public static void main(String [] args) throws Exception
{
FileReader file = new FileReader("test.txt");
Scanner sc = new Scanner(file);
double sum = 0;
while(sc.hasNext())
{
String line = sc.nextLine();
// spit the line on - char
String [] data = line.split("-");
// Important : assuming price is always at index 2 parse and use value
sum = sum + Double.parseDouble(data[2].trim());
}
sc.close();
System.out.println("Sum is "+sum);
}
}
The first line of your file is:
The_Hunger_Games - Suzanne_Collins - 5 - Scholastic_Press - 9781921988752
The code
sum+= fileReader.nextInt ( );
expects a number thought.
Before calculating the sum you need to parse out the part of the line which is the number to be added to the sum.
Scanner.next*() reads a stream until the next delimiter (the default is defined in Character.isWhitespace). When you call nextInt() it reads the next delimiter and then attempts to parse it as an int, if it is not then you get an InputMixmatchException. You can define your own delimiter like this (using an escaped regegex for <space>-<space>):
new Scanner(input).useDelimiter("\\s-\\s");
Then you would need to call .next() to skip over the parts of each line that you aren't parsing.
Now that last line of your example shows a hyphen directly following the publisher (without space). This fix might require more work over more data (such as book titles with hyphens in them). Hopefully, this gets you unstuck.

making an encryption program and im getting a sytnax error but i dont know why

Currently i'm trying to make a basic program that takes an input of any string such as a sentence or a paragraph and it takes each letter and turns it into a 3 character code once i get this working i'm assuming i should just be able to do the reverse and have it take the 3 digit code and turn it back to text, anyway i'm getting an error when i try to compile the program to test it. i have the issue marked below. also once i get the program working i would like to make a gui for it where you put in the input and it shows the out put after you click a button but as i am just starting out that seems a but advanced for me if you know any good tutorials for it please let me know!
import java.util.Scanner;
import java.util.*;
class test {
private static Scanner inp;
public static void main(String[] args) {
Map <Character, String> encryptionMappings = new HashMap<>();
encryptionMappings.put('a',"{qaz}");
encryptionMappings.put('b',"{wsx}");
encryptionMappings.put('c',"{edc}");
encryptionMappings.put('d',"{rfv}");
encryptionMappings.put('e',"{tgb}");
encryptionMappings.put('f',"{yhn}");
encryptionMappings.put('g',"{ujm}");
encryptionMappings.put('h',"{ik,}");
encryptionMappings.put('i',"{ol>}");
encryptionMappings.put('j',"{p;?}");
encryptionMappings.put('k',"{[']}");
encryptionMappings.put('l',"{qwe}");
encryptionMappings.put('m',"{asd}");
encryptionMappings.put('n',"{zxc}");
encryptionMappings.put('o',"{rty}");
encryptionMappings.put('p',"{fgh}");
encryptionMappings.put('q',"{vbn}");
encryptionMappings.put('r',"{yui}");
encryptionMappings.put('s',"{hjk}");
encryptionMappings.put('t',"{nm,}");
encryptionMappings.put('u',"{iop}");
encryptionMappings.put('v',"{qaw}");
encryptionMappings.put('w',"{sxz}");
encryptionMappings.put('x',"{red}");
encryptionMappings.put('y',"{cvf}");
encryptionMappings.put('z',"{ytg}");
encryptionMappings.put('A',"{hnb}");
encryptionMappings.put('B',"{iuj}");
encryptionMappings.put('C',"{kml}");
encryptionMappings.put('D',"{opl}");
encryptionMappings.put('E',"{wom}");
encryptionMappings.put('F',"{wsv}");
encryptionMappings.put('G',"{ths}");
encryptionMappings.put('H',"{imv}");
encryptionMappings.put('I',"{ybf}");
encryptionMappings.put('J',"{cja}");
encryptionMappings.put('K',"{thw}");
encryptionMappings.put('L',"{maz}");
encryptionMappings.put('M',"{pqa}");
encryptionMappings.put('N',"{zwl}");
encryptionMappings.put('O',"{;ld}");
encryptionMappings.put('P',"{'d;}");
encryptionMappings.put('Q',"{;ny}");
encryptionMappings.put('R',"{;ws}");
encryptionMappings.put('S',"{c/.}");
encryptionMappings.put('T',"{%#^}");
encryptionMappings.put('U',"{/mc}");
encryptionMappings.put('V',"{uka}");
encryptionMappings.put('W',"{zby}");
encryptionMappings.put('X',"{&hd}");
encryptionMappings.put('Y',"{&hw}");
encryptionMappings.put('Z',"{^#^}");
encryptionMappings.put('0',"{$g%}");
encryptionMappings.put('1',"{^#%}");
encryptionMappings.put('2',"{142}");
encryptionMappings.put('3',"{243}");
encryptionMappings.put('4',"{089}");
encryptionMappings.put('5',"{756}");
encryptionMappings.put('6',"{423}");
encryptionMappings.put('7',"{312}");
encryptionMappings.put('8',"{145}");
encryptionMappings.put('9',"{187}");
encryptionMappings.put('~',"{)*(}");
encryptionMappings.put('`',"{$#%}");
encryptionMappings.put('!',"{!^#}");
encryptionMappings.put('#',"{#^&}");
encryptionMappings.put('#',"{^#&}");
encryptionMappings.put('$',"{!?*}");
encryptionMappings.put('%',"{^<+}");
encryptionMappings.put('^',"{+$$}");
encryptionMappings.put('&',"{!!*}");
encryptionMappings.put('*',"{((%}");
encryptionMappings.put('(',"{*&^}");
encryptionMappings.put(')',"{$%^}");
encryptionMappings.put('_',"{&#^}");
encryptionMappings.put('-',"{<>?}");
encryptionMappings.put('=',"{:'^}");
encryptionMappings.put('{',"{%%G}");
encryptionMappings.put('}',"{$$$}");
encryptionMappings.put('[',"{***}");
encryptionMappings.put(']',"{:::}");
encryptionMappings.put(':',"{#$%}");
encryptionMappings.put('|',"{?H*}");
encryptionMappings.put(';',"{B&&}");
encryptionMappings.put('"',"{#gs}");
encryptionMappings.put('?',"{^gl}");
encryptionMappings.put('/',"{#gn}");
encryptionMappings.put('<',"{%TG}");
encryptionMappings.put('>',"{5%5}");
encryptionMappings.put(',',"{yty}");
encryptionMappings.put('.',"{ggg}");
inp = new Scanner(System.in);
System.out.println("Input Password");
int n = inp.nextInt();
if(n!=234) {
System.out.println("Denied Acess");
} else {
System.out.print("Password Accepted"
+ " ");
System.out.print("Input Text to encrypt: ");
String m = inp.next();
String encryptMe = "He";
StringBuilder builder = new StringBuilder();
The below line is the one that shows a syntax error for "toCharArray" i'm not sure why, I just started learning java so if its something simple that i'm missing i'm sorry, any and all help is apreciated.
for (Character c : encryptMe.toCharArray) {
builder.append(encryptionMappings.get(c));
}
String encrypted = builder.toString();
}
}
You are trying to call a method on the object but you are missing the empty () that need to follow toCharArray. Some languages allow you to omit the empty parentheses, but Java is not one of them. You should use:
for (Character c : encryptMe.toCharArray()) {
builder.append(encryptionMappings.get(c));
}
A good IDE (Eclipse, Intellij IDEA, Netbeans, etc.) will help you catch these syntax errors as you learn.

Read tab delimited file and ignore empty space

I am working on a simple project in which a tab delimited text file is read into a program.
My problem:
When reading the text file there are regularly empty data spaces. This lack of data is causing an unexpected output. For lines that do not have data in the token[4] position all data read is ignored and "4" is displayed when I run a System.out.println(Just a test that the data is being read properly). When I incorporate a value in the token[4] position the data reads fine. It is not acceptable that I input a value in the token[4] position. See below for file and code.
2014 Employee Edward Rodrigo 6500
2014 Salesman Patricia Capola 5600 5000000
2014 Executive Suzy Allen 10000 55
2015 Executive James McHale 12500 49
2015 Employee Bernie Johnson 5500
2014 Salesman David Branch 6700 2000000
2015 Salesman Jonathan Stein 4600 300000
2014 Executive Michael Largo 17000 50
2015 Employee Kevin Bolden 9200
2015 Employee Thomas Sullivan 6250
My code is:
// Imports are here
import java.io.*;
import java.util.*;
public class EmployeeData {
public static void main(String[] args) throws IOException {
// Initialize variables
String FILE = "employees.txt"; // Constant for file name to be read
ArrayList<Employee> emp2014; // Array list for 2014 employees
ArrayList<Employee> emp2015; // Array list for 2015 employees
Scanner scan;
// Try statement for error handling
try {
scan = new Scanner(new BufferedReader(new FileReader(FILE)));
emp2014 = new ArrayList();
emp2015 = new ArrayList();
// While loop to read FILE
while (scan.hasNextLine()) {
String l = scan.nextLine();
String[] token = l.split("\t");
try {
String year = token[0];
String type = token[1];
String name = token[2];
String monthly = token[3];
String bonus = token[4];
System.out.println(year + " " + type + " " + name + " " + monthly + " " + bonus);
} catch (Exception a) {
System.out.println(a.getMessage());
}
}
} catch(Exception b) {
System.out.println(b.getMessage());
}
}
}
The output I receive for lines with "Employee" returns in an unexpected way.
Output:
run:
4
2014 Salesman Patricia Capola 5600 5000000
2014 Executive Suzy Allen 10000 55
2015 Executive James McHale 12500 49
4
2014 Salesman David Branch 6700 2000000
2015 Salesman Jonathan Stein 4600 300000
2014 Executive Michael Largo 17000 50
4
4
BUILD SUCCESSFUL (total time: 0 seconds)
I tried to use an if-then to test for null value in token[4] position but that didn't really help me. I've done quite a bit of searching with no success.
I am still very new to the programming world, so please pardon my coding inefficiencies. Any support and general feedback to improve my skills is greatly appreciated!
Thank you,
Bryan
Java Devil is right that the underlying issue because of an ArrayOutOfBoundsException. But it's also worth exploring why you didn't see that. As we discussed in the comments your "Try statement for error handling" is in fact not handling your errors at all, instead it is suppressing them, which is generally a poor plan as it allows your program to continue running even after your assumption (that it works correctly) has been violated.
Here's a slightly cleaned up version of your code. The underlying problem that causes the ArrayOutOfBoundsException is still there, but the issue would be immediately apparent if you'd structured your code this way instead. There's a few comments calling out issues inline.
public class EmployeeData {
// constants should be declared static and final, and not inside main
private static final String FILE = "employees.txt";
// If you have an exception and you don't know how to handle it the best thing
// to do is throw it higher and let the caller of your method decide what to do.
// If there's *nothing* you want to do with an exception allow main() to throw
// it as you do here; your program will crash, but that's a good thing!
public static void main(String[] args) throws IOException {
// Notice the <> after ArrayList - without it you're defining a "raw type"
// which is bad - https://stackoverflow.com/q/2770321/113632
ArrayList<Employee> emp2014 = new ArrayList<>();
ArrayList<Employee> emp2015 = new ArrayList<>();
// A try-with-resources block automatically closes the file once you exit the block
// https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
try (Scanner scan = new Scanner(new BufferedReader(new FileReader(FILE)))) {
while (scan.hasNextLine()) {
String l = scan.nextLine();
String[] token = l.split("\t");
// The code below this line assumes that token has at least five indicies;
// since that isn't always true you need to handle that edge case before
// accessing the array indicies directly.
String year = token[0];
String type = token[1];
String name = token[2];
String monthly = token[3];
String bonus = token[4];
System.out.println(year + " " + type + " " + name + " " + monthly + " " + bonus);
}
}
}
}
This is happening because you are actually getting an ArrayOutOfBoundsException and the message for that is '4'. Because the index of 4 is greater than the length of the array. You should put in your catch statement b.printStackTrace() as this will give you greater details when ever the caught exception occurs.
You can get around this by adding the following:
String bonus = "";
if(token.length > 4)
bonus = token[4];

Categories