Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
I have a situation where my program needs to read and print bunch of 3 lines at a time. I know basic Java hence finding difficult to write.
I have a text file which has 3 lines of one message; the catch is it’s not only one message but multiple message of three lines. Each time I want to read and print first message which has 3 lines and then jumpy to 4th line to read next three lines and print them and so on
For example :
Test1
Test2
Test3
Test4
Test5
Test6
Test7
Test8
Test9
Now out come should be
Test1
Test2
Test3
Test4
Test5
Test6
Test7
Test8
Test9
Need help in resolving this issue.
From what I understand you want to iterate over the lines in file and print them in 'groups' of three lines each. If that is correct - here is a small piece of code that can help you achieve it:
try {
File file = new File("<<enter your file path here>>");
Scanner scanner = new Scanner(file);
int counter = 0; // Counter to track the number of lines printed
while(scanner.hasNextLine()) {
if(counter < 3) { // Grouping by three lines
String line = scanner.nextLine();
System.out.print(line + " "); // Printing on the same line
counter++;
} else {
System.out.println(" "); // Printing a 'newLine'
counter = 0; // Reset the counter
}
}
scanner.close(); // Close the scanner
} catch (Exception e) {
e.printStackTrace(); // Change this as per your need
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm new in Java and I have a task that's going like this:
Generate one number between 0-9 and a second number between 10-99.
You need to check how many times the first number appears in the second number.
(For example: The first number is 5 and the second one is 55 so the first number (5) apears 2 times in the second number (55) ).
Btw, you need to make it without arrays.
package randomNumber_01;
import java.util.Random;
public class third {
public static void main (String[] args){
Random n = new Random();
int first = n.nextInt(9)+1;
System.out.println("First numnber: "+first);
int second = n.nextInt(99)+10;
System.out.println("Second number: "+second);
System.out.println("I stuck here");
}
}
int count=0;
if((second%10)==first){
count++;
}
if(Math.round((second/10))==first){
count++;
}
System.out.println(first+" occurs "+count+" times in "+second);
Maybe this can help you ;), replace your last println by this 8 lines ;)
but as shmosel sait in comment, be carreful it's
int first = n.nextInt(10);
int second = n.nextInt(90)+10;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
//input: multiple integers with spaces inbetween
Scanner sc = new Scanner(System.in);
while(sc.hasNextInt())
{
//add number to list
}
sc.hasNextInt() is waiting for an integer. It only breaks out if you input a non-integer character.
I saw a solution here before not too long ago but i cant find it anymore.
The solution (was the best if you ask me) was using two scanners. I cant seem to figure out how it used two scanners to go around this problem.
sc.NextLine() maybe?
A user can input multiple integers the amount is unknown. Ex: 3 4 5 1. There is a space inbetween them. All i want to do is read the integers and put it in a list while using two scanners.
Based on your comment
A user can input multiple integers the amount is unknown. Ex: 3 4 5 1. There is a space inbetween them. All i want to do is read the integers and put it in a list while using two scanners.
You are probably looking for:
scanner which will read line from user (and can wait for next line if needed)
another scanner which will handle splitting each number from line.
So your code can look something like:
List<Integer> list = new ArrayList<>();
Scanner sc = new Scanner(System.in);
System.out.print("give me some numbers: ");
String numbersInLine = sc.nextLine();//I assume that line is in form: 1 23 45
Scanner scLine = new Scanner(numbersInLine);//separate scanner for handling line
while(scLine.hasNextInt()){
list.add(scLine.nextInt());
}
System.out.println(list);
Try this:
while (sc.hasNext()) {
if (sc.hasNextInt()) {
// System.out.println("(int) " + sc.nextInt());
// or add it to a list
}
else {
// System.out.println(sc.next());
// or do something
}
}
Scanner sc = new Scanner(System.in);
while(sc.hasNextInt())
{
System.out.println("Hi");
System.out.println("Do you want to exit ?");
Scanner sc2 = new Scanner(System.in);
if(sc2.next().equalsIgnoreCase("Yes")){
break;
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a program that reads a file in order to sort the file alphabetically. So the output of the program is displayed in an ascending order (From A-Z if applies). However, I want my program to just output the first 100 words by ignoring the rest of it. Is there a Unix command that allows me to carry out this function? or do I have to implement a code/algorithm within my program in order to accomplish it?
It should be something like:
Scanner sc2 = null;
try {
sc2 = new Scanner(new File("file.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int numberword=100;
int count=0;
while (sc2.hasNextLine() && count<numberword) {
Scanner s2 = new Scanner(sc2.nextLine());
boolean b;
while (b = s2.hasNext() && count<numberword) {
String s = s2.next();
count++;
System.out.println(s);
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Hi i have a program that doesnt seem to be registering. I am fairly new so I would appreciate any help and there might be stupid mistakes made :/
But the point of the program is the enter a name and then find the name in a file called names.txt and then show the popularity of the name throughout the century. I currently have a program that doesnt seem to be working. Help Please
import java.io.*;
import java.util.Scanner;
public class Babynames{
public static void main (String[] args)throws FileNotFoundException{
Scanner reader = new Scanner(new File("names.txt"));
Scanner input=new Scanner(System.in);
System.out.print("What name would you like to search up: ");
String name = input.nextLine();
Scanner lineScan = new Scanner(name);
String thisname = lineScan.next();
if (name.equals(name))
{
while (lineScan.hasNextInt())
{
int next = lineScan.nextInt();
for (int i = 1900; i <=2000; i+=10)
{
System.out.print(i + next);
}
}
}
else
{
System.out.println("File not found! Try again: ");
String filename = input.nextLine();
Scanner lineScan2 = new Scanner(name);
}
}
}
Edit
it just asks for the name and after that the program ends
Your assignment seems to be:
Accept a baby name as input
find that name in a file that includes some information about that name
output the result which includes the info about the name.
From your code, I'm making an educated guess that a line in your file looks like:
name value value value value value value value value value value value
Where the values represent the popularity of the name 1900 - 2000 by decade (11 values)
So, your program would need to:
Get the user input (name) from System.in using a Scanner .
Open the file
Loop, reading a line from the file
Split the line by space (" ") into a String[] array
Compare the first element (array[0]) to see if it's your name
if it is, go through the rest of the array and output the values
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i have been working on a project that would scan a textfile line per line and at each line, each word in the line will be stored in an array
here is my code as of now. when it comes to the storing from the Answer.txt file, an error occurs. can someone please help me?
try
{
String s = sc.nextLine();
//System.out.println(s);
String[] Question = s.split(" ");
for(int i=0;i<=Question.length;i++)
{
System.out.println(Question[i]);
}//debug
s = sc2.nextLine();
//System.out.println(s2);
String[] Answer = s.split(" ");
for(int c=0;c<=Answer.length;c++)
{
System.out.println(Answer[c]);
}//debug
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("...");
}
You're probably getting ArrayIndexOutOfBounds exception.
for(int i=0;i<=Question.length;i++)
Should be:
for(int i=0;i<Question.length;i++)
^
(The same for the other loop).
Why?
Remember that arrays are zero-based in Java. So if you have an array of size N, the indexes will be from 0 to N - 1 (total sum of N).
You can avoid the index counting all together with an "foreach" loop.
for (String s: Question){
System.out.println(s);
}