943,733 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1846
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 20th, 2009
1

Change bases in Java

Expand Post »
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.
Java Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 22
Solved Threads: 0
Newbie Poster
Hanyouslayer is offline Offline
18 posts
since Sep 2009
Sep 20th, 2009
1

Re: Change bases in Java

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.
Featured Poster
Reputation Points: 1907
Solved Threads: 950
Posting Expert
JamesCherrill is offline Offline
5,768 posts
since Apr 2008
Sep 20th, 2009
1

Re: Change bases in Java

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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Sep 20th, 2009
0

Re: Change bases in Java

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.
Reputation Points: 15
Solved Threads: 1
Newbie Poster
Dougnukem is offline Offline
7 posts
since Sep 2009
Sep 20th, 2009
1

Re: Change bases in Java

>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. :-)
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Sep 20th, 2009
0

Re: Change bases in Java

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.


Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 15
Solved Threads: 1
Newbie Poster
Dougnukem is offline Offline
7 posts
since Sep 2009
Sep 21st, 2009
0

Re: Change bases in Java

COMPLETE!
Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 15
Solved Threads: 1
Newbie Poster
Dougnukem is offline Offline
7 posts
since Sep 2009
Sep 21st, 2009
2

Re: Change bases in Java

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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Sep 21st, 2009
0

Re: Change bases in Java

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:
Java Syntax (Toggle Plain Text)
  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:
Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 15
Solved Threads: 1
Newbie Poster
Dougnukem is offline Offline
7 posts
since Sep 2009
Sep 21st, 2009
1

Re: Change bases in Java

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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: seminar related to java
Next Thread in Java Forum Timeline: DB related query





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC