Going To NextLine In Java Buffered Reader
Jul 18, 2014
I have a text and I am reading each line in the text with the simple while loop:
BufferedReader br = new BufferedReader(new FileReader(new File(a.txt)));
string line = new String();
while((line = br.readLine())!=null){
if(line.equals("john"))
//skip to next line
else{
//continue something else..
}
}
My question is how do I skip to the next line ? Using apache.commons.io.FileUtils; one could easily have done something like this:
LineIterator it = FileUtils.lineIterator(file, "UTF-8");
String line = it.nextLine(); //this goes to the next line..
How can this be done using BufferedReader ?
View Replies
ADVERTISEMENT
Feb 16, 2015
I have to make an application called miles to meters that converts miles to meters that asks for user input through joption pane and the output can be eather system.out.println or joption pane, I found the code i need but it uses buffered reader for input not joption pane. Here is the source code
import java.io.*;
import java.util.*;
class MetersToMiles{
public static void main (String[] args)throws
Exception{
// 1 meters = 0.00062137119 miles;
[Code] ....
View Replies
View Related
Nov 3, 2014
I've got a .csv file with some text and numeric data. I've used BufferedReader to successfully print the data to the console. Now I need to perform mathematical operations on the numerical data. How do I access the data from the BufferedReader in my calculation methods?
This is what my BufferedReader looks like:
ReadCVS obj=new ReadCVS();
obj.run();
}
public void run(){
String csvFile = "myDataSet.csv";
BufferedReader buffread=null;
String row="";
[Code] .....
What I would like to be able to do is create some loops to calculate totals for some of the elements, but I'm not sure how to access the data from other methods (and potentially classes?).
View Replies
View Related
Oct 13, 2014
I am trying to read the following input (which will be inputted by the user when run) using the BufferedReader approach:
1
*** * * * *** *** *** ***
* * * * * * * * *
* * * *** *** *** *** ***
* * * * * * * * *
*** * * *** *** *** ***
I am reading each line of the input one line at a time and incrementally storing four char positions into an array, so i am able to hold a vertical representation of each column. I.e. column 1 will be stored in array[0].
The problem with my code is that is does not read the last line of the input, it reads all the other inputs before it but just refuses to read the last line and execute the procedure of storing the characters.
Code:
public void defuseBomb(){
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in));
String asciiLine = reader.readLine()+ " ";
int digit = (asciiLine.length())/4;
[Code] .....
View Replies
View Related
Mar 12, 2015
String name="admin";
String fpass="";
try {
BufferedReader in = new BufferedReader(new FileReader("D:Dairy MangamentNew1 Dairy ManagmentPassword.txt"));
fpass = in.readLine();
in.close();
} catch (Exception e) {e.printStackTrace();}
[Code] ....
Its not comparing the user name and password..
View Replies
View Related
Mar 12, 2014
what this code means?
public static String readBuffer(Reader reader, int limit) throws IOException {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < limit; i++) {
int c = reader.read();
if (c == -1) {
return ((sb.length() > 0) ? sb.toString() : null);
[Code] ....
I am particularly confused with the below lines -
if (((char) c == '
') || ((char) c == '
')) {
break;
}
This is how I am calling this code from my application -
BufferedReader bf = null;
StringBuffer stringBuff = new StringBuffer();
String ln = null;
while ((ln = readBuffer(bf, 2048)) != null) {
stringBuff.append(ln);
}
View Replies
View Related
Dec 8, 2014
I'm making a tree of contacts with people's names as one string and their numbers as another. I need to read that in from a .dat that is set up to have two columns, across from the names are the numbers, so i have to read that in, but I'm not sure how. Here is what I have:
Tree<String, String> tree = new Tree<String, String>();
BufferedReader br = new BufferedReader(new FileReader("/Users/katedess/Desktop/animals.dat"));
String read;
while((read = br.readLine()) != null) {
tree.add(read);
}
br.close();
}
View Replies
View Related
Feb 2, 2015
I've been working on a saving system for a game I'm working on using java IO writer and reader. When I tried exporting it (as a executable jar) to test I couldn't get it to work, when it worked before. If I had to guess it's the file location not being able to find the file. Which it just looks like :
FileReader end = new FileReader("Saves.txt");
I don't exactly know how to set it up to read the file. I'd tried giving it a look up but I don't know how to exactly word my problem.
View Replies
View Related
Oct 29, 2014
ive been working on a poker game in java but seem to have got stuck in my 3 of a kind. What I was trying to do was create a loop that would increment a counter every time time is more than 1 instance of a card, but even if the counter increments and I draw a 3 of a kind it still returns false.
private boolean ThreeOfKind(ArrayList<Card> sortedCards) {
Card previousCard = null;
for (Card c : sortedCards){
int kindcount = 0;
[code]....
View Replies
View Related
May 12, 2015
I am asking the user a question and already have a correct answer. The answer is a string but is more than one word.
When the user types in the correct answer, it still comes up as wrong but I only have the input.next method.
Quote
Why are rabbits ears long?
United states of america
Incorrect!
View Replies
View Related
Jan 26, 2014
My program is supposed to let me enter the following info for 3 employees: name, number of hours worked this week, and hourly pay rate. It lets me enter the name for the first employee, but for the second and third, it skips over the name and forces me to enter the number of hours worked and pay rate without a name. I have the code and the output posted below. First the program code:
Java Code:
import java.util.Scanner;
public class SalaryCalc {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String name;
double hours;
double rate;
double grossPay = 0;
double overtime;
[code]...
Enter hours worked this week: mh_sh_highlight_all('java'); Notice I can only enter the name for the first employee (Bigfoot). If I wanted to enter "Nessie" for my second employee's name, it skips over the name prompt and prompts me for the hours worked instead.
View Replies
View Related
Apr 10, 2014
My code is as follows:
import java.util.Scanner;
public class GetUserInput {
public static void main (String args[]) {
int i;
float f ;
String s;
[code]....
My Query is: input.nextLine() does not wait for user input. Instead it continues execution from next line. But if I move up input.nextLine(); before both input.nextInt(); and input.nextFloat() in the above code, the execution works fine, input.nextLine(); waits for user input. edited code and output are as follows.
import java.util.Scanner;
public class GetUserInput {
public static void main (String args[]) {
int i;
float f ;
String s;
[code]....
View Replies
View Related
Jan 29, 2014
I Just made a simple code, but in any case it's not taking the input I'm trying to enter for the city.
View Replies
View Related
Mar 5, 2014
a buffer reader , how does it work and what is the code for it ?
View Replies
View Related
Feb 14, 2015
package jdbc;
import java.sql.*;
import javax.sql.*;
import java.util.*;
public class Jdbc {
public static void main(String[] args) {
[code]....
View Replies
View Related
Feb 12, 2015
How would I go about erasing the first buffered image before displaying the second one and eventually a third and fourth so that it appears the image is moving across the screen?
myBuffer.drawImage(myArray[0].getImage(), xPos, yPos, null);
myBuffer.drawImage(myArray[0].getImage(), xPos + width, yPos, null);
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
public class KarelPanel extends JPanel {
private static final int WIDTH = 395, HEIGHT = 391; //constants
[Code] .....
View Replies
View Related
Sep 22, 2014
Trying to write to a txt file using buffered writer - however I'm getting some issues.
BufferedWriter says 'The resource type BufferedWriter does not implement java.lang.AutoCloseable'
br says 'The type BufferedWriter is not visible'
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class FileWriter {
public static void main(String[]args){
[Code] .....
View Replies
View Related
Sep 8, 2014
I am trying to read an image I have in the location of my project, So I do this:
When I read it in the try/catch like: BufferedImage image = ImageIO.read(new file(""));
And try to access it after the try/catch, it does not know that image exists, so I need to declare it as a global variable for that class first, and then it works.
public class Gui extends JFrame
{
private BufferedImage image1;
public Gui() {
super("MyApp");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
initUI();
[Code] ....
View Replies
View Related
Mar 7, 2010
I wanted to write a method which gets a buffered image as parameter, rotates it 90 degrees clockwise around its top left corner, and then returns it. This method will be call from another method which then draws the rotated image on a rather large background which consists of many other images.
Here is the code I have so far:
public static BufferedImage rotate(BufferedImage img) {
int w = img.getWidth();
int h = img.getHeight();
BufferedImage newImage = new BufferedImage(width, height, img.getType());
Graphics2D g2 = newImage.createGraphics();
g2.rotate(Math.toRadians(90), w/2, h/2);
g2.drawImage(img,null,0,0);
return newImage;
}
This method does rotate an image 90 degrees, but when the calling method recieves this image and displays it on the bigger frame, parts of the rotated image is cut. I think because the frame holding the returned image is not big enough. I've tried playing around with the code a lot, chaging the sizes of different images, and trying AffineTransform features, but I have had no luck.
View Replies
View Related
Jan 10, 2015
I tried to make a buffered screen image that constantly refreshed an image (a black screen) however it does not seem to be working.
Java Code: import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
[code]....
View Replies
View Related
Oct 31, 2014
I am writing a java program that takes a FROM image, a TO image, and a ratio (this is a slide bar in the GUI). Here's my code:
Java Code:
public static BufferedImage rollUp (BufferedImage from,
BufferedImage to,
double ratio) {
BufferedImage finalBufferedImage = new BufferedImage(from.getWidth(),
from.getHeight(),
from.getType());
int packedColor = 0;
for (int r = 0; r < from.getHeight(); ++r) {
for (int c = 0; c < from.getWidth() ; ++c) {
[code]...
So from 0 to 1 (ratio is a double between 0 and 1) the image will "roll up". The effect works completely in the GUI, but the console freaks out at about 0.33 ratio.. This program runs for testing in a class Main and uses a class Splittinimage.. this method is in class TwixPix. When the main class is run, a box pops up with a combo box and a slider. You pick an effect (in this case, roll up) and then slide the slider to set the ratio. The image below those two things performs the effect that was selected. Imagine a PowerPoint presentation slide effect.
View Replies
View Related
Apr 13, 2014
So I've got this class method:
private void createTextBox() {
Graphics g = Game.getG();
Font font = new Font(fontName, fontStyle, fontSize);
g.setFont(font);
height = BORDER_WIDTH*2 + g.getFontMetrics().getHeight();
int stringWidth = g.getFontMetrics().stringWidth(dialog);
width = BORDER_WIDTH*2 + stringWidth;
[Code] .....
This gets called when ever I want to create my Textbox object however what gets created is a purple box (i know that I have set the array elements to purple to begin with) however when I try to use the drawstring method to "draw a string" I get an image which looks like the gif below. I have changed the text size to various sizes but without success. The string is supposed to say "This is a test" but obviously it doesn't.
View Replies
View Related
Feb 12, 2014
How can I write a BufferredImage to an 8-bit .bmp using indexed colors stored in a <String,Color> Hashtable?
I've never used Hashtables before, and I didn't know color indexing existed until now, but I can do most other things in java fairly well.
I'm not looking for code, just the concept, as I really don't know how Hashtables work (although I could figure it out), and how color indexing does. I know how to write image files, just not indexed or with a specific number of bits. I am using Hashtables generated from GIMP.
EDIT: I mainly want to know how to save a BufferedImage as an indexed .bmp.
View Replies
View Related
Apr 2, 2014
I am using netbeans to create a hotel booking system, just tessting out code to get the booking information input to a file when the next button is clicked.
File file = new File("file.txt");
try (BufferedWriter br = new BufferedWriter(new FileWriter(file))) {
br.write("################################");
br.newLine();
br.write(" Booking Information");
br.newLine();
br.newLine();;
br.write("First Name: " + TxtName.getText());
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
Nothing is getting wrote to the file though. Does that code seem right?
View Replies
View Related
Feb 18, 2015
How do I make the file reader object move to the next line if there is no more input on the line. Here is my text and output file as you can see that my text file column cuts off on the 2nd line after 70. I want to read that next line which is 100 into my labs variable however its reading it into my final exams variable. I'll also post the code but I didn't think it was necessary.
textfile:
100908095
1008070
10070
output:
Labs Projects Tests Final Exams
100908095
1008070100
70
[import java.util.Scanner;
import java.io.*;
public class MyGrades
{
public static void main (String[] args) throws IOException
{
int lab, project, test;
int finalExam;//Par and Player values
[Code]...
not sure if I code wrapped it correctly
View Replies
View Related
Apr 9, 2014
So what I do normally is draw the bottom layer of grass, and when I draw the second layer with trees, the tree layer has a shadow, that shadow doesn't go ontop of the grass layer, but instead it overwrites the grass layer aswell and I've checked the color codes, the shadow has combined with white, not the first layer.
it's obvious that that's not how alpha works then, how do I do it?
View Replies
View Related