Searching Through Lines Of Code

Jan 29, 2014

If you were given a problem to search for an error in a section of code but it is incredibly long, what are some ways you can go through the lines of code quickly?

View Replies


ADVERTISEMENT

Code To Open Up TXT File And Display Only First Five Lines?

Apr 7, 2013

I am trying to write a java code for a homework problem in my intro java class but im having problems. This is the problem:

Write a program that open the MyFile.txt and read from a file with these criteria.

1)-The program should display the first five lines of the file’s contents only.

2)-If the file contains less than five lines, it should display the file’s entire content. You must check for end of file and less than 6 lines on the loop iterations.

3)- your program should use loop to read the line by line of the file MyFile.txt.

The serperate myfile.txt has this

I can still remember, at the end of that year, we sat for the last time in our circle together. She played us her favorite song, "like a bridge over troubled water" and quietly passed out a gift she had made for each of us.She had made each of us a pottery heart with our name and the date on it on the back each read "I believe in ME".We all cried including her. She told us she would always remember US. We believed she would.

and this is what the code is suppose to look like after you run it : Java33.png

And this is what i have so far : Java22.png

what needs to go next to complete the javascript to show the first 5 lines of the text files?

View Replies View Related

Code That Reads In Lines Of Text And Returns Number Of Spaces

Nov 28, 2014

I am trying to make a java code that reads in lines of text and returns the number of spaces in each line.I think i have made it but i can not compile it..

Here is my code:

class Mainh {
public static void main( String args[] ) {
System.out.print( "#Enter text : " );
String text = BIO.getString();
while ( ! text.equals( "END" ) )

[Code] ....

View Replies View Related

Searching ArrayList Of Objects

Mar 23, 2014

Okay, I will be as succinct as possible. I am writing a rudimentary book store program for homework. The program consists of two class files and a main. The main issue I am having is on getting the search portion of my program to work. I will post as little as I can and still make sense. This is the block of code belongs to my public Book getBook() method, which is of the BookStore class.

View Replies View Related

Searching And Sorting Algorithm

Jan 2, 2015

I have a code that is meant to read a file and organize all the names from least to greatest salaries. It also allows the user to enter a name to find from the file, and the program finds the name and displays it. I have two errors, and I will show the error in my code

View Replies View Related

Searching For Object In LinkedList?

Nov 3, 2014

I'm doing LinkedList at the moment and I'm having a bit of trouble with my assignment. The part I'm struggling with is remove an employee from a training course (as specified by their employee number),

what I'm confused about is iterating through the linked list to find the employee we're looking for. What I would do in this situation if I was using an array list is

for(Employee emp : myList) {
if(emp.getEmployeeNumber().equals(searchedNumber)) {
remove from training course..
break;
}
}

"Can only iterate over an array or an instance of java.lang.Iterable" is what it is telling me, and I can't figure out why/how its done differently for linked lists.

View Replies View Related

String Searching For A Word

Mar 25, 2015

I'm very new to Java, and I'm writing a code to search a string to see how many times the word "dog" is found in it. I'm not sure if this is error-free or the most efficient, but I'd like to keep it simple.

public void run()
{
 
String input = new String("The Dogman was no ordinary dog, nor man, but rather a peculiar dog-like man who barked like a dog, and panted like a dog, he even ate like a dog. He owned a dog named Doglips, and interestingly enough, his favorite food was hotdogs.");
 
println(input);
int index = -1;
int count = 0;
print("Counting dogs:");
inputarray = input.split(" ");
 
[Code] .....

View Replies View Related

Searching / Entering Name And Number

Jun 28, 2014

import java.io.*;
public class pho
{
public static void main (String args[]) throws IOException
{
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
String name = new String [50];
long phn = new long [50];
System.out.println("enter A for adding phone numbers");

[Code] .....

View Replies View Related

Searching And Sorting Arrays

Nov 2, 2014

My code is not working properly. The ascending and descending numbers are not showing up. I believe what it is printing is the memory location. In this lab you will be coding a program that will make use of functions to search and sort an array. There will also be a print method, again complete with a full menu system. The Menu options are listed below in the section labeled menu.You will need to set up a hundred (100) position integer (int) array that is defined in main. You will also need an integer (int) variable called size. By doing this, you will have to pass the array and the size to each method you write.

Menu:

The menu should have the following eight options:
1. Fill the array with random numbers (1 -100)
2. Print the array
3. Sort the array in ascending sequence
4. Sort the array in descending sequence
5. Sequential search of the array for a
6. Binary search of the array for a target
7. Exit (this can be option zero if you prefer)

From these seven Options, one can see that six methods will be needed. Each of the six main functionalities above will need a function that does what they say. When printing the array, it is required to print the position number alongside the value. Please start your positions at zero, and not one. When doing the sorting methods, please use two different sorting algorithms. (ie, use a Min Max sort for ascending and an enhanced bubble for descending.)

For the Searching methods: you should ask for the target (number the user is searching for) in the dispatch method. Then pass the target to the search method. The search method should return the position it was found at (0 - Size) OR -1 if it was not found. Then have the appropriate messages print inside of the dispatch method.You could write another function that does this part if you wish to keep your dispatch method cleaner and more organized. But that is up to you.

import java.util.Arrays;
import java.util.Scanner;
import java.util.Random;
public class Lab9 {
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int [] values = new int [100];

[code]....

View Replies View Related

Searching Linked List

Apr 30, 2014

Ok here I have a code that generates 1 million random values then converts them to a string then hashcode. I then insert into a linked list and then I want to run through each hash and find it in the linked list timing each run then averaging out the time at the end.

It works great for smaller amounts of numbers it is searching for (fine under 50 thousand searches for the for loop starting at line 24 LinkedListTest.java) but when I try to do the full million searches it gives me "a Exception in thread "main" java.lang.StackOverflowError" at line 158 in List.java. Maybe im getting tired but I cannot figure out why.

// class to represent one node in a list
class ListNode< T >
{
// package access members; List can access these directly
T data; // data for this node
ListNode< T > nextNode; // reference to the next node in the list

[code]....

View Replies View Related

Searching ArrayList By Object Variable?

Oct 7, 2014

I'm almost finished my Bank Account exercise and I found myself stuck at the last part. Its asking me to add a method that asks the user to input the name of the account into which they want to deposit money, then search the ArrayList for that account. If it is found, the user is asked how much money they wish to deposit.

I already have my deposit method sorted so basically what I need is just searching through the ArrayList by the name variable. I assume its don't by iterating through with some form of for loop.Heres what I have:

import java.util.Scanner;
public class BankAccount {
private double balance;
private String name;
public BankAccount(double balance, String name){
this.balance = balance;

[Code]......

And the driver class

import java.util.ArrayList;
import java.util.Scanner;
public class BankDriver {
Scanner scan = new Scanner(System.in);
ArrayList<BankAccount> list;
public BankDriver(){

[Code]...

View Replies View Related

Searching For A Word In Text File

Apr 17, 2014

I'm creating this small JAVA programme thats checks for a word in the dictionary but when I run it I get an error

"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at spellChecker.project1.SpellChecker.main(SpellCheck er.java:10)"

This is my code:

package spellChecker.project1;
import java.io.FileReader;
import java.util.Scanner;
 public class SpellChecker{
public static void main(String[] args){ 
String myFileName = "/Users/Stalin/Desktop/dictionary.txt";

[Code] ....

View Replies View Related

Searching Array Of Objects In Java

Jul 23, 2014

'm working on a program that is to act as an inventory for a book store. There are two classes (Store and Book), and numerous methods which you will see in my code. The program is supposed to read in an inventory from a file which contains 10 books (each row in the file represents a book with an ISBN number, the price of the book, and the number in stock)and store this file into an array of type Book[]. There is then to be another method to process a purchase interactively. The Store class is also to keep a count of how many books were sold at the time of closing and also how much was made that day.

I'm having some difficulty figuring out how I should set up the purchase processing method though. I've written out how I think it would be set up (not too familiar with the terminology, but I think it would be called pseudo code?) I'll include that now along with a few other notes.

public Book[] purchase(String isbn, double price, int copies) {
int itemsSold;
double totalMade;
Scanner input = new Scanner(System.in);
System.out.println("Please enter the ISBN number of the book you would like to purchase: ");

[Code] ....

So basically here's what needs to be accomplished with this portion of code:

1.Ask the user to enter the ISBN number of the book they'd like to purchase.
2.Search the array for the object that contains that ISBN.
3.If the ISBN isn't found in the array, display a message stating that we don't have that book.
4.If the ISBN is found but the number of copies is 0, display a message saying the book is out of stock.
5.If the ISBN is found and the number of copies is greater than 0, ask the user how many copies they'd like to purchase.
6.If the number they enter is greater than the number of copies of that book in the array, display a message stating that and ask them to enter another quantity.
7.Once the purchase is complete I need to update the array by subtracting the number of copies of that particular book that was purchased.
8.Print the updated array.
9.Display a count of how many books were purchased, and how much money was made from the purchase.

I know that code has a lot of holes in it, but I'm just trying to get everything together and figure out how to do each step.

The part I'm stuck on right now is trying to figure out how to search the array to first see if the ISBN that was entered is in the array, and then to find the number of copies of the book with that ISBN number.

In case it will visualize what this array looks like, here's a print out of the array from a sample run I just did.

ISBN: 1234567, Price: 31.670000, Copies: 0
ISBN: 1234444, Price: 98.500000, Copies: 4
ISBN: 1235555, Price: 27.890000, Copies: 2
ISBN: 1235566, Price: 102.390000, Copies: 6

[Code] ....

View Replies View Related

Passing A Method And Searching Array

Apr 22, 2014

How to pass the methods listed below.

public ArrayList<Person> findPerson(String searchFor)

^ this method is suppose to search an array and find whatever you search (numerical or alphabetical)

I have this

public ArrayList<Person> findPerson(String searchFor) {
Main.handleSearchPerson(keyboard, searchFor);
ArrayList<Person> matches = new ArrayList<>();
for (Person p : personList) {
boolean isAMatch = false;

[Code] .....

I am not sure if how i am searching the method is correct or how to pass that to another class to put it inside of a switch statement.

View Replies View Related

Searching Through Array / Find Name And Print

Oct 5, 2014

An array which contain a list of names. I let for a user input and I use that to search through the array. For example lets say the string name "Christian" is stored inside the names array and the user types in Chri, the program looks in the array and finds a name that contains Chri and prints them out. How do I go about doing this?

View Replies View Related

Searching Through Array (List Of Names)

Oct 5, 2014

An array which contain a list of names. I let for a user input and I use that to search through the array. For example lets say the string name "Christian" is stored inside the names array and the user types in Chri, the program looks in the array and finds a name that contains Chri and prints them out. How do I go about doing this?

View Replies View Related

Searching Directories Recursively For File

May 8, 2015

I have a case: i have to make fast searching of a file in to all file systems(Linux or Windows). I use search directories recursively for file. Is there a quick way?

View Replies View Related

Passing A Method And Searching Array

Apr 22, 2014

How to pass the methods listed below.

public ArrayList<Person> findPerson(String searchFor)

^ this method is suppose to search an array and find whatever you search (numerical or alphabetical)

i have this

public ArrayList<Person> findPerson(String searchFor) {
Main.handleSearchPerson(keyboard, searchFor);
ArrayList<Person> matches = new ArrayList<>();
for (Person p : personList) {
boolean isAMatch = false;
if (p.getFirstName().equalsIgnoreCase(searchFor)) {
isAMatch = true;

[Code] ....

I am not sure if how i am searching the method is correct or how to pass that to another class to put it inside of a switch statement.

View Replies View Related

Basic Java GUI Application - Array Searching

Nov 30, 2014

I am currently working on a application for a car park system. It uses GUI created by myself and holds 15 parking spaces. The user has the option to add, delete or search for a specific car. When the user adds a new car, details of the car are entered, the cars registration number (saved as a string), and the user had to check one raido button if the car is expensive, large or normal.

Once this information is entered a new instance of a vehicle will be created and the bay the car is allocated to turns green and stored in one of three arrays(expensive, normal or large). I also have a fourth array for creating the parking spaces.Each array can only hold 5 vehicles apart from the fourth which can hold all 15. When searching for a vehicle the user enters the registration of the vehicle they want to find, but I have to search through all three arrays to find it, and if it does not exist showing a message saying so.These are my three arrays

public static ParkingBay[] regularBays = new ParkingBay[5];
public static ParkingBay[] largeBays = new ParkingBay[5];
public static ParkingBay[] expensiveBays = new ParkingBay[5];

String Registration; - saves registration entered from add form
String RegistrationNumber; - saves registration number from search form

I need to search to see if RegistrationNumber is in either of the three arrays if not show a message saying otherwise

View Replies View Related

Searching Though Linked List Given And Displaying It Given Integer

Nov 1, 2014

I need to display an object, which is in a linked list, given a vin number. It is the display_info method. Here is what i have so far:

import java.util.*;
public class Car implements Comparable<Car>, Cloneable
{

private String model;
private String color;
private int year;
private int vin_number;
private double price;

[code]....

View Replies View Related

Reading / Searching Through List Of Strings With StringTokenizer?

Jan 24, 2015

So I need to be able to find the longest increasing sequence in a 2D grid of numbers. A sequence is simply a series of adjacent squares. Squares may not be used twice. Example here.

97 47 56 36 60 31 57 54 12 55
35 57 41 13 82 80 71 93 31 62
89 36 98 75 91 46 95 53 37 99
25 45 26 17 15 82 80 73 96 17
75 22 63 96 96 36 64 31 99 86
12 80 42 74 54 14 93 17 14 55
14 15 20 71 34 50 22 60 32 41
90 69 44 52 54 73 20 12 55 52
39 33 25 31 76 45 44 84 90 52
94 35 55 24 41 63 87 93 79 24

The longest sequence has length 10, consisting of entries (row, column) as follows;

(5,0) with cost 12
(6,0) with cost 14
(6,1) with cost 15
(6,2) with cost 20
(7,2) with cost 44
(7,3) with cost 52
(7,4) with cost 54
(6,3) with cost 71
(5,3) with cost 74
(4,3) with cost 96

I wouldn't be having a problem if I could use a 2D Array but the assignment requires the use of a List and StringTokenizer.

The way it works, a file is read and each row of the grid is an element of the List of Strings. So in the example above if I were to call myList.get(1) it would return 35 57 41 13 82 80 71 93 31 62 since it's the second row and second index value of the List.

If it was a 2D array I would check values at [x-1][x-1],[x-1][x]... to check all sides and diagonals ect ect. But with the List and Tokenizer there isn't a way that I know of that can search through the String using index values.

I wouldn't even know where to start with that. As of now I read the file fine and make the List of Strings and populate it properly, I'm just a little stuck. I should also mention that which ever method I use to search needs to be recursive.

This is what little I have. It reads in the file, creates the list and assigns the rows to the list.

import java.util.*;
import java.io.*;
public class MaxIncreasingSub {
public static void main(String []args) throws Exception{
File file = new File("Ass1Q2_test1");
List<String> myList = new ArrayList<String>();

[Code] .....

View Replies View Related

Searching ArrayList - Cannot Find Symbol For Search Method

Mar 30, 2014

This is the code fragment I have for searching my ArrayList. Now, each contact is stored in the ArrayList with five elements (first name, last name, etc.) and they're Strings. The error I get when I try to compile my program lies within this code fragment. It says it cannot find the symbol for the search method. I'm not quite sure what to do with this error.

int foundIndex = SAAddressBook.search(aBook);
System.out.println();
if (foundIndex > -1)
aBook.get(foundIndex).displayContact();
else {
System.out.println("No Entry Found");
}

View Replies View Related

Searching Large Char Array For Matching Substrings

Feb 10, 2015

I have written an algorithm that compares the chars and returns all the found words in a 20x10 puzzle, but not for a 100x50 puzzle.

The puzzles are loaded in via text file and I have confirmed they're loading correctly.

guesses = String[] of search words
puzzle = char[][] of letters

Why it doesn't work on the large array?

Expected output:
word1, word2, word3

Actual output:
[blank line]

Here's the horizontal algorithm (row by row search)

//temp variable
ArrayList<String> temp = new ArrayList<String>();
try {
//for each guess word
for(String word : guesses) {
int count = 0;

[Code] .....

View Replies View Related

Searching Array - Read From A File That Is List Of Songs

Oct 10, 2014

I have an assignment for my intro class that requires me to read from a file that is a list of songs, their artists, and the year they were released. As seen below, a print line statement prompts the user to enter an artist name, and then it uses a buffered reader to gain input, and then it is supposed to match that input.I realize that this is not a complete statement, but I'm mostly concerned with getting the .indexOf statement to work.Currently it only returns the first object in the array.

for(int i = 0; i < song.length; i++) {
System.out.println("Enter an Artist name");
String input1 = kb.readLine();
if (song[i].getArtist().indexOf(input1) > -1) {
/*tried changing -1 to -2. When I do, it returns the
first array entry, regardless of what I input*/
System.out.println(song[i].toString());
}
}

View Replies View Related

Searching Binary Search Tree For Movie By Rating

May 8, 2015

The purpose of this function is to use a Movie object and a binary search tree to find all movies that have been read in through a file that have a certain rating (G,PG,PG-13,R). The error I am getting is that it seems I am not traversing fully through the tree and therefore only some movies with the rating I search for are output, not all of them with that rating. I have tried figuring out why this is happening and at first I thought my tree was unbalanced, but I am sure it is simply because I am not traversing the tree correctly. I think my implementation in the main is close to what I need but it needs some tweaking. This is my BST class which I created and required to use for this purpose.

public class BinarySearchTree {
/**
* The head of the tree.
*/
private Node root;
/**
* Constructor that declares the head of tree to null.
*/
public BinarySearchTree() {
root = null;
}
/**
* Returns null if head of tree is empty.
* @return null if head of tree is empty.
*/
public boolean isEmpty(){

[Code]...

View Replies View Related

Searching TXT File For A Specific Keyword And Output Whole Line

Feb 12, 2014

I need to search a txt file for a specific keyword and then output all the lines that contain that keyword. Right now I I think I have my search done but I don't know how I would print the whole line.

TextIO.readFile("xxx.txt");
String search;
String word;
int count=0;
TextIO.put("Please enter your search word: ");
search = TextIO.getln();
while (!TextIO.eof()) {
word = TextIO.getln();
count = count+1;
if (search.equalsIgnoreCase(word)==true){
TextIO.put(count + "-");
TextIO.put(word);

Right now it doesnt even let me enter in any values for the search. Not sure what I've done wrong..

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved