Java console input length limit - java

I have an interview task where I was given a text file with file.in format, the task said that my program should be using standard data input and output (I assume console). The input file goes something like this:
249089
439506849 989399339
773359725 989399094
33290819 989399230
771114928 989399164
823133180 989399164
615096154 989399094
340750872 989399164
41881535 989399230
637599407 989399339
510268939 989399506
46219619 989399544
221332387 989399659
236968778 989399824
902942034 989399945
936095694 989400101
**end to the line 249090**
The first number is the number of objects
The second is two numbers, but for the purpose of the task I only use the second one
For the purpose of parsing the numbers I use for loop and code below:
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)))
String line = bufferedReader.readLine();
System.out.println(line);
StringTokenizer stringTokenizer = new StringTokenizer(line, " ");//
stringTokenizer.nextToken();
int height = Integer.parseInt(stringTokenizer.nextToken());
I use IntelliJ build in console and when I paste into console i get like a couple thousands results in starting from the end, so the first number is wrong, and when i run my program i get Runtime Error:
Exception in thread "main" java.lang.NumberFormatException: For input string: "84 995058150"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
at java.base/java.lang.Integer.parseInt(Integer.java:658)
at java.base/java.lang.Integer.parseInt(Integer.java:776)
at pl.artur.Main.getNumberOfBuildings(Main.java:23)
at pl.artur.Main.main(Main.java:14)
Is there a way to get around it using standard input?

This has nothing to do with where the input comes from; as the stack trace shows, the exception is thrown by the Integer.parseInt method on the string "84 995058150". This string clearly does not represent a (singular) integer. If the StringTokenizer.nextToken method returns this string, then it is StringTokenizer that's the problem. As David Conrad notes in the comments, the documentation says:
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
The String.split method will split line into the two parts, so you can then call Integer.parseInt on the part you want:
String line = bufferedReader.readLine();
String[] parts = line.split(" ");
int height = Integer.parseInt(parts[1]);

Ok, I resolved the issue.
The solution was to set the bigger buffer size for the console in IntelliJ settings:

I think you cannot convert or parse string including empty spaces between them to Integer.
// This string cannot be parsed to Integer directly.
// Because an empty space is included between `84` and `995058150`.
String s = "84 995058150";
If you want to parse this, you should use trim() method. example:
int intValue= Integer.parseInt(s.trim());
I hope this answer will be helpful for you.

Related

Replace data between special character

My text file has a pattern and it's just like the following:
1;Mary Yeah;John Freeman;(12)3456-7890;iammary#gmail.com
2;Ash Wilson;One Two Three;(99)1111-2222;lorddragon#hotmail.com
3;Xin Zhao;Street Address 55;(11)0101-0202;lolyourface#gmail.com
4;My Name;My Address;My Phone;myemail#mail.com
I want to be able to type the line number, the type of data I want to replace(e-mail, phone, name), and the string I want to replace them with. The program overwrites the text.
How could I code this in Java?
The issue of how to find a given row based on the line number depends on many things, most importantly it depends on code you haven't shown us. But as for what you can do once you have found a given line, you may try the following:
String line = "2;Ash Wilson;One Two Three;(99)1111-2222;lorddragon#hotmail.com";
String[] parts = line.split(";");
parts[4] = "some.address#mail.com"; // to change the email
// now join back to a single line
line = String.join(";", Arrays.asList(parts));
Demo

Java - SecurityException in Method "printDuplicates"

I'm a newcomer to Java trying to submit a working project, in this instance printDuplicates. The instructions are as follows:
Write a method named printDuplicates that accepts as its parameter a Scanner for an input file containing a series of lines. Your method should examine each line looking for consecutive occurrences of the same token on the same line and print each duplicated token along how many times it appears consecutively. Non-repeated tokens are not printed. Repetition across multiple lines (such as if a line ends with a given token and the next line starts with the same token) is not considered in this problem.
For example, if the input file contains the following text:
hello how how are you you you you
I I I am Jack's Jack's smirking smirking smirking smirking smirking revenge
bow wow wow yippee yippee yo yippee yippee yay yay yay
one fish two fish red fish blue fish
It's the Muppet Show, wakka wakka wakka
Your method would produce the following output for the preceding input file:
how*2 you*4
I*3 Jack's*2 smirking*5
wow*2 yippee*2 yippee*2 yay*3
wakka*3
Your code prints only the repeated tokens; the ones that only appear once in a row are not shown. Your code should place a single space between each reported duplicate token and should respect the line breaks in the original file. This is why a blank line appears in the expected output, corresponding to the fourth line of the file that did not contain any consecutively duplicated tokens. You may assume that each line of the file contains at least 1 token of input.
Here is my code, pretty much ready for submitting.
import java.io.*;
import java.util.*;
Scanner input;
public static void printDuplicates(Scanner input) throws Exception {
String word = "";
String word2 = "";
input = new Scanner(new File("idontknowwhattodo.txt"));
while(input.hasNextLine()) {
Scanner line = new Scanner(input.nextLine());
int repeat = 1;
word = line.next();
while(line.hasNext()) {
word2 = line.next();
while(word.equals(word2)) {
repeat++;
if(line.hasNext()){
word2 = line.next();
} else {
break;
}
}
if(repeat!=1) {
System.out.print(word + "*" + repeat + " ");
}
repeat = 1;
word = word2;
}
System.out.println();
}
}
However, whenever I try to submit my project, it throws back this error:
(no output was produced!)
SecurityException on line 5:
You are not allowed to read the file /usr/share/tomcat7/temp/idontknowwhattodo.txt
java.lang.SecurityException: You are not allowed to read the file /usr/share/tomcat7/temp/idontknowwhattodo.txt
at java.io.FileInputStream.<init>(FileInputStream.java:135)
at java.io.FileReader.<init>(FileReader.java:72)
at Scanner.<init>(Scanner.java:330)
at printDuplicates (Line 5)
What does this mean? I have multiple working projects but I can't seem to submit them due to this one error. Any experts that can help me on this one? Thank you.
It looks like you are using Tomcat from your path. Tomcat requires special security permission to read or write files. This is a basic protection to prevent malicious code from accessing sensitive files on the OS. You can configure these directories or stick to reading and writing to the default ones:
https://tomcat.apache.org/tomcat-7.0-doc/security-manager-howto.html
Unable to add a comment because of reputation points so using the Answers section.
Agree with above comments, it is related to permissions.
Do an ls -ltr on /usr/share/tomcat7/temp/idontknowwhattodo.txt
Check whether the user (say myuser) with which you are running you java application has necessary permissions for /usr/share/tomcat7/temp/idontknowwhattodo.txt.
Two options below:
Give the user "myuser" the necessary permissions to the idontknowwhattodo.txt using chmod.
Or copy idontknowwhattodo.txt to a location where "myuser" has the permissions.
Problem description says that you're getting Scanner object as parameter. You don't have to recreate it, you're probably trying to submit your project to some online competition. Program on the server will load your class and call the method printDuplicates() with Scanner object as parameter, you don't have to worry about how it gets created. Just use it, and everything would be fine.
Just comment the scanner assignment line as below
String word = "";
String word2 = "";
/*input = new Scanner(new File("idontknowwhattodo.txt"));*/
while(input.hasNextLine()) {
...
As per instructions, you are already getting the Scanner object(which references the input file) as parameter to your method. So, you should not be re-initializing it.
This line should be removed:
input = new Scanner(new File("idontknowwhattodo.txt"));

Java Parsefloat Exception with a string that is number followed by characters

When I run this line of code: Float.parseFloat("1460000 JPY") I get the error
Exception in thread "main" java.lang.NumberFormatException: For input string: "1460000 JPY"
This string is coming from an API call from a form where this is a text field with no validation. It usually works because people put in just a number, but sometimes you get this issue. How do I get it to return just the initial numbers as a float and disregard the trailing alpha characters?
You can use regex to find if that string contains only digit or not
String apistring = "1460000 JPY";
if(apistring.matches("[0-9]+")){
// do your code
}else{
// throw some error message
}
Stripping char from that will be difficult as you said its a input field and user can enter any text. You can strip it off only if you know that there is a particular pattern
Since DecimalFormat is very lenient about parsing Strings I would recommend that.
You can use it like this:
DecimalFormat df = new DecimalFormat();
try {
float parsedValue = df.parse("1460000 JPY").floatValue();
System.out.println(parsedValue);
} catch (ParseException pe) {
pe.printStackTrace();
// handle exception a bit more
}
This prints:
1460000.0
As you can see the parse method can throw a ParseException if the passed String starts with something else than a number, like:
blub 1460000 JPY
If that won't happen in your app, then you don't have to bother about it.
You can use regex to extract the numbers in input .
s = s.replaceAll("[^0-9]","");
and then parse float from it. Only downside is that it will extract all numbers (It will extract 1245 and 3 both from 1245 JPY 3).
UPDATE: to account for the bug that #Tom brought up:
Float.parseFloat("1.46 JPY".replaceAll("[^0-9.]",""));
1.46
the above is a superior solution. See below for explanation.
As #azurefrog said, stripping out non-numeric characters and then parsing what is left as a Float is the way to go.You can accomplish this using the following code:
Float.parseFloat("1460000 JPY".replaceAll("[^0-9]",""));
1460000.0
This is not very robust though, because for inputs like "1.46" the output becomes
146.0
.replaceAll("[^0-9.]","") fixes this inaccuracy by adding the decimal . character to the exclusion regex like so [^0-9.]

Java: reading a string in a particular format

I am not posting any code I am struck with. I am trying this in Java:
Issue:
I have words like:
,xxxx-1223
yyyyy,xxdd-345
$,xxxxr-7
sdsdsdd-18
so what ever format I have I should be able to read the last one:
xxxx-1223
xxdd-345
xxxxr-7
sdsdsdd-18
what so may be the words, all I need to to get the words as shown.
Use String#lastIndexOf(int) to find where the last comma occurs, and use String#substring(int) to get the rest of the string that follows.
String input = /* whatever */;
int lastComma = input.lastIndexOf(',');
String output = input.substring(lastComma + 1);
String[] str=yourWord.split(",");
String output=str[str.length-1];
You can use this Regex: -
(\\w+-\\d+)$
Or this specific problem can simply be solved using String.split() or String.substring(int) methods

Add exact number of spaces to start of string using java.util.Formatter

I am using Formatter to output Java code to a file. I want to add a specific number of spaces to the start of each line. My problem is I cannot find a way to do this "neatly". The standard options seem to only allow adding a minimum number of spaces but not a specific number of spaces.
As a work around, I am currently doing the following:
out.format("%7s%s", "", "My text"); but I'd like to do it with only two arguments like this out.format("%7s", "My text");.
Does anyone know if there is a way to do this using the standard Formatter options?
I'm not exactly sure what you want here:
out.format("xxx%10sxxx", "My text");
// prints: xxx My textxxx
While:
out.format("xxx%-10sxxx", "My text");
// prints: xxxMy text xxx
As far as I know, there is no way to do the old C-style formatting to specify the size in an argument like "%*s" because then you could pass in (str.length() + 7).
I'm afraid that your way seems to the the most "neat". If you can explain why you don't like it maybe we can find a better workaround.
You can prepend text into your string.
Another way to reapet any string which you can use this code:-
String str = "abc";
String repeated = StringUtils.repeat(str, 3);
here StringUtils is org.apache.commons.lang3.StringUtils class.
Use Commons Lang
String line = "Hello World!";
int numberOfSpaces = 2;
String lineWithSpacePadding = StringUtils.leftPad(line, line.length() + numberOfSpaces);

Categories

Resources