Splitting Values From A File With String Tokenizer And Storing It In Another Class
Aug 26, 2014
I'm new to java. I have a Product class with getters and setters.
E.g. setProdType & getProdType
I want to store the values from a file into that
StringTokenizer token = new StringTokenizer(line,"**");
while(token.hasMoreElements()) {
int p.setProdType = Integer.parseInt(token.nextElement().toString());
}
View Replies
ADVERTISEMENT
Jan 27, 2015
So I'm having a problem with the .hasMoreElement() method. I try to read lineOfInput for a file which contains:
Hobbit, Long,t@gmail.com,475-555-4444,3
Long, Hobbit,b@yahoo.com,445-222-5342,2
Hobbit,Long,b@yahoo.com,465-222-5342,2
Help,Yerp,,455-222-2222,4
but when i get to the ,, on the last line I get a NoSuchElementException and the program crashes.
StringTokenizer inputField = new StringTokenizer(lineOfInput, ",");
while(inputField.hasMoreElements()) //for each fields is a line
{
lastName = inputField.nextElement().toString();
firstName = inputField.nextElement().toString();
email = inputField.nextElement().toString();
phoneNumber = inputField.nextElement().toString();
level = Integer.parseInt(inputField.nextElement().toString());
}
View Replies
View Related
Apr 15, 2014
So I have this file in which has a few sentences on a single line. What I need to be able to do is read the file and then take each word and create a token out of it. Then what it does is it selects the first letter of every 5th word, uppercase it, and then allow me to use append it via a stringbuffer object to create a word out of it.
I know I'll need to use a string tokenizer but I'm not sure how to do so in a way that makes each word separate and how to tell it to only hit the 1st letter of every 5th word.
Here's what I've come up with so far, but I'm currently at a loss at what to do and my textbook/documentation is just not working.
Java Code:
String line; // The line we read from the file.
char letter; // The first letter of each 5th word.
String sw; // The completed string.
public static void main (String [] args)throws IOException {
[Code] ....
View Replies
View Related
Mar 30, 2014
I am creating a simple Symptom Checker application. The problem I have is that I'm trying to retrieve user input (JTextField) by comma's using StringTokenizer which contacts the database for a result which matches the user's input (SELECT * FROM DIAGNOSIS WHERE ?, ?, ?) . It successfully finds the correct result however only in a particular format. Not different combinations....
for example, if I enter say within the JTextField: "tearful, nausea, lack of motivation" it will find the result successfully (as that is how it is formatted within the particular column (in the database table) i wish to display a result from) however, if i enter a different combination of these symptoms: "nausea, lack of motivation, tearful" - it will not find any result. I'm very unsure how to make it work regardless of what is inputted first, second or last.
Here is the code:
public void actionPerformed(ActionEvent e) {
try {
String abc = fieldsymp1.getText();
StringTokenizer str = new StringTokenizer(abc);
while (str.hasMoreTokens()) {
str.nextToken((", ")).trim();
[Code] ....
View Replies
View Related
Mar 6, 2015
I am having trouble adding numbers read from a file with BufferedReader, using Tokenizer to split the numbers up by " " - space and adding them to an adjacency matrix.Below is the text file and my code, that I have at the moment.
0 1 0 0 1 1 0 0
1 0 0 0 0 1 1 0
0 0 0 1 0 0 1 0
0 0 1 0 0 0 0 1
1 0 0 0 0 1 0 0
1 1 0 0 1 0 0 0
0 1 1 0 0 0 0 1
0 0 0 1 0 0 1 0
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.StringTokenizer;
public class Foo
{
@SuppressWarnings("null")
public static void main(String[] args) throws Exception
{
String line, token = null, delimiter = " ";
[code]....
View Replies
View Related
Apr 9, 2014
What does the line Console[] conList mean? How am I going to access the string values in class consolelist?
class ConsoleInfo
{
private String conTitle;
private double conPrice;
private int conQty;
private String conPic;
[Code] .....
View Replies
View Related
Nov 14, 2014
I am writing a program where I need to split an array of full names into First names and Surnames, using mapping. However, I am struggling how to split it up... and my First Names and Surnames list are both just displaying the full name.
public static void main(String[] args) {
String[] names;
names = new String[8];
Scanner s = new Scanner(System.in);
for (int i = 0; i < names.length; i++) {
System.out.println("Enter full student name:");
[Code] ....
View Replies
View Related
Feb 14, 2014
I'm trying to use the split method to split a string for a calculator, but it keeps throwing this Exception:
Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0
+
public class exp {
private static String ques="88+12";
private static String [] splitCalc;
public static void main(String[] args) {
splitCalc = ques.split("+");
for(String s : splitCalc){
System.out.println(s);
}
}
}
View Replies
View Related
Jul 21, 2014
I am currently trying to split the string "EAM est" between the part. I have gotten the code to work if the was a -. But I can't see why the error is occuring
I have tried
String test = "EAM-testing";
String[] parts = test.split("-");
System.out.println("parts[0] = " + parts[0]);
System.out.println("parts[1] = " + parts[1]);
String test1 = "EAM esting";
String[] parts1= test1.split("");
[Code] .....
The error occurs at line: String[] parts1= test1.split("");
View Replies
View Related
Jan 14, 2014
How do I make a method that can seperate parts of a String. The String is [name,aid]... example [John,5],
I want to remove [ , and ] from the String... How can I do this?
This is what i've done this far:
static public String splitta(String tf)
{
String part1 = "";
String part2 = "";
String part11 = "";
String part22 = "";
String part111 = "";
String part222 = "";
String[] parts = new String[2];
[Code] ....
This dosn't work, it wont compile... It states the following:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
at testprojekt.Validering.splitta(Validering.java:130)
View Replies
View Related
Jan 21, 2014
I am trying to split a string into a String[] tokens array to declare variables for an object; however, I'm having an issue getting the string to tokenize correctly. Here's an example of the input:
a : 100 : John Smith : 20 Main St.
a : 101 : Mary Jones : 32 Brook Rd.
Here is the basic code I have now, to properly sort each line of text, etc. (without the split() method):
Java Code:
while (scanner.hasNextLine()) {
currentLine = scanner.nextLine();
lineScan = new Scanner(currentLine);
if (currentLine.startsWith("/") || currentLine.trim().isEmpty())
continue;
[Code] ....
I was able to eliminate the comments and identifiers from the text by trimming the first two characters of the string. For the split, I tried String[] tempArray = currentLine.split("s+"); however, that also took the spaces out of the addresses and names...so the results looked like this:
100
John
Smith
20
Main
St.
As you can see, it splits via space regardless, including where I replaced all the :'s with spaces. Is there any way to do this?
View Replies
View Related
Aug 6, 2014
I am storing out of range values in int and byte type
public class OverflowDemo {
public static void main(String args[]) {
int value = 2147483647 + 10;
System.out.println(value);
byte b=127+10;
System.out.println(b);
}
}
Program will give error for only byte type as OverflowDemo.java:8: error: possible loss of precision
byte b=127+1; ^
required: byte
found: int
So why not for Integer?
View Replies
View Related
May 20, 2014
Write a complete Student class that allows you to use the statements such as the following in methods in outside classes, with the obvious meanings. Then write an application program that creates one student object, reads in test grades until a negative "grade" is seen (as a signal that the list of grades has ended), then prints out all available information about the Student:
Student bob = new Student ("Robert", "Newhart");
bob.storeGrade (23); // bob scored 23 on this test
return bob.getTotalGrades();; // total of test scores to date
return bob.getAverageGrade();; // for all test scores to date
return bob.getName();; // returns "Robert Newhart"
What I need is in the Student class... I'm not sure how to go about storing the grades without renewing the value of storeGrade (I'm sure what I have right now is incorrect, but I'm stuck). This is what I have so far, as you can see I left some of the grade-related bits blank for now, but all I want to know is how I can store the grades:
Java Code:
public class Student extends Object
{
private int itsTotalGrades;
private int itsAverageGrades;
private String itsFirstName;
private String itsLastName;
private int storeGrade;
public Student (String first, String last)
[Code] ....
View Replies
View Related
May 13, 2014
I am really struggling to make the Split String method work with another method which is double hours.
HtmlElement span = new HtmlElement(SPAN_OPEN, SPAN_CLOSE);
span.setValue("Drive Time: ");
td.addNestedElement(span);
HtmlElement span2 = new HtmlElement(SPAN_OPEN, SPAN_CLOSE);
span2.addAttribute("class", "drive_time");
String time=String.valueOf(showSet.getTransferHours());
/*Split the Strings into Hours and Minutes*/
[code]...
View Replies
View Related
Jul 19, 2014
I am designing the game of Snake but it crashes after i added the checkScore() method.
package com.complex.snake;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;
[Code] .....
The error i get is : Exception in thread "Thread-3" java.lang.ArrayIndexOutOfBoundsException: 1
View Replies
View Related
Jan 29, 2014
I've written a program just for the sake of it (to learn) and it seem's like theres quite a lot in the main method that perhaps could be split up into seperate classes. I'm not too sure where should start with this though, or what my thought process should have been as I was writing the program.
import java.util.Scanner;
public class Loops {
public static void main(String[] args) {
int answer = 16;
Scanner scan = new Scanner(System.in);
// Question
System.out.println("What is 4 x 4 ?");
[code]...
--- Update ---
here's a version without code comments as they might make it harder to read here -
import java.util.Scanner;
public class Loops {
public static void main(String[] args) {
int answer = 16;
Scanner scan = new Scanner(System.in);
System.out.println("What is 4 x 4 ?");
int userAnswerInt = 0;
[code]...
View Replies
View Related
Dec 2, 2014
I am developing a project where i have to split a zip file into fragments. Do we have any code for this splitting of zip files. I upload a zip file and it has to split into fragments and these fragments has to be renamed. I am having problem with splitting the zip file.
View Replies
View Related
Jan 30, 2015
Me and my mate are struggling to get our code to split a line in a text file which are separated by (":") and store into an array. We've managed to split each individual line into an array, but cannot work out how to do it with multiple splits over one line.
try{
FileReader fr = new FileReader("C:UsersTomDocumentsNetBeansPro jectsfootballA2_Tom_Tyler2srcukackingsto nci5100football_a2 om_mcLean_tyler_wattsmo delfootball_teams_phase1a.txt");
BufferedReader br = new BufferedReader(fr);
while(br.ready()){
String nextLine = br.readLine();
String[] fTArray = nextLine.split(",");
System.out.println();
for(String str : fTArray) {
System.out.println(str);
}
}
This code works but how would we do it for multiple splits over one line, along with this array?
View Replies
View Related
Feb 21, 2014
I am new to a Java. I have a small project where I have a file of 10 million records of data located under d:/test/orders.csv
I would like to split this file into manageable file sizes of 5MB with the same name and be stored under a separate folder d:/test/output e.g
orders1.csv
orders2.csv
orders3.csv
..............
ordersn.csv
View Replies
View Related
Nov 7, 2014
I have a .txt file which i am currently using Scanner to input into the console, the text file contains the names of football games followed by their scores:
E.g. Man U : Liverpool : 2 : 1
I need to create an equation to add together all scores to create a string reading something like "total goals: 28" ....
View Replies
View Related
Sep 24, 2014
One of the requirements of my programming assignment is that a user enters a command along with two numbers. For example, if I entered the following command:
I 5 6
The program create an image like this (it wouldn't be outputted though):
00000
00000
00000
00000
00000
00000
It creates a 5 x 6 "image". This is where my troubles begin. The program should also accepts other commands, such as:
L 3 2 F
which would produce (also not outputted):
00000
00F00
00000
00000
00000
00000
Here is my method for creating an "image" with an M * N array
for (int i = 0; i < column; i++) {
for (int j = 0; j < row; j++) {
System.out.print("0");
} System.out.println();
}
The code works, but I need to store the image in an array so it can be changed by other methods (I can't create the image manually every time). I tried doing something like this but it doesn't work:
public static String[][] createImage(int row, int column) {
String[][] image = new String[row][column];
for (int i = 0; i < column; i++) {
for (int j = 0; j < row; j++) {
image[j][i] = "0";
} System.out.println();
} return image;
}
This method outputs as many blank lines as the columns I entered and the memory location of image.
So my question is: how would I store "0" in a 2D array so that it can be accessed and changed by other methods?Or, am I using a 2D array incorrectly and will the image have to be created manually every time? If so, how would I output image if it is created in a separate method?
View Replies
View Related
Dec 3, 2014
I have the following method that I need to implement:
{
// YOUR CODE HERE
File file = new File(filename);
int counter = 0;
String tempArtist, tempName;
Album tempAlbum;
Track tempTrack;
[code]....
Ok so I updated my code from the initial post since I made some progress on my own. I guess now I'm just stuck on how to scan in the file of strings and stock it into the type Track. (I've tried using both the initial linked list for this and a temporary variable with no luck).
View Replies
View Related
Jun 10, 2014
I am building an app, where I have to store the data eg: "hello ! how are you" in java as a string and then use scanner to get input and check if the entered word is present in the line stored. If it is stored then the entire sentence must be displayed. eg : if the stored string is "hello how are you"
if the entered string is "how", then the entire sentence "hello how are you" should be displayed.
View Replies
View Related
Jan 20, 2015
I am having trouble with getting a user input once they enter it in to a JTextArea and then storing it in to a string value. See, I wanted the user to enter an answer in to the JTextArea, and then once I store it in to a string value, I would be able to use it later. I wanted to after storing the variable to print it on to a JLabel, but nothing is showing up... Heres my code.
static String usera[]= new String [10];
for (int i=0; i< usera.length; i++) {
//These are the JTextArea names
usera[0]= Question1.getText();
usera[1]= Question2.getText();
[Code].....
I already initialized the JLabels earlier in my code, and I did not feel the need to post all my code here. This is where I am trying to get the string variable and then printing it out.
View Replies
View Related
Oct 2, 2014
When learning HashMaps in C++ I had to create the whole algorithm. In the code I created I could simply place a string into the method and it would store the names for me by turning the string into a integer and storing is accordingly. If there was a collision it would grow linearly at that location.
//play with Hash Tables
void getNames(String names) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put(names,22);
}
How can I do this in Java. I read about them and look at examples and they all for the most part look like this.
View Replies
View Related
Oct 30, 2014
public class MonthsName
{
static String monthsName[];
public static void main(String[] args) {
String[] months = new String[13];
months[0] = null ;
[Code] .....
I keep getting 1 error that tells me this.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at MonthsName.main(MonthsName.java:22)
Java Result: 1
Just before that netbeans couldn't find the main class. I was wondering if it netbeans or my code that's wrong.
View Replies
View Related