| | |
Change bases in Java
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 3
Reputation:
Solved Threads: 0
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<Enum.java:196>
at project2$Convert.valueOf<project2.java:5>
at project2.main<project2.java:49>
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.
Exception in thread "main" java.lang.IllegalArgumentException: No enum const class project 2$Convert.bi
at java.lang.Enum.valueOf<Enum.java:196>
at project2$Convert.valueOf<project2.java:5>
at project2.main<project2.java:49>
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.
Java Syntax (Toggle Plain Text)
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"); } } } }
•
•
Join Date: Sep 2009
Posts: 7
Reputation:
Solved Threads: 1
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.
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.
Last edited by Dougnukem; Sep 20th, 2009 at 3:53 pm.
•
•
Join Date: Sep 2009
Posts: 7
Reputation:
Solved Threads: 1
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.
After this I surely wont need any help. I am fairly certain I can figure the rest out after this.
Thanks again.
Java Syntax (Toggle Plain Text)
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)); } } } }
•
•
Join Date: Sep 2009
Posts: 7
Reputation:
Solved Threads: 1
COMPLETE!
Java Syntax (Toggle Plain Text)
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.
- 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 don't accept change; I don't deserve to live.
•
•
Join Date: Sep 2009
Posts: 7
Reputation:
Solved Threads: 1
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<Integer> ls = new ArrayList<Integer>();
^
.\converter.java:25: cannot find symbol
symbol : class ArrayList
location: class converter
List<Integer> ls = new ArrayList<Integer>();
^
.\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:
And my worker:
.\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<Integer> ls = new ArrayList<Integer>();
^
.\converter.java:25: cannot find symbol
symbol : class ArrayList
location: class converter
List<Integer> ls = new ArrayList<Integer>();
^
.\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:
Java Syntax (Toggle Plain Text)
import java.io.*; import java.util.*; public class project2 { public static void main(String[] args) throws IOException { converter convertObject = new converter(); convertObject.converting(); } }
Java Syntax (Toggle Plain Text)
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(); } }
Last edited by Dougnukem; Sep 21st, 2009 at 3:01 am.
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.
These are pretty simple error messages to hunt down and fix; I'd recommend reading the sticky at the top of the forum.
Last edited by ~s.o.s~; Sep 21st, 2009 at 10:05 am.
I don't accept change; I don't deserve to live.
![]() |
Similar Threads
- Applying mathematical equations to JAVA code (Java)
- Java Datatypes to SQL Datatypes (Java)
- Java (AWT Lable) & GridBagLayout (Java)
- Simple Java Help (Java)
- Advantages: C++/Java (C++)
- Can I use pointers to non-primitive types in JAVA? (Java)
- Java Gui help (Java)
- Java Tutorials (Java)
- Java Client/Server (Java)
Other Threads in the Java Forum
- Previous Thread: seminar related to java
- Next Thread: DB related query
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle physics plazmic print problem program programming project recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree trolltech unlimited utility webservices windows






