Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a simple program in progress that needs the declaration lines
read = new BufferedReader(new FileReader("marks.txt"));
and
line = read.readLine();
to be class variables. How would I do this?
Here is the code I wrote so far.
import java.io.*;
import java.math.*;
public class WriteKong
{
public static String line;
public static BufferedReader read;
public static PrintWriter write;
public static void main(String[] args) throws IOException
{
read = new BufferedReader(new FileReader("marks.txt"));
line = read.readLine();
while(line != null)
{
System.out.println(line);
read.readLine();
}
}
public static void sort()
{
// THIS IS WHAT THE FUNCTION DOES:
// > check the fourth digit in the line
// > if there is no fourth digit then store the mark
// > if mark is less than 50 then write to "fail.txt"
// > if mark is 50 or greater then write to "pass.txt"
}
}
EDIT: I want these variables to be declared as a class variable. I don't want to go through the pain of redefining the same variables in all of the methods I use.
They are class variables in your code. The code satisfies the requirements given.
If you're confused why your loop does not read all the lines from the file it's because you never assign the newly read line to to line.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Trying to make it so pasting a 60 character long url, will print only the first 56 characters. I have only ever used Java in high school, so I am very inexperienced.
import java.util.Scanner;
public class CopyOfCopyOfethanrun
{
public static void main()
{
Scanner kb = new Scanner(System.in);
link();
}
static void link(Scanner kb, String[] args)
{
String link;
System.out.print("\n\n Enter Link: ");
link = kb.nextLine();
String input = link;
String firstfiftysix = "";
if (input.length() > 56)
{firstfiftysix = input.substring(0, 56);}
else{firstfiftysix = input;
}
System.out.println(firstfiftysix);
}
}
[Here is an image showing the error I am experiencing][1]
[1]: https://i.stack.imgur.com/T6ux7.png
Your link method has 2 arguments: When invoking link, you need to provide [A] a Scanner instance, and [B] an instance of an array of Strings.
When you call link();, you provide neither of those. Presumably you want link(kb, args); there. Your main must look like:
public static void main(String[] args)
it currently doesn't. Once you fix that, voila, you have your args. But, you should just get rid of that, you don't use it anywhere in link.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
How would you go about adding the total number to all the integers inside an array element?
My code below is what I have and the issue is the multiple numbers are all displayed in different rows but I can't get them to add together because its all considered one integer.
This is what my output looks like.
462085
361250
351477
328955
But when I attempt to alter the numbers in any way I get something like this,
+2
462087
361252
351479
328957
When I really want to get just get the total sum of the numbers.
Desrired Output:
1503767
I attempted to use .parseInt() but that did not seem to make a difference.
import java.io.*;
import java.nio.charset.StandardCharsets;
public class babySort {
public static void main(String[] args) {
File inputFile = new File("src/babynames.txt");
try (BufferedReader br = new BufferedReader(new FileReader(inputFile, StandardCharsets.UTF_8))) {
String input;
String maleNames;
while ((input = br.readLine()) != null) {
// process the line
String[] inputSplit = input.split("\\s+");
// System.out.println(inputSplit[2]);
int maleBb = Integer.parseInt(inputSplit[2]);
System.out.println(maleBb);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
You want the total. Declare a variable to compute the total with before your loop. Add the values to the total. Print it after your loop. Like,
int total = 0;
while ((input = br.readLine()) != null) {
String[] inputSplit = input.split("\\s+");
// int maleBb = Integer.parseInt(inputSplit[2]); // what is a maleBb?
total += Integer.parseInt(inputSplit[2]);
}
System.out.println(total);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Improve this question
I have a text corpus, which I have to read, split, sort and perform other operations on it.
In the very beginning, when I split it, I see that the Scanner only reads one line. This is the code:
public class CorpusTest {
public static void processCorpus(Scanner scanner) throws IOException{
String line="0";
while (scanner.hasNextLine()) {
line = scanner.nextLine();
}
String[] w = line.replaceAll("[^a-zA-Z\\s]","").toLowerCase().split(" ");
for (int i = 0; i < w.length; i++) {
w[i].trim();
}
System.out.println("Word" + "\t" + "Frequency");
System.out.println(Arrays.toString(w));
}
public static void main(String [] args) throws IOException{
File temp = new File("input.txt");
Scanner scanner = new Scanner(temp);
CorpusTest.processCorpus(scanner);
}
}
I tried adding:
String text = new Scanner( new File("input.txt") ).useDelimiter("\\A").next();
But I get errors because in the method above I am working with an array.
The while loop only reads the last line, which is no good.
I'm not sure what your issue is, and it seems as if you might be trying to make things more difficult than they need to be. Why not simply read your lines in with the Scanner, one at a time, put them into a StringBuilder, and then when the text has been read in, convert to a String and manipulate your String to your hearts content?
#user2864740 helped me out with redirecting me to the right source. I used this instead of the loop in the beginning of my code:
String content = new Scanner(new File("input.txt")).useDelimiter("\\Z").next();
String[] w = content.replaceAll("[^a-zA-Z\\s]","").replaceAll("\n","").toLowerCase().split(" ");
Now it works.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to read input from a text file in java. I have a text file as follows.
5
4
abcd
6
8
defgh
10
I want to read each character from file as a separate entity and work on that character individually like storing 4 in database separating abcd as a b c d and work on them individually.
What are the various ways to do it. What is the most efficient way.
The easy way (and short) if you use Java 7:
List<String> lines = Files.readAllLines(Paths.get("path to file"), StandardCharsets.UTF_8);
It will put all file data to list where list item represents one row
Use read line if your file has new lines.
String sCurrentLine;
br = new BufferedReader(new FileReader("C:\\testing.txt"));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
If you want to use each character individually, then using a Scanner might be the way to go:
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class SOExample {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(new File("myfile.txt"));
sc.useDelimiter("");
while (sc.hasNext()) {
String s = sc.next();
if (s.trim().isEmpty()) {
continue;
}
System.out.println(s);
}
sc.close();
}
}
output:
5
4
a
b
c
d
6
8
d
e
f
g
h
1
0
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I am following this tutorial, for testing and this was a little test/lesson we were doing. I keep getting errors on the 11th line. Can someone tell me whats going on? This is the code, except its not complete.
import java.util.Scanner;
public class Test2Coding {
public static void main(String[] args) {}
{}
Scanner in = new Scanner(System.in);
int age;
System.out.println("How old are you?");
}
You have some extra brackets in the code. Your 3 lines of code should be directly inside the brackets after the main method declaration.
public static void main(String[] args) {
// code here
}
Just remove the brackets you are not using
import java.util.Scanner;
public class Test2Coding {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int age;
System.out.println("How old are you?");
}