Change bases in Java

Thread Solved

Join Date: Sep 2009
Posts: 3
Reputation: Hanyouslayer is an unknown quantity at this point 
Solved Threads: 0
Hanyouslayer Hanyouslayer is offline Offline
Newbie Poster

Change bases in Java

 
1
  #1
Sep 20th, 2009
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.
  1. import java.io.*;
  2. import java.util.*;
  3. public class project2
  4. {
  5. public enum Convert { bin, oct, hex, dec}
  6. public static void main(String[] args)
  7. throws IOException
  8. {
  9. //Declare variables
  10. String bin;
  11. String oct;
  12. String dec;
  13. String hex;
  14. String input;
  15. String output;
  16. String str;
  17.  
  18. Scanner inFile = new Scanner(new FileReader("E:\\LIFE.txt"));
  19. PrintWriter outFile = new PrintWriter("E:\\REPORT.txt");
  20.  
  21. //Prompt
  22. System.out.println();
  23. System.out.println("Please be sure that the file LIFE is located in the root folder.");
  24. System.out.println("An example of this is C:");
  25. System.out.println("The REPORT file will be in the same location.");
  26.  
  27.  
  28.  
  29.  
  30. //Switch that will convert numbers
  31. while (inFile.hasNext())
  32. {
  33. str = inFile.next();
  34.  
  35. switch(Convert.valueOf(str.substring(4,6)))
  36. {
  37. case bin:
  38.  
  39. int binary= Integer.parseInt(str,2);
  40. break;
  41. case oct:
  42.  
  43. int octal= Integer.parseInt(str,8);
  44. break;
  45. case hex:
  46.  
  47. int hexi= Integer.parseInt(str,16);
  48. break;
  49. case dec:
  50.  
  51. str = str.substring(0,4);
  52. break;
  53. default: System.out.println("default");
  54. }
  55.  
  56. }
  57. }
  58. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 145
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: Change bases in Java

 
1
  #2
Sep 20th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,614
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Change bases in Java

 
0
  #3
Sep 20th, 2009
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'.
Last edited by ~s.o.s~; Sep 20th, 2009 at 10:11 am.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 7
Reputation: Dougnukem is an unknown quantity at this point 
Solved Threads: 1
Dougnukem Dougnukem is offline Offline
Newbie Poster

Re: Change bases in Java

 
0
  #4
Sep 20th, 2009
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.
Last edited by Dougnukem; Sep 20th, 2009 at 3:53 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,614
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Change bases in Java

 
0
  #5
Sep 20th, 2009
>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. :-)
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 7
Reputation: Dougnukem is an unknown quantity at this point 
Solved Threads: 1
Dougnukem Dougnukem is offline Offline
Newbie Poster

Re: Change bases in Java

 
0
  #6
Sep 20th, 2009
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.


  1. import java.io.*;
  2. import java.util.*;
  3.  
  4.  
  5. public class project2
  6. {
  7.  
  8. public enum Convert { bin, oct, hex, dec}
  9. public static void main(String[] args)
  10. throws IOException
  11. {
  12. //Declare variables
  13. String str;
  14. int MAX = 12;
  15. int[] converted_list = new int[MAX];
  16.  
  17. Scanner inFile = new Scanner(new FileReader("E:\\LIFE.txt"));
  18. PrintWriter outFile = new PrintWriter("E:\\REPORT.txt");
  19.  
  20. //Prompt
  21. System.out.println();
  22. System.out.println("Please be sure that the file LIFE is located in the root folder.");
  23. System.out.println("An example of this is C:");
  24. System.out.println("The REPORT file will be in the same location.");
  25. System.out.println();
  26.  
  27.  
  28.  
  29. //Switch that will convert numbers
  30. while (inFile.hasNext())
  31. {
  32. str = inFile.next();
  33.  
  34. switch(Convert.valueOf(str.substring(4,7)))
  35. {
  36. case bin:
  37.  
  38. int binary= Integer.parseInt(str.substring(0,4),2);
  39.  
  40.  
  41. break;
  42. case oct:
  43.  
  44. int octal= Integer.parseInt(str.substring(0,4),8);
  45.  
  46. break;
  47. case hex:
  48.  
  49. int hexi= Integer.parseInt(str.substring(0,4),16);
  50.  
  51. break;
  52. case dec:
  53.  
  54. str = str.substring(0,4);
  55.  
  56. break;
  57. default: System.out.println("One of the numbers in the input file is incorrect. Ex. input 1010bin");
  58.  
  59. }
  60.  
  61. //List for converted numbers
  62.  
  63. List<String> ls=new ArrayList<String>();
  64. ls.add(str.substring(0,4));
  65.  
  66. Iterator it=ls.iterator();
  67.  
  68. while(it.hasNext())
  69. {
  70. String value=(String)it.next();
  71.  
  72. System.out.println(str.substring(0,4));
  73.  
  74. }
  75. }
  76.  
  77. }
  78. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 7
Reputation: Dougnukem is an unknown quantity at this point 
Solved Threads: 1
Dougnukem Dougnukem is offline Offline
Newbie Poster

Re: Change bases in Java

 
0
  #7
Sep 21st, 2009
COMPLETE!
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4.  
  5. public class project2
  6. {
  7.  
  8. public enum Convert { bin, oct, hex, dec}
  9. public static void main(String[] args)
  10. throws IOException
  11. {
  12. //Declare variables
  13. String str;
  14.  
  15.  
  16.  
  17. Scanner inFile = new Scanner(new FileReader("E:\\LIFE.txt"));
  18. PrintWriter outFile = new PrintWriter("E:\\REPORT.txt");
  19.  
  20. //Prompt
  21. System.out.println();
  22. System.out.println("Please be sure that the file LIFE is located in the root folder.");
  23. System.out.println("An example of this is C:");
  24. System.out.println("The REPORT file will be in the same location.");
  25. System.out.println();
  26.  
  27. List<Integer> ls = new ArrayList<Integer>();
  28.  
  29. //Switch that will convert numbers
  30. while (inFile.hasNext())
  31. {
  32. str = inFile.next();
  33.  
  34. switch(Convert.valueOf(str.substring(4,7)))
  35. {
  36. case bin:
  37.  
  38. int binary= Integer.parseInt(str.substring(0,4),2);
  39. ls.add(binary);
  40. break;
  41. case oct:
  42.  
  43. int octal= Integer.parseInt(str.substring(0,4),8);
  44. ls.add(octal);
  45. break;
  46. case hex:
  47.  
  48. int hexi= Integer.parseInt(str.substring(0,4),16);
  49. ls.add(hexi);
  50. break;
  51. case dec:
  52. int deci= Integer.parseInt(str.substring(0,4),10);
  53. ls.add(deci);
  54. break;
  55. default: System.out.println("One of the numbers in the input file is incorrect. Ex. input 1010bin");
  56. }
  57. }
  58. //Output unsorted
  59. System.out.println("Unsorted");
  60. System.out.println(ls);
  61.  
  62. //Output unsorted to file
  63. outFile.println("Unsorted");
  64. outFile.print(ls);
  65. outFile.println("");
  66. outFile.println("Sorted");
  67.  
  68. //Sort
  69. Collections.sort(ls);
  70.  
  71. //Output sorted
  72. System.out.println("Sorted");
  73. System.out.println(ls);
  74.  
  75. //Output sorted to file
  76. outFile.print(ls);
  77.  
  78. //Close files
  79. inFile.close();
  80. outFile.close();
  81. }
  82. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,614
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Change bases in Java

 
1
  #8
Sep 21st, 2009
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 don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 7
Reputation: Dougnukem is an unknown quantity at this point 
Solved Threads: 1
Dougnukem Dougnukem is offline Offline
Newbie Poster

Re: Change bases in Java

 
0
  #9
Sep 21st, 2009
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:
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class project2
  5. {
  6.  
  7. public static void main(String[] args)
  8. throws IOException {
  9.  
  10. converter convertObject = new converter();
  11. convertObject.converting();
  12.  
  13. }
  14. }
And my worker:
  1. public class converter
  2. {
  3. static Scanner console = new Scanner(System.in);
  4. public void converting()
  5. {
  6. //Declare variables
  7. String str;
  8. String infile;
  9. String outfile;
  10.  
  11. //Get file locations
  12. System.out.println("Enter location of the input file.");
  13. System.out.println("An example of this is C:");
  14. infile = console.next();
  15. System.out.println("Enter location of the input file.");
  16. outfile = console.next();
  17. Scanner inFile = new Scanner(new FileReader(infile));
  18. PrintWriter outFile = new PrintWriter(outfile);
  19.  
  20. //Create list
  21. List<Integer> ls = new ArrayList<Integer>();
  22.  
  23. //Switch that will convert numbers
  24. while (inFile.hasNext())
  25. {
  26. str = inFile.next();
  27.  
  28. switch(Convert.valueOf(str.substring(4,7)))
  29. {
  30. case bin:
  31. int binary= Integer.parseInt(str.substring(0,4),2);
  32. ls.add(binary);
  33. break;
  34.  
  35. case oct:
  36. int octal= Integer.parseInt(str.substring(0,4),8);
  37. ls.add(octal);
  38. break;
  39.  
  40. case hex:
  41. int hexi= Integer.parseInt(str.substring(0,4),16);
  42. ls.add(hexi);
  43. break;
  44.  
  45. case dec:
  46. int deci= Integer.parseInt(str.substring(0,4),10);
  47. ls.add(deci);
  48. break;
  49.  
  50. default: System.out.println("One of the numbers in the input file is incorrect. Ex. input 1010bin");
  51. }
  52.  
  53. }
  54.  
  55. //Output unsorted
  56. System.out.println("Unsorted");
  57. System.out.println(ls);
  58.  
  59.  
  60. //Output unsorted to file
  61. outFile.println("Unsorted");
  62. outFile.print(ls);
  63. outFile.println("");
  64. outFile.println("Sorted");
  65.  
  66. //Sort
  67. Collections.sort(ls);
  68.  
  69.  
  70.  
  71. //Output sorted
  72. System.out.println("Sorted");
  73. System.out.println(ls);
  74.  
  75. //Output sorted to file
  76. outFile.print(ls);
  77.  
  78.  
  79.  
  80. //Close files
  81. inFile.close();
  82. outFile.close();
  83.  
  84. }
  85.  
  86. }
Last edited by Dougnukem; Sep 21st, 2009 at 3:01 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,614
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Change bases in Java

 
0
  #10
Sep 21st, 2009
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.
Last edited by ~s.o.s~; Sep 21st, 2009 at 10:05 am.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC