I can't seem to find the issue in my code and was wondering if you wonderful people on here would be willing to help me out. My professor is requiring us to create a 2-D array from information pulled from a file she provided us. Using the Scanner and File class we should be able to accomplish this, however, I have hit a speed bump. My scanner is not recognizing the integer after the delimiter I have set for it. Here is the file she provides us with.
5x7
o,2,3
7,1,3
7,1,1
X,4,2
This info is separated by newlines where there are spaces in the blockquote.
Here is my code:
import java.io.*;
import java.util.*;
public class Battlefield {
// Use the FILL_CHAR for a space that contains no creature.
// Use these chars for creatures so that your code will pass
// the tests used for evaluating your program.
public final char FILL_CHAR = '-';
public final char OGRE = 'o';
public final char CENTAUR = '7';
public final char DRAGON = 'X';
private char[][] field;
public Battlefield(String fn) {
try {
// You write code here.
// Read a file and initialize the field.
// The name of the file is passed in from the driver.
// Keep all the file reading stuff in the try/catch block
// to make file exceptions easier to deal with.
File battlefield = new File(fn);
Scanner scan = new Scanner(battlefield);
scan.useDelimiter("x");
int row = scan.nextInt();
System.out.println(row);
System.out.println(scan.next());
System.out.println(scan.hasNextInt());
int column = scan.nextInt();
char[][] field = new char[row][column];
/**
Scanner scan2 = new Scanner(battlefield);
scan2.useDelimiter(",");
/**
field[scan2.nextInt()][scan2.nextInt()] = OGRE;
field[scan2.nextInt()][scan2.nextInt()] = CENTAUR;
field[scan2.nextInt()][scan2.nextInt()] = CENTAUR;
field[scan2.nextInt()][scan2.nextInt()] = DRAGON;
**/
} catch (IOException ex) {
System.err.println(ex.getStackTrace());
}
}
And my main method/driver class:
public class BattlefieldDrv {
public static void main(String[] args)
{
Battlefield battlefieldOne = new Battlefield("1field.dat");
System.out.println(battlefieldOne.toString());
}
}
Here is my stack trace:
> 5
7
o,2,3
7,1,3
7,1,1
X,4,2
false
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Unknown Source)
at java.base/java.util.Scanner.next(Unknown Source)
at java.base/java.util.Scanner.nextInt(Unknown Source)
at java.base/java.util.Scanner.nextInt(Unknown Source)
at Battlefield.<init>(Battlefield.java:38)
at BattlefieldDrv.main(BattlefieldDrv.java:15)
Thank you for any help or insight you have!
So let's step through this code.
scan.useDelimiter("x");
int row = scan.nextInt();
5 is read into row.
System.out.println(row);
5 is printed.
System.out.println(scan.next());
The rest of the file is read and printed, because that's what comes after the x.
System.out.println(scan.hasNextInt());
There's nothing left to read, so the NoSuchElementException is thrown here.
You need to make the scanner also accept newlines as delimiter; you can do that by using
scan.useDelimiter("(x|\\s)");
(The \\s is the pattern for "any whitespace").
As a side note, it's good practice to use the try-with-resources-construct:
try (Scanner scan = new Scanner(Paths.get("1field.dat"))) {
scan.useDelimiter(...);
...
} catch (IOException e) {
This will result in your file resource being closed automatically.
Related
This question already has answers here:
Array Required, but java.lang.String found
(2 answers)
Closed 11 months ago.
I have a file definitions.dict which contains words and their respective definitions. A snippet of the file looks like this:
dictionary ? a book or electronic resource that gives a list of the words of a language in alphabetical order and explains what they mean
word ? a single unit of language that means something and can be spoken or written
computer ? an electronic machine that can store, organise and find information, do processes with numbers and other data, and control other machines
I'm trying to write Java code, that takes the word as standard input and outputs the definition of said word. The code I've written so far fails to even compile, giving the error:
Dictionary.java:25: error: array required, but String found
if(searchword.equals(line[i][0])){
^
Dictionary.java:26: error: array required, but String found
System.out.println(line[i][1]);
^
Here is the code I have so far:
import java.util.*;
import java.io.*;
public class Dictionary {
public static void main(String[] args) throws FileNotFoundException {
String searchword = "";
String[] line = {};
int i;
Scanner scanword = new Scanner(System.in);
if(scanword.hasNextLine()){
searchword = scanword.nextLine();
}
try{
Scanner scan = new Scanner(new File("sample.dict"));
while (scan.hasNextLine()){
line = Arrays.copyOf(line, line.length + 1);
line[line.length-1] = scan.nextLine();
}
for(i = 0; i < line.length; i++) {
line[i].split("\\ ? ");
if(searchword.equals(line[i][0])){
System.out.println(line[i][1]);
}
}
}
catch (FileNotFoundException ex) {
;
}
}
}
I will admit that this is part of an assignment which I've been struggling with for a while and I am asking here as a last resort.
(Note that I am only allowed to work with arrays and no other data structure.)
Slighlty different approach but what I did was actually create a dictionary object with two parameters:
public String term;
public String definition;
Then had a method to load dictionary objects into a Dictionary[] array from my sample.dict.
So then all I had to do in my main method was something like:
Dictionary[] definitions = loadDefinitions(f);
while (scan.hasNext()){
String userInput = scan.next();
for (int i = 0; i<definitions.length-1;i++){
if(userInput.contentEquals(definitions[i].term)){
System.out.println(definitions[i].definition);
Hope that logic kinda makes sense. Good luck on the task! (Have a look at the athletes tutorial example :) )
The code is supposed to read an unidentified number of inputs from the keyboard and return any tabs as *. My program seems to work when I run it in eclipse and get no errors. When I turn in the code on the submission website, this is the error I get.
Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Scanner.java:1589) at replaceHW.main(replaceHW.java:9)
import java.util.Scanner;
public class replaceHW {
public static void main(String[] args) {
//write a program that converts all TABS in your code
//with STARS i.e. *
Scanner in = new Scanner(System.in);
String ans;
while(!(ans = in.nextLine()).equals(""))
System.out.println(ans.replace("\t","*"));
}
}
Your problem is simple: nextLine() works in tandem with hasNextLine(): the correct code is:
try (Scanner in = new Scanner(System.in)) {
while (in.hasNextLine()) {
String line = in.nextLine();
if (!"".equals(line)) {
System.out.println(ans.replace("\t","*"));
}
}
The try-with-resources is best practice. But be wary than with System.in, it will close it when done.
hasNextLine() will try to read has much input is needed to find a line.
I've been given a task that I have to create a Shopping List program. I've done this in Python, and it was relatively straight forward. However, in Java I've hit a bit of a roadblock.
These are my variables, I am aware of the issues with using statics in this way and that it would be best to avoid doing it.
private static Scanner input = new Scanner(System.in);
private static String list_add = "string"; //"string" is just a place holder
private static ArrayList listFull = new ArrayList();
private static ArrayList listPos = new ArrayList();
private static int userIn = 1; //1 is also being used as place holder
Which I use in:
private static void userInput() {
boolean isValid = false;
while (!isValid) {
isValid = true;
try {
userIn=Integer.parseInt(input.next());
}
catch (InputMismatchException e) {
System.out.println("That's not a valid number!");
isValid = false;
}
}
}
The reasoning behind doing it this way is the less one needs to type the quicker the task can be completed. Which was working as nice philosophy up until I tried this. My previous attempt to solve this problem gave an infinite loop, and the second solution that came to mind returned a StackOverflowError. When I asked about avoiding the infinite loop, I was directed to another question (Endless loop while using "try and catch " block inside a "while loop") which I did not believe helpful to begin with, however found that it was (Thank you whoever marked that). I didn't get this solution to work, however, and I cannot see where I went wrong.
After trying different inputs to see if their was one specific type that killed it, these were the errors I received:
Test Case "strin":
Exception in thread "main" java.lang.NumberFormatException: For input string: "strin"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at DebuggingMethods.AllIn(DebuggingMethods.java:17)
at DebuggingMethods.Menu(DebuggingMethods.java:33)
at DebuggingMethods.main(DebuggingMethods.java:58)
Test Case "Ten":
Exception in thread "main" java.lang.NumberFormatException: For input string: "Ten"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at DebuggingMethods.AllIn(DebuggingMethods.java:17)
at DebuggingMethods.Menu(DebuggingMethods.java:33)
at DebuggingMethods.main(DebuggingMethods.java:58)
Test Case int(10):
Which ran with the anticipated outcome.
I had thought that I was missing a module, so I did import java.lang.*; which did not change the error. If someone has a solution, please help me out. I can't find a question that already posted that explains what I am doing wrong. When I pulled it out of the Try-Catch it was working kind of.
Full Piece
import java.util.InputMismatchException;
import java.util.*;
import java.util.Scanner;
import java.lang.*;
public class TestOne {
private static Scanner input = new Scanner(System.in);
private static String list_add = "string";
private static ArrayList listFull = new ArrayList();
private static ArrayList listPos = new ArrayList();
private static int userIn = 1;
private static void userInput() {
boolean isValid = false;
while (!isValid) {
isValid = true;
try {
userIn=Integer.parseInt(input.next());
}
catch (InputMismatchException e) {
System.out.println("That's not a valid number!");
isValid = false;
}
}
}
public static void main(String[] args) {
//titleMain();
userInput(); // For the sake of demonstration
}
}
You've got your exceptions mixed up. You're catching an InputMismatchException, which is what input.nextInt() would have thrown if the input were invalid. But you're actually using Integer.parseInt to parse the input, which throws NumberFormatException in case of invalid input. And since NumberFormatException isn't a subclass of InputMismatchException, it isn't caught and you end up dying with a stack trace.
You don't actually need to do anything with exception at all here. The Scanner can tell you if the next input is a valid integer or not and you can then actively decide what do about it. Think along these lines:
Scanner sc;
//...
System.out.println("Please enter a number");
while (!sc.hasNextInt()){
sc.nextLine(); // throw away the bad input
System.out.println("Please enter a valid number");
}
int theNum = sc.nextInt();
I do not know how to take the integer and ignore the strings from the file using scanner. This is what I have so far. I need to know how to read the file token by token. Yes, this is a homework problem. Thank you so much.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ClientMergeAndSort{
public static void main(String[] args){
int length = 13;
try{
Scanner input = new Scanner(System.in);
System.out.print("Enter the file name with extention : ");
File file = new File(input.nextLine());
input = new Scanner(file);
while (!input.hasNextInt()) {
input.next();
}
int[] arraylist = new int[length];
for(int i =0; i < length; i++){
length++;
arraylist[i] = input.nextInt();
System.out.print(arraylist[i] + " ");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Take a look at the API for what you're doing.
http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#hasNextInt()
Specifically, Scanner.hasNextInt().
"Returns true if the next token in this scanner's input can be interpreted as an int value in the default radix using the nextInt() method. The scanner does not advance past any input."
So, your code:
while (!input.hasNextInt()) {
input.next();
}
That's going to look and see if input hasNextInt().
So if the next token - one character - is an int, it's false, and skips that loop.
If the next token isn't an int, it goes into the loop... and iterates to the next character.
That's going to either:
- find the first number in the input, and stop.
- go to the end of the input, not find any numbers, and probably hits an IllegalStateException when you try to keep going.
Write down in words what you want to do here.
Use the API docs to figure out how the hell to tell the computer that. :) Get one bit at a time right; this has several different parts, and the first one doesn't work yet.
Example: just get it to read a file, and display each line first. That lets you do debugging; it lets you build one thing at a time, and once you know that thing works, you build one more part on it.
Read the file first. Then display it as you read it, so you know it works.
Then worry about if it has numbers or not.
A easy way to do this is read all the data from file in a way that you prefer (line by line for example) and if you need to take tokens, you can use split function (String.split see Java doc) or StringTokenizer for each line of String that you are reading using a loop, in order to create tokens with a specific delimiter (a space for example) so now you have the tokens and you can do something that you need with them, hope you can resolve, if you have question you can ask.
Have a nice programming.
import static java.nio.file.Files.readAllBytes;
import static java.nio.file.Paths.get;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String args[]) throws IOException {
String newStr=new String(readAllBytes(get("data.txt")));
Pattern p = Pattern.compile("-?\\d+");
Matcher m = p.matcher(newStr);
while (m.find()) {
System.out.println("- "+m.group());
}
}
}
This code fill read the file and then using the regular expression you can get only Integer values.
Note: This code works in Java 8
I Think This will work for you requirement.
Before reading the data from the file initially,try to write some content to the file by using scanner and filewriter then try to execute the below code snippet.
File file = new File(your filepath);
List<Integer> list = new ArrayList<Integer>();
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String str =null;
while(true) {
str = bufferedReader.readLine();
if(str!=null) {
System.out.println(str);
char[] chars = str.toCharArray();
String finalInt = "";
for(int i=0;i<chars.length;i++) {
if(Character.isDigit(chars[i])) {
finalInt=finalInt+chars[i];
}
}
list.add(Integer.parseInt(finalInt));
System.out.println(list.size());
System.out.println(list);
} else {
break;
}
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
The final println statement will display all the integer in your file line by line.
Thanks
So, I am working on a code for class and can't figure what is wrong. The code compiles and when I enter the file I'm searching for, I get this message:
Enter the name of the file: FanData.txt
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at FanDriver.fillArray(FanDriver.java:76)
at FanDriver.main(FanDriver.java:35)
Press any key to continue . . .
I'm using TextPad as my compiler and the text file is in the project. The following is the code that I have written (ignore the methods being called in the quotes as they are something I need to do afterwards):
import java.io.*;
import java.util.Scanner;
public class FanDriver
{
private static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) throws IOException
{
// Constant for the amount of elements of the array
final int MAXSIZE = 100;
// Declaring variables
int amountFans = 0;
// Declaring and initializing our array of fans
Fan[] fans = new Fan[MAXSIZE];
// Calling all of our methods
amountFans = fillArray(fans, MAXSIZE);
/**
listFanData(fans, amountFans);
bubbleSortByAge(fans, amountFans);
listFanData(fans, amountFans);
bubbleSortByFan(fans, amountFans);
listFanData(fans, amountFans);
searchByAge(fans, amountFans);
searchByFan(fans, amountFans);
*/
}
public static int fillArray(Fan[] array, int MAXSIZE) throws IOException
{
// Declaring variables
int counter = 0;
int age;
String name;
// Getting the file name
System.out.print("\nEnter the name of the file: ");
String fileName = keyboard.nextLine();
// Opening the file
File file = new File(fileName);
Scanner inputFile = new Scanner(file);
// Making sure the file was successfully opened
if (!file.exists())
{
System.out.println("\nERROR: FILE DOESN'T EXIST. CLOSING PROGRAM NOW.");
// Exiting the program
System.exit(0);
}
// Reading all of the amounts from the file
while (inputFile.hasNext() && counter < MAXSIZE)
{
name = inputFile.nextLine();
age = inputFile.nextInt();
array[counter] = new Fan(name, age);
// Adding to our counter
counter = counter + 1;
}
//Closing file
inputFile.close();
return counter;
}
}
I do not have the code for the Fan class, just the class itself.
The file we are retrieving is the file FanData.txt, which looks like this:
Chris P. Cream
5
Scott Free
9
Lou Tenant
3
Trish Fish
12
Ella Mentry
4
Holly Day
3
Robyn DeCradle
12
Annette Funicello
4
Elmo
7
Grover
3
Big Bird
9
Bert
7
Ernie
3
Grover
9
The text file is line-by-line. One line is a name and the next is a number. I don't know how to format it correctly on here.
Any help would be greatly appreciated.
I am not sure about the delimiter in your input file.
The way you had instantiated your "Scanner"(without specifying any explicit delimiter) shall use default delimiter i.e. a whitespace character.
It seems that the "nextLine()" method in your case is not returning a "line".
It is rather returning a number or string(may be, you need check your file for that) which is not matching with the pattern followed by your Scanner.
The error is because of the type mismatch. After your first read scanner will be pointing to a non Integer value.
try to print your name before doing
age = inputFile.nextInt();
you will get to know the issue.
Above mentioned are means to understand the mistake.
Solution for your problem
Try :
age = Integer.parseInt(inputFile.nextLine()); // instead of age = inputFile.nextInt();
Exception is because of type mismatch as you are reading a text value from textfile and directly assigning to an int type.
Exception will be resolved if you parse the value as an integer as below
Integer.valueOf(inputFile.nextLine());