First, I want to thank you for helping if you did, if not, thanks for checking it anyway. Second, I have been working on this for awhile, but I only am getting baby steps towards my goal. I get this far and I get compile correctly, but when I run it I get:
Exception in thread "main" java.lang.IllegalArgumentException: No enum const class project 2$Convert.bi
at java.lang.Enum.valueOf
at project2$Convert.valueOf
at project2.main
Essentially, what I am trying to do is this: I have an input file has numbers like FFFFhex, 1010bin, 0013dec, 7777oct, and I want to convert it to decimal. I have to read these numbers one at a time and store them in a list. Then after they are all read and converted, I have to sort, print, and write the list into a new file.
import java.io.*;
import java.util.*;
public class project2
{
public enum Convert { bin, oct, hex, dec}
public static void main(String[] args)
throws IOException
{
//Declare variables
String bin;
String oct;
String dec;
String hex;
String input;
String output;
String str;
Scanner inFile = new Scanner(new FileReader("E:\\LIFE.txt"));
PrintWriter outFile = new PrintWriter("E:\\REPORT.txt");
//Prompt
System.out.println();
System.out.println("Please be sure that the file LIFE is located in the root folder.");
System.out.println("An example of this is C:");
System.out.println("The REPORT file will be in the same location.");
//Switch that will convert numbers
while (inFile.hasNext())
{
str = inFile.next();
switch(Convert.valueOf(str.substring(4,6)))
{
case bin:
int binary= Integer.parseInt(str,2);
break;
case oct:
int octal= Integer.parseInt(str,8);
break;
case hex:
int hexi= Integer.parseInt(str,16);
break;
case dec:
str = str.substring(0,4);
break;
default: System.out.println("default");
}
}
}
}Looks like your input data has a value that is not one of your enum constants. Try printing str.substring(4,6) immediately before the switch.
Read the contract of the substring method; substring(4, 6) == 2 characters.
Also get in the habit of using uppercase characters for enums i.e. `BIN' instead of `bin'.
Due to some error, my account isn't activated as Hanyouslayer, so I made this one as a temp.
I changed my str.substring(4,6) to str.substring(4,7) (thanks to ~s.o.s~) because it was only taking in bi instead of bin. The first number on my list is 1010bin. When I put it outside the switch, it did return bin. Haha, there I go with lapse of attention to detail. I have solved the problem. The reason why it was having problems was because I wasn't being specific and it was reading the entire part, 1010bin, but I fixed it so it only reads 1010 when it converts.
ex of fixed code: This was for the binary conversion to decimal
int binary= Integer.parseInt(str.substring(0,4),2);
Thanks a lot guys for the help! I will flag this as solved when I get my account fixed.
>Thanks a lot guys for the help!
Good luck with the remaining exercises.
> I will flag this as solved when I get my account fixed.
Done and done. :-)
Well, I figured that part out, but I ran into something else that I have been trying to figure out all day. What I am having problems with now is inserting my converted numbers into my list. I have the list code down, but .add isn't working. I can only get it to take in the unconverted numbers. When I try and add my int binary in, it gives me cannot find symbol.
After this I surely wont need any help. I am fairly certain I can figure the rest out after this.
Thanks again.
import java.io.*;
import java.util.*;
public class project2
{
public enum Convert { bin, oct, hex, dec}
public static void main(String[] args)
throws IOException
{
//Declare variables
String str;
int MAX = 12;
int[] converted_list = new int[MAX];
Scanner inFile = new Scanner(new FileReader("E:\\LIFE.txt"));
PrintWriter outFile = new PrintWriter("E:\\REPORT.txt");
//Prompt
System.out.println();
System.out.println("Please be sure that the file LIFE is located in the root folder.");
System.out.println("An example of this is C:");
System.out.println("The REPORT file will be in the same location.");
System.out.println();
//Switch that will convert numbers
while (inFile.hasNext())
{
str = inFile.next();
switch(Convert.valueOf(str.substring(4,7)))
{
case bin:
int binary= Integer.parseInt(str.substring(0,4),2);
break;
case oct:
int octal= Integer.parseInt(str.substring(0,4),8);
break;
case hex:
int hexi= Integer.parseInt(str.substring(0,4),16);
break;
case dec:
str = str.substring(0,4);
break;
default: System.out.println("One of the numbers in the input file is incorrect. Ex. input 1010bin");
}
//List for converted numbers
List<String> ls=new ArrayList<String>();
ls.add(str.substring(0,4));
Iterator it=ls.iterator();
while(it.hasNext())
{
String value=(String)it.next();
System.out.println(str.substring(0,4));
}
}
}
}COMPLETE!
import java.io.*;
import java.util.*;
public class project2
{
public enum Convert { bin, oct, hex, dec}
public static void main(String[] args)
throws IOException
{
//Declare variables
String str;
Scanner inFile = new Scanner(new FileReader("E:\\LIFE.txt"));
PrintWriter outFile = new PrintWriter("E:\\REPORT.txt");
//Prompt
System.out.println();
System.out.println("Please be sure that the file LIFE is located in the root folder.");
System.out.println("An example of this is C:");
System.out.println("The REPORT file will be in the same location.");
System.out.println();
List<Integer> ls = new ArrayList<Integer>();
//Switch that will convert numbers
while (inFile.hasNext())
{
str = inFile.next();
switch(Convert.valueOf(str.substring(4,7)))
{
case bin:
int binary= Integer.parseInt(str.substring(0,4),2);
ls.add(binary);
break;
case oct:
int octal= Integer.parseInt(str.substring(0,4),8);
ls.add(octal);
break;
case hex:
int hexi= Integer.parseInt(str.substring(0,4),16);
ls.add(hexi);
break;
case dec:
int deci= Integer.parseInt(str.substring(0,4),10);
ls.add(deci);
break;
default: System.out.println("One of the numbers in the input file is incorrect. Ex. input 1010bin");
}
}
//Output unsorted
System.out.println("Unsorted");
System.out.println(ls);
//Output unsorted to file
outFile.println("Unsorted");
outFile.print(ls);
outFile.println("");
outFile.println("Sorted");
//Sort
Collections.sort(ls);
//Output sorted
System.out.println("Sorted");
System.out.println(ls);
//Output sorted to file
outFile.print(ls);
//Close files
inFile.close();
outFile.close();
}
}A few observations:
- Always perform resource cleanup in the `finally' block to be rest assured that the cleanup indeed is performed even if the code ends up throwing an exception.
- The way you have written your code resembles the procedural ad-hoc way of writing code. Break up your logic in methods and create logical classes which represent your problem domain.
Taking a look at the sticky created at the top of the forum might just help you get started in the right direction.
I was going to make two classes and a few methods, maybe about 6, and in order to break it up into classes, I went with a driver and a worker class. Upon doing it it came up with errors when compiling, however, if I run it it will work fine. I get 15 errors in total:
.\converter.java:3: cannot find symbol
symbol : class Scanner
location: class converter
static Scanner console = new Scanner(System.in);
^
.\converter.java:3: cannot find symbol
symbol : class Scanner
location: class converter
static Scanner console = new Scanner(System.in);
^
.\converter.java:14: cannot find symbol
symbol : class Scanner
location: class converter
Scanner inFile = new Scanner(new FileReader(infile));
^
.\converter.java:14: cannot find symbol
symbol : class Scanner
location: class converter
Scanner inFile = new Scanner(new FileReader(infile));
^
.\converter.java:14: cannot find symbol
symbol : class FileReader
location: class converter
Scanner inFile = new Scanner(new FileReader(infile));
^
.\converter.java:15: cannot find symbol
symbol : class PrintWriter
location: class converter
PrintWriter outFile = new PrintWriter(outfile);
^
.\converter.java:15: cannot find symbol
symbol : class PrintWriter
location: class converter
PrintWriter outFile = new PrintWriter(outfile);
^
.\converter.java:25: cannot find symbol
symbol : class List
location: class converter
List ls = new ArrayList();
^
.\converter.java:25: cannot find symbol
symbol : class ArrayList
location: class converter
List ls = new ArrayList();
^
.\converter.java:32: cannot find symbol
symbol : variable Convert
location: class converter
switch(Convert.valueOf(str.substring(4,7)))
^
.\converter.java:34: cannot find symbol
symbol : variable bin
location: class converter
case bin:
^
.\converter.java:39: cannot find symbol
symbol : variable oct
location: class converter
case oct:
^
.\converter.java:44: cannot find symbol
symbol : variable hex
location: class converter
case hex:
^
.\converter.java:49: cannot find symbol
symbol : variable dec
location: class converter
case dec:
^
.\converter.java:71: cannot find symbol
symbol : variable Collections
location: class converter
Collections.sort(ls);
My driver method:
import java.io.*;
import java.util.*;
public class project2
{
public static void main(String[] args)
throws IOException {
converter convertObject = new converter();
convertObject.converting();
}
} And my worker:
public class converter
{
static Scanner console = new Scanner(System.in);
public void converting()
{
//Declare variables
String str;
String infile;
String outfile;
//Get file locations
System.out.println("Enter location of the input file.");
System.out.println("An example of this is C:");
infile = console.next();
System.out.println("Enter location of the input file.");
outfile = console.next();
Scanner inFile = new Scanner(new FileReader(infile));
PrintWriter outFile = new PrintWriter(outfile);
//Create list
List<Integer> ls = new ArrayList<Integer>();
//Switch that will convert numbers
while (inFile.hasNext())
{
str = inFile.next();
switch(Convert.valueOf(str.substring(4,7)))
{
case bin:
int binary= Integer.parseInt(str.substring(0,4),2);
ls.add(binary);
break;
case oct:
int octal= Integer.parseInt(str.substring(0,4),8);
ls.add(octal);
break;
case hex:
int hexi= Integer.parseInt(str.substring(0,4),16);
ls.add(hexi);
break;
case dec:
int deci= Integer.parseInt(str.substring(0,4),10);
ls.add(deci);
break;
default: System.out.println("One of the numbers in the input file is incorrect. Ex. input 1010bin");
}
}
//Output unsorted
System.out.println("Unsorted");
System.out.println(ls);
//Output unsorted to file
outFile.println("Unsorted");
outFile.print(ls);
outFile.println("");
outFile.println("Sorted");
//Sort
Collections.sort(ls);
//Output sorted
System.out.println("Sorted");
System.out.println(ls);
//Output sorted to file
outFile.print(ls);
//Close files
inFile.close();
outFile.close();
}
}Your second file is missing the class imports. I don't see the declaration of your `Convert' enum class. Your naming conventions are still off.
These are pretty simple error messages to hunt down and fix; I'd recommend reading the sticky at the top of the forum.
I got it to work thanks to all your help. Thank you very much. I am now in the process of reading/exploring the stuff the sticky.