Java Equivalent to fscanf in C [duplicate] - java

This question already has answers here:
Is there an equivalent method to C's scanf in Java?
(7 answers)
what is the Java equivalent of sscanf for parsing values from a string using a known pattern?
(8 answers)
Closed 4 years ago.
Hi I i'm trying to do a line by line reading in of a file in Java. For example I'd like to be able to do something like
//c code
while( fscanf(ptr, "%s %s %s",string1,string2,string3) == 3)
printf("%s %s %s",string1,string2,string3);
Where I scan in every line in the file individually and store it in a variable.
I have started out by making a file object and finding the file. However I haven't found a function to be able to read input.

Use line oriented input, split the lines and print the contents. That is, read each line, check if you have three tokens (end if you don't) and print them. Like,
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[] tokens = line.split(" ");
if (tokens.length != 3) {
break;
}
System.out.printf("%s %s %s%n", tokens[0], tokens[1], tokens[2]);
}
Change System.in to a File to read from a file.

Related

How to read only first expression of my file and move to another line [duplicate]

This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 3 years ago.
I have a file that looks like follows:
What I need is to read only names (first column) and output it.
while (sc.hasNext())
System.out.println(sc.next());
Another solution I tried is:
while (sc.hasNextLine())
System.out.println(sc.nextLine());
The ones that are above obviously don't work. I am stuck and don't know what to do. I was trying to google my problem but couldn't find the answer.
while(sc.hasNextLine()) {
StringBuilder toPrint = new StringBuilder();
String line = sc.nextLine();
for(int i = 0; i < line.length() && line.charAt(i) != ' '; i ++) {
toPrint.append(line.charAt(i));
}
System.out.println(toPrint.toString());
}
The reason I don't use String.split() is because that does lot's of unnecessary work, as it splits the entire string, and you only want the first word.

Java .split() by character with spaces in string [duplicate]

This question already has answers here:
What's the difference between next() and nextLine() methods from Scanner class?
(15 answers)
Closed 5 years ago.
I am trying to read a line from a text file in java. I get a java.lang.ArrayIndexOutOfBoundsException: 1 exception.
Here is my code:
try {
Scanner kb = new Scanner(new FileReader(fileName));
String line = "";
int count = 0;
while (kb.hasNext()) {
line = kb.next();
String[] temp = line.split("#");
System.out.println(temp[1]);
Wedding tempWed = new Wedding(temp[0], temp[1], temp[2], temp[3], Integer.parseInt(temp[4]));
test[count] = tempWed;
count++;
}
} catch (FileNotFoundException ex) {
}
This is the line in the textfile:
Chiquita Sanford#Magee Sosa#2016-11-05#Garden#84
I need to split by the "#", and this partly works.
Java throws the exception when I try to access the element at position 1.
I think this is because there is a space between the first name and the surname, because when I System.out.println(temp[0]) it displays "Chiquita" and not "Chiquita Sanford".
Does Java have some restriction on splitting when there are multiple words in the first array index.
You have to use the nextLine method to read the full line. next will read until the first token ("Chiquita" in your case because its followed by a space character and is interpreted as a delimiter). So change this line:
line = kb.next();
with this:
line = kb.nextLine();
You are using kb.next() that will return the next word not the next line, for this use kb.nextLine() similar issue with kb.hasNext() needs to be kb.hasNextLine()

Search String in file [duplicate]

This question already has answers here:
Creating a Java Program to Search a File for a Specific Word
(3 answers)
Closed 5 years ago.
I am trying to count the number of times a specific string appears into a file.
This is the code I am using:
while (scanner.hasNext()) {
String nextToken = scanner.next();
if (nextToken.equalsIgnoreCase(wordidnamee1))
count++;
}
This code only counts the number of time the string appears 'clean', but if it is attached to another word or followed by a colon it is not counted.
How can I solve this problem ?
Use contains()
while (scanner.hasNext()) {
String nextToken = scanner.next();
if (nextToken.contains(wordidnamee1))
count++;
}
For Non Case Sensitive match:
while (scanner.hasNext()) {
String nextToken = scanner.next();
if (nextToken.toLowerCase().contains(wordidnamee1.toLowerCase()))
count++;
}

How to take multiple string value as input? [duplicate]

This question already has answers here:
How can I read input from the console using the Scanner class in Java?
(17 answers)
Closed 5 years ago.
I have a string variable that I use to get input values. for ex.
Scanner in=new Scanner(System.in);
varName=in.next();
when I give value as (John jony) it only displays John. Any way to get whole string?
Use the following instead:
Scanner in=new Scanner(System.in);
String text = in.nextLine();
System.out.println( text );
"in.nextLine()" reads in a whole line

How can I both check the scanner contents of a file and use them to assign input without skipping data with nextLine? [duplicate]

This question already has an answer here:
How to use java.util.Scanner to correctly read user input from System.in and act on it?
(1 answer)
Closed 6 years ago.
The method should read the contents of a file line by line and add each line to the array list. It should end once it reaches a solitary "." (period) or no more lines.
The problem is that I can not figure a way to check the contents of the next line without skipping lines since I am using nextLine numerous times. I am limited to the use of hasNext and nextLine.
public static ArrayList<String> getList(Scanner in) {
ArrayList<String> list = new ArrayList<String>();
while(in.hasNext() && !in.nextLine().equals("."))
{list.add(in.nextLine());}
return list;}
As written the output will skip lines to output lines 2, 4, 6 etc
when I need it to output 1,2,3,4 etc.
I am sure I am simply not seeing the way to solve the issue but any hints specifically on how to get it working by reformatting what I have with the methods listed are appreciated.
Just store the line in a variable:
public static List<String> getList(Scanner in) {
List<String> list = new ArrayList<String>();
while (in.hasNextLine()) {
String line = in.nextLine();
if (line.equals(".")) {
break;
}
else {
list.add(line);
}
}
return list;
}

Categories