Win a copy of Microservices Testing (Live Project) this week in the Spring forum!

Jason Mackie

Greenhorn
+ Follow
since Feb 07, 2007
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jason Mackie

right on...thanks
15 years ago
i actually used the StringTokenizer to solve the problem...I tried messing around with "split" for a long while and was not able to come to a solution. Here was my finishing code:



thanks to everyone that helped...
Jason
15 years ago
sorry here is the code i just posted might be easier to read this way

15 years ago
it seems to me that the whole "split" thing would work but how would i be able to store them in differnt variables?

in the file im inputing there are 3 "variables" per line. the first one is name which has no spaces, the second is an integer that is called creditHrs, and the last is a double called qualityPts. I was thinking of using an if statement like

if (______ == string or char){
name == _________;
}
else if (_______ == int){
creditHrs = _____________;
}
else if (_______ == double){
qualityPts = ___________;
}

is there any way to do that?
15 years ago
if i have to input 5 separate lines of code like:

Smith 27 83.7
Jones 21 28.35
Walker 96 182.4
Doe 60 150
Wood 100 400

how would i be able to put smith, 27, and 83.7 in separate variables?

here is my code so far...



there is more at the bottom but that is not important! I just need some guidance as to how to read a line of code from a file and then put the 3 different variables in their corresponding variables (im looking for help with the last comment in the snippet of code)
15 years ago
yes I can. That was not specified in the assignment. But I figured it out...gosh it took me forever. Thanks to everyone that helped me. Here is the snipit of code i needed to get it to work. I ran this code 3 separate times with slightly different variables for the three separate lines that i was inputing.



thanks again guys...this site is a big help...now should i delete this whole post thing since i no longer need help?
15 years ago
ok so here is the code that I have and i put a comment block in where I think the remove whitespace code goes



here is the input and output and how it should look

15 years ago
ok sorry to Ernest...

and to Pete,

I have no idea how to do that whole ubb thing. I am using Eclipse and i didnt see anything that said ubb in edit.

the extra whitespaces in my input code are missing but they dont matter. they can be anywhere. I just cant figure out how to get rid of more than one between each word. It should read just like in the output. I tried using the "trim()" method but that had no effect. I think i need to read the words by themselves and then call "trim" but i cant figure out how to do this. Im new to this stuff and its kicking my rear I also looked in my book for string commands and trim was the only promising one in there that i noticed.

thanks for your help...i hope this will further help you help me! lol thanks again
15 years ago
So i have to import a file using a fileInputStream and then take out all the extra white spaces and then output the file into a different file obviously using a fileOutputStream. For the life of me I cant figure out how to remove the extra white spaces. Here is my code and all that good stuff so far:

Code:

import java.io.*;
import java.util.*;
public class FileIO {
public static void main (String[] args){
Scanner inputStream = null;

try{
inputStream = new Scanner (new FileInputStream ("Lab6Input.txt"));
}

catch (FileNotFoundException e){
System.out.println("There is no file with that name");
System.exit(0);
}

String firstLine = inputStream.nextLine();
inputStream.useDelimiter(" ");
String secondLine = inputStream.nextLine();
inputStream.useDelimiter(" ");
String thirdLine = inputStream.nextLine();
inputStream.useDelimiter(" ");
String array[];

System.out.println(firstLine);
System.out.println(secondLine);
System.out.println(thirdLine);

}
}

The input file:

Now is the time for all
good people to come to the
aid of their computers.

What the output file should look like:

Now is the time for all
good people to come to the
aid of their computers.


THANKS FOR YOUR HELP!!!
15 years ago
the exact error i get is "Unreachable catch block for DivisionByZeroException. This exception is never thrown from the try statement body". I am wondering if it has anything to do with the warning i get in my division by zero exception class...it says "The serializable class DivisionByZeroException does not declare a static final serialVersionUID field of type long"
15 years ago
I am also trying to declare my own exception called DivisionByZeroException. Therefore I have a class called DivisionByZeroException and in my Ratio class I added another catch statement to my try block and I am getting an error saying that my division by zero exception catch block cannot be reached. here is the code once for my exception class...


public class DivisionByZeroException extends Exception{

public DivisionByZeroException(){
super ("DivisionByZero error!");
}

public DivisionByZeroException(String message){
super(message);
}
}

____________________________________________________________________________
here is the code for my ratio class...

import java.util.*;
public class Ratio {
Scanner scan = new Scanner (System.in);
int n1, n2;
double r1;
boolean done;

public void ReturnRatio(){
do
{
done = true;
try
{
System.out.print("Please enter the first integer: ");
n1 = scan.nextInt();
System.out.print("Please enter the second integer: ");
n2 = scan.nextInt();
r1 = (double) n1 / n2;
System.out.print(r);
}

catch (InputMismatchException e)
{
scan.nextLine();
System.out.println("Please enter an Integer");
System.out.println();
done = false;
}

catch (DivisionByZeroException e)
{
scan.nextLine();
System.out.println("Please do not divide by zero");
System.out.println();
done = false;
}
}
while (!done);
}

}
15 years ago
ok so I tried that and it wasnt working last night so thats why I asked the "stupid" question lol...but i realized i forgot the public static void main (String[] args) statement...thanks for your help guys
15 years ago
what would the syntax be tho? i have 2 classes...1 called ratio which holds the code above, and the other is called ratioTester which holds the public static void method...what would i put in ratioTester?
15 years ago
Ok so basically i have to call the returnRatio method in my main class...and I have no idea how to call it because im not creating an object and then calling the method on the object. here is my code of the method...


EDIT by mw: Added Code Tags to original poster's indentation.

[ February 11, 2007: Message edited by: marc weber ]
[ February 11, 2007: Message edited by: Bear Bibeault ]
15 years ago
awesome thanks marc that worked...
15 years ago